pythongui界面复选框数值选择并求和_如何使用Python从图像中分离复选框按钮和复......

我已经拆分了以并行方式排列的图像,并像下面的图像一样保存了它们.

这是我的工作代码,用于拆分方形复选框和该图像中的文本.

# Import necessary libraries

from matplotlib import pyplot as plt

import cv2

# Read image

image = cv2.imread('data_2/5.png')

# Height and width of the image

height, width = image.shape[:2]

print("Dimensions of the image.")

print("Height:",height)

print("Width:",width)

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

gray = cv2.GaussianBlur(gray, (5,5), 0)

# Finding Edges

edges = cv2.Canny(gray, 60, 255)

# contours -> an outline representing or bounding the shape.

_,cnts, hierarchy = cv2.findContours(edges.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

contours = sorted(cnts, key=cv2.contourArea, reverse=True)[:10]

count = 1

for contour in contours:

if(count<=4):

#print("Count:",count)

count = count + 1

area = cv2.contourArea(contour)

if area > 100000 and area < 1000:

contours.remove(contour)

perimeter = cv2.arcLength(contour, True)

approx = cv2.approxPolyDP(contour, 0.01*perimeter, True)

if len(approx) == 4:

cv2.circle(image, (720, 360), 5, (255,0,0), 5)

cv2.drawContours(image, [approx], -1, (0, 255, 0), 2)

M = cv2.moments(approx)

centers = []

if M["m00"] != 0:

cX = int(M["m10"] / M["m00"])

cY = int(M["m01"] / M["m00"])

else:

cX, cY = 0, 0

P1 = approx[0]

P1x = P1[0][0]

P1y = P1[0][1]

P2 = approx[1]

P2x = P2[0][0]

P2y = P2[0][1]

P3 = approx[2]

P3x = P3[0][0]

P3y = P3[0][1]

P4 = approx[3]

P4x = P4[0][0]

P4y = P4[0][1]

plt.imshow(image)

plt.title('Detecting Square')

plt.show()

# Cropping the square_image using array slices -- it's a NumPy array

cropped_square = image[P1y:P3y, P2x:P3x]

# Cropping the text image

cropped_text = image[P1y:P3y,P3x+5:width]

# Displaying the cropped square and cropped text image.

plt.imshow(cropped_square)

plt.title('Cropped Square')

plt.show()

plt.imshow(cropped_text)

plt.title('Cropped Text')

plt.show()

# Now saving the cropped square and cropped text image

cv2.imwrite('results/square1.png',cropped_square)

cv2.imwrite('results/text1.png',cropped_text)

以上程序的输出:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值