Reputation: 1567
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?
Upvotes: 5
Views: 1449
Reputation: 11
First, two 3x3 in squential should be same to a 5x5. Second, as for the performance, 5x5 is slower. There are some rough ideas about it: if you consider raw implementation (based on loop), the number of calculation should be 3x3xWxHx2 comparing to 5x5xWxHx1.
Upvotes: -2
Reputation: 3486
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.
Upvotes: 5
Reputation: 2108
You will have to measure the performance yourself but it seems logical that one dilation with a 6x6 element should be faster. Wikipedia sais that binary dilation is associative. This means that if a 3x3 rectangle dilated with another such rectangle gives a 6x6 rectangle then indeed two dilations by 3x3 are equivalent to one dilation by 6x6.
Upvotes: -2