python – 在opencv中突出显示所有可能的圆圈(气泡表选项)
作者:互联网
我正在努力自动纠正扫描的气泡表测试.
目前,我可以提取工作表的解决方案部分并修复其旋转.
在输出图像中运行以下代码
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选择..我无法检测到这些圆圈中的一些..
那么有没有更好的想法/算法来完成这个特定的任务?
解决方法:
您可以尝试使用自适应阈值:
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)
现在,您可以继续在此图像上找到所需的轮廓.
希望能帮助到你 .. :)
标签:opencv3-0,python,opencv,image-processing 来源: https://codeday.me/bug/20190823/1700838.html