Search code examples
c++opencvimage-processingmathematical-morphology

Is performing two morphological Dilation with 3x3 structuring element equal to one with 6x6 structuring element?


My question is simple. It might be too simple. But the thing is while working on one of my projects I used the following lines to dilate a binary image.

cv::dilate(c_Proj, c_Proj, Mat(), Point(), 2);

Which is basically dilating the binary image with a 3x3 rectangular structuring element. From the last argument you can see I am performing 2 iterations of this operations, which is equivalent to:

cv::dilate(c_Proj, c_Proj, Mat(), Point(), 1);
cv::dilate(c_Proj, c_Proj, Mat(), Point(), 1);

My question is this: Instead of performing two iterations, if I perform only one iteration using a 6x6 structuring element, is this equivalent to the above code in terms of accuracy and performance? Is it faster as the image is iterated only once?


Solution

  • Dilation with the same kernel can be expressed with two convolution operations:

    ("YourImage" convolve "DilationKernel") convolve "DilationKernel" 
    

    Because of the properties of convolution, this operation is equivelant to:

    "YourImage" convolve ( "DilationKernel" convolve "DilationKernel")
    

    A convolution of a 3x3 kernel with itself will result in 5x5 matrix, so your 6x6 assumption is wrong.

    In terms of performance, there is so much to consider. In my previous internship, our aim is to use as small kernel as possible because of performance losses of bigger kernels. Rule of thumb is small kernels act faster on an image because simply you can store and retrieve them using CPU registers, without accessing L1 or L2 caches. Also if your kernel fits in registers, you can easily use SSE instructions.

    Parallelization of the convolution is another story, and I don't have much practical information about it. So I don't know this empirical facts still holds if using a parallelized implementation.