【问题标题】:Python cv2 edge and contour detectionPython cv2 边缘和轮廓检测
【发布时间】:2018-07-20 21:44:25
【问题描述】:

我正在尝试检测 OMR 表上的气泡,看起来像这样:

我的边缘检测和轮廓显示代码引用自here。但是,在找到实际轮廓之前,我试图检测边缘但不知何故无法设置正确的参数值。 这是我得到的:

代码:

from imutils.perspective import four_point_transform
from imutils import contours
import numpy as np
import argparse
import imutils
import cv2

def auto_canny(image, sigma=0.50):
    # compute the median of the single channel pixel intensities
    v = np.median(image)

    # apply automatic Canny edge detection using the computed median
    lower = int(max(0, (1.0 - sigma) * v))
    upper = int(min(255, (1.0 + sigma) * v))
    edged = cv2.Canny(image, lower, upper)


# return the edged image
return edged

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
    help="path to the input image")
args = vars(ap.parse_args())
image = cv2.imread(args["image"])

r = 500.0 / image.shape[1]
dim = (500, int(image.shape[0] * r))

# perform the actual resizing of the image and show it
image = cv2.resize(image, dim, interpolation = cv2.INTER_AREA)

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
equalized_img =  cv2.equalizeHist(gray)
cv2.imshow('Equalized', equalized_img)
# cv2.waitKey(0)
blurred = cv2.GaussianBlur(equalized_img, (7, 7), 0)
# edged =cv2.Canny(equalized_img, 30, 160)
edged = auto_canny(blurred)

cv2.imshow('edged', edged)
cv2.waitKey(0)

如何获得所有 90*4 的圆圈?

【问题讨论】:

  • 你可以从检测到的圆圈中推断出圆圈之间的方向和距离,这样你就可以得到缺失圆圈的坐标。
  • 你试过大津二值化吗?
  • 嗨 Akhilesh,以下任何答案都有帮助吗?抱歉恢复晚了。

标签: python opencv


【解决方案1】:

您应该使用 Hough 来搜索圈子。此方法将每个白色像素投影为一个圆圈,并尝试获得尽可能多的重叠像素。您必须指定要在图像中找到的圆的预测半径。

  • 左 - 原始图像
  • 右上角 - 每个白色像素都投影为红色圆圈 - 它们太小而无法找到相交点
  • 右下 - 绿色圆圈更大,所有相交点都在圆圈的中间相交!半径和位置都由cvHoughCircles 返回

此人使用cvHoughCirclescvCanny 化图像处理了斑点检测(我认为这就是寻找圆圈)。

OpenCV: Error in cvHoughCircles usage

【讨论】:

    【解决方案2】:

    您需要改进轮廓检测。 最终通过不改变它,但通过更好地预处理早期阶段。 轮廓检测在图像中具有更多对比度和颜色分离效果更好。如果您还不需要使用 Simple Threshold、Adaptive 或更智能的技术(如 Otsu)对图像进行阈值处理。检查打开简历文档here

    除此之外,对于您的情况,最终需要更高级的技术,例如“使用积分图像的自适应阈值”,描述为 here

    【讨论】:

    • 不接受仅链接的答案。请在您的回答中提供上下文,以便它是独立的。
    猜你喜欢
    • 2013-12-29
    • 2017-11-19
    • 2013-06-10
    • 2016-01-11
    • 1970-01-01
    • 2016-04-23
    • 2020-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多