我正在努力自动纠正扫描的气泡表测试。 目前,我可以提取工作表的解决方案部分并修复其旋转。
在输出图像中运行以下代码
def get_answers(image):
display_normal("Just image",image)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurry = cv2.GaussianBlur(gray, (3, 3), 1)
thresh = cv2.threshold(blurry, 225, 255,
cv2.THRESH_BINARY_INV)[1]
display_normal("Binary", thresh)
# find contours in the thresholded image, then initialize
# the list of contours that correspond to questions
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[1]
questionCnts = []
# loop over the contours
for c in cnts:
# compute the bounding box of the contour, then use the
# bounding box to derive the aspect ratio
(x, y, w, h) = cv2.boundingRect(c)
ar = w / float(h)
# in order to label the contour as a question, region
# should be sufficiently wide, sufficiently tall, and
# have an aspect ratio approximately equal to 1
if w >= 18 and h >= 18 and 0.9 <= ar and ar <= 1.2:
questionCnts.append(c)
cv2.drawContours(image, questionCnts, -1, (255, 0, 0), 1)
display_normal("Image with contours",image.copy())
if(questionCnts < 45*4):
raise Exception("Didn't found all possible answers")
问题在于:我将输入图像转换为二进制并尝试查找看起来像圆圈的轮廓,但我找不到全部可能的45 * 4选项..我无法检测到其中一些圆圈。
那么有没有更好的想法/算法来完成这个特定的任务?
答案 0 :(得分:6)
您可以尝试使用自适应阈值:
adapt_thresh = cv2.adaptiveThreshold(equ, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
cv2.imshow('adapt_thresh.jpg', adapt_thresh)
(我调整了原始图片的大小以使其更小)
<强>更新强>
我刚刚执行的另一种方法.......
我使用直方图均衡来均衡灰度图像:
equalized_img = cv2.equalizeHist(gray)
cv2.imshow('Equalized Image.jpg', equalized_img )
然后我使用np.median(equalized_img)
获得均衡图像的中值,并通过选择 [0.6 *中位数] 以下的所有像素值来应用二进制阈值
ret, thresh = cv2.threshold(equalized_img, lower, 255, 1)
cv2.imwrite("Final Image.jpg", thresh)
现在,您可以继续在此图像上找到所需的轮廓。
希望它有所帮助.. :)