检测OCR标记

3
我正在处理光学标记识别问题。我找到了需要填写学生考号的感兴趣区域(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()

1
在findContours中使用“EXTERNAL”标志,而不是cv2.RETR_TREE - Micka
1个回答

1
由于标记为黑色,因此您应该尝试在输入图像中分割黑色段,并从二进制掩模中找到轮廓并过滤出圆形轮廓(如果需要,还可以过滤面积轮廓)。
找到所有轮廓后,按其边界矩形的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

Output:

Roll Number:  4216789503

1
也许你正在使用OpenCV2.x API,而我编写的代码是在OpenCV 3.1.0中编写的,可能会有一些小差异,例如在OpenCV2.x中,cv2.findContours()返回2个对象作为contours, hierarchy,因此您需要将该行替换为contours, hierarchy = cv2.findContours(marker_seg_mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) - ZdaR
那么问题是什么?你能否上传新的图片,其中填充了4216789503个气泡? - ZdaR
这段代码的当前输出是什么?你期望的输出是什么? - ZdaR
当您应用代码于最新更新的图像时,它会显示错误: OpenCV错误:在cvtColor函数中,文件/build/buildd/opencv-2.4.8+dfsg1/modules/imgproc/src/color.cpp的第3737行中断言失败(scn == 3 || scn == 4) Traceback(最近的调用)如下: File "bubbledetect.py", line 14, in <module> gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) cv2.error: /build/buildd/opencv-2.4.8+dfsg1/modules/imgproc/src/color.cpp:3737: error: (-215) scn == 3 || scn == 4 in function cvtColor - Pandey Rashmi
1
哦,我明白了,数字解释出了一些问题,我已经更新了代码@PandeyRashmi。 - ZdaR

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接