【问题标题】:Erosion and dilation in Python OpenCV returns whitePython OpenCV 中的腐蚀和膨胀返回白色
【发布时间】:2017-04-16 18:20:25
【问题描述】:

我正在尝试使用 dialation 和 ertion

例如,像这样:

dialated = cv2.dilate(edgesCopy, cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3)), iterations = 1)

输入是一个 uint8 图像,只有 0 和 255 的值,来自

threshold, thresholdedEdges = cv2.threshold(edges, 220, 255, cv2.THRESH_BINARY_INV);

然而,输出只是一个白色图像。我不明白原因。

整个代码是

   imageSize = img.shape
    if len(imageSize) != 2:#color
        print "got a color image - quitting"
        return

    cv2.imshow("im1", img)
    cv2.moveWindow("im1", 60, 50)

    gaussianBlur = cv2.GaussianBlur(img, (5, 5), 0)
    # cv2.imshow("gaussianBlur", gaussianBlur)
    # cv2.moveWindow("gaussianBlur", 260, 50)

    medianBlur = cv2.medianBlur(gaussianBlur, 5)
    # cv2.imshow("medianBlur", medianBlur)
    # cv2.moveWindow("medianBlur", 460, 50)

    minGradientValueThreshold = 225
    maxGradientValueThreshold = 150
    edges = cv2.Canny(medianBlur, minGradientValueThreshold, maxGradientValueThreshold)
    cv2.imshow("edges", edges)
    cv2.moveWindow("edges", 660, 50)

    # Threshold.
    # Set values equal to or above 220 to 0.
    # Set values below 220 to 255.

    threshold, thresholdedEdges = cv2.threshold(edges, 220, 1, cv2.THRESH_BINARY_INV);

    edgesCopy = thresholdedEdges.copy()

    #close the edges before floodfilling, to avoid filing the background
    # closing = cv2.morphologyEx(floodFilledImage, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_RECT,(5,5)))  DOESN'T WORK
    dialated = cv2.dilate(edgesCopy, cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3)), iterations = 1)

    cv2.imshow("dialated", dialated)
    cv2.moveWindow("dialated", 60, 250)

    eroded = cv2.erode(dialated, cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3)), iterations = 1)

    closing = eroded

    cv2.imshow("closing", closing)
    cv2.moveWindow("closing", 60, 250)

【问题讨论】:

  • 请问你为什么用不同的滤镜两次模糊你的图像?

标签: python opencv image-processing


【解决方案1】:

canny 边缘检测的结果是具有厚度为 1 的二进制边缘的图像。您正在使用阈值设置 cv2.THRESH_BINARY_INV 对这些边缘进行阈值处理(顺便说一句,这不是必需的),这意味着阈值结果的值为 1 ,其中像素低于阈值,高于阈值时为 0。这种阈值处理的结果自然是几乎是带有黑线的白色图像->您实际上只是在反转精明边缘检测器的结果。对这样的图像进行膨胀最终会产生完全白色的图像(无论输入图像实际上是什么)。

我建议你跳过阈值步骤!

如果您仍然想进行阈值处理,请使用THRESH_BINARY 并将maxval 设置为255。我还认为每个cv2.imshow() 之后应该有cv2.waitKey() 函数调用(至少在我的情况下它没有显示其他)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-01
    • 1970-01-01
    相关资源
    最近更新 更多