Search code examples
pythonopencvimage-processingimage-morphology

Iterations in morphology closing


I have a code that goes like:

def blockKernel(sizeX, sizeY = None):
    if sizeY == None:
        sizeY = sizeX
    return np.ones((sizeX, sizeY), np.uint8)

img = readImage() // skipping readImage function, since irrelevant
closeBlock1 = cv.morphologyEx(img, cv.MORPH_CLOSE, blockKernel(3), iterations=1) 
closeBlock2 = cv.morphologyEx(img, cv.MORPH_CLOSE, blockKernel(3), iterations=3)

I get the following outputs:

closeBlock1:

closeBlock1

closeBlock2:

closeBlock2

But then I saw this. It says:

According to the "Digital Image Processing, 3rd edition", by Gonzales, The multiple application of opening/closing doesn't have any effect after the first time you apply it!

But that's not the result I'm getting. I'm getting 1 vs 3 iterations, and getting different results. Am I doing something wrong or misunderstanding something?


Solution

  • cv.morphologyEx(img, cv.MORPH_CLOSE, blockKernel(3), iterations=3) applies the closing with the iteration of kernel, it does not iterate the closing.

    The morphological closing is a dilation followed by an erosion. This function applies the dilation by iterating the 3x3 dilation three times, then the erosion by again iterating the 3x3 erosion three times.