gpt4 book ai didi

python - 检测 OCR 标记

转载 作者:太空宇宙 更新时间:2023-11-03 21:11:06 26 4
gpt4 key购买 nike

我正在研究光学标记识别问题。我找到了感兴趣区域 (ROI),其中要填充学生的卷号。哪种方法可以帮助我解码填充的圆值?我尝试编写代码,但它无法正常运行。

图片

在这张图片中给出了初始 ROI。之后我应用了分割。第三张图片由学生填充,表示学生的卷号。

这张图检测到381个圆,实际是100个

Input: Filled circle image
Output: roll number : 4216789503

image = cv2.imread("rotatedb/ROI_omr.png")
hsvimg = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower_blue = np.array([0,70,0])
upper_blue = np.array([255,255,255])
mask = cv2.inRange(hsvimg, lower_blue, upper_blue)
contours, hierarchy = cv2.findContours(mask.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
print "No. of circles",len(contours)

i=0
for contour in contours:
(x,y),radius = cv2.minEnclosingCircle(contour)
center = (int(x),int(y))
radius = int(radius)
cv2.circle(image,center,radius,(0,255,0),2)
position = (center[0] - 10, center[1] + 10)
text_color = (0, 0, 255)
cv2.putText(image, str(i + 1), position, cv2.FONT_HERSHEY_SIMPLEX, 0.5, text_color, 2)
i=i+1

cv2.imshow("thresold",image)
cv2.waitKey(0)
cv2.destroyAllWindows()

最佳答案

因为标记是黑色的,所以你应该尝试分割输入图像中的黑色部分,你可以从那个二进制掩码中找到轮廓并过滤掉圆形轮廓(你可能还想过滤如果你愿意,可以用区域绘制轮廓)。

找到所有轮廓后,根据边界矩形的 x 坐标对轮廓进行排序,这将产生我们在水平方向遍历轮廓时的轮廓顺序(cv2. findContours() 以随机顺序返回轮廓,因此根据您的需要对它们进行排序始终是个好主意。)

最后,您计算每个轮廓的中点并估计它们所在的圆。

代码:

import cv2

img = cv2.imread('/Users/anmoluppal/Downloads/QYtuv.png')
MARKER_LOWER_BOUND = ( 0, 0, 0)
MARKER_UPPER_BOUND = (20, 20, 20)

img = cv2.blur(img, (7, 7))
marker_seg_mask = cv2.inRange(img, MARKER_LOWER_BOUND, MARKER_UPPER_BOUND)

# Number of rows and columns of number matrix
n_rows, n_cols = 10, 10
single_element_height, single_element_width = marker_seg_mask.shape[0]/10, marker_seg_mask.shape[1]/10

# Now find the contours in the segmented mask
img, contours, hierarchy = cv2.findContours(marker_seg_mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

# Sorting the contours w.r.t contour rect X
contours.sort(key = lambda x:cv2.boundingRect(x)[0])

# Now iterate over each contour and see if it is in circular shape
roll_number = ""
for contour in contours:
approx = cv2.approxPolyDP(contour, 0.01*cv2.arcLength(contour,True), True)
if len(approx) > 8:
# Find the bounding rect of contour.
contour_bounding_rect = cv2.boundingRect(contour)
mid_point = contour_bounding_rect[0] + contour_bounding_rect[2]/2, contour_bounding_rect[1] + contour_bounding_rect[3]/2
roll_num_digit = mid_point[1]/single_element_height

# Since your numbering format is from 1, 2, 3, ... 0, So to parse the roll number correctly we need additional operation
roll_num_digit = (roll_num_digit + 1) % 10
roll_number += str(roll_num_digit)
print "Roll Number: ", roll_number

输出:

Roll Number:  4216789503

关于python - 检测 OCR 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41426130/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com