Erosion and dilation in Python OpenCV returns white
<p>I am trying to use dialation and ertion</p>

<p>For example, like so:</p>

<pre><code>dialated = cv2.dilate(edgesCopy, cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3)), iterations = 1)
</code></pre>

<p>The input is a uint8 image that has only values of 0 and 255, as came out of</p>

<pre><code>threshold, thresholdedEdges = cv2.threshold(edges, 220, 255, cv2.THRESH_BINARY_INV);
</code></pre>

<p>The output, however is just a white image. I fail to understand the reason.</p>

<p>The entire code is</p>

<pre><code>   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)
</code></pre>

<p>The result of the canny edge detection is image with binary edges of thickness 1. You are thresholding this edges (which is not needed by the way) with a threshold setting cv2.THRESH_BINARY_INV, which means that the threshold result gets value 1 where pixels are bellow threshold and 0 when above. The result of such thresholding is naturaly almost white image with black lines -> you are actually just inverting the result of canny edge detector. Dilating such an image finally results in totally white image (whatever the input image actually is).</p>

<p>I suggest you to just skip the thresholding step!</p>

<p>If you want to do the thresholding nevertheless, use THRESH_BINARY and set maxval to 255. I also think that there should be cv2.waitKey() function call after each cv2.imshow() (at least in my case it doesnt show anything otherwise).</p>

python opencv image-processing