Ask Your Question

Can cv2.dilate using with even kernel like 2x2 or 4x4?

asked 2015-06-26 14:14:41 -0500

Shiloh gravatar image

Is that possible doing dilation with even kernel in cv2.dilate?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2015-06-26 15:22:21 -0500

LBerger gravatar image

updated 2015-06-27 04:30:31 -0500

Best way is to test

Mat m2=Mat::ones(2,2,CV_8U);
Mat m3=Mat::ones(3,3,CV_8U);
float x = norm(dst);
cout<<"Difference between erode 2x2 and 3x3 : "<<x;

the difference is 4337.25 and for anchor (ksize = 2 or 3 in my example and anchor.x==-1) line 357 filterengine.hpp opencv 3.0

   if( anchor.x == -1 )
       anchor.x = ksize.width/2;
   if( anchor.y == -1 )
       anchor.y = ksize.height/2;
edit flag offensive delete link more


I already try it, but the kernel 2x2 and 3x3 give result the same image, and where opencv put the origin of structure element if kernel 2x2?

Shiloh gravatar imageShiloh ( 2015-06-27 01:55:33 -0500 )edit

@LBerger I'm sorry there is mistake when I writing the code, it's giving different result now, but I'm still confuse the anchor x,y value. If default value=(-1,-1) and I use kernel 2x2 so the anchor x,y=1, so it's mean the origin is in bottom right for kernel 2x2 isn't it?

Shiloh gravatar imageShiloh ( 2015-06-27 10:34:08 -0500 )edit

Yes with -1 default value is width and height which are used. Filter for 2x2 filter ksize=2 so 2/2 equal 1

_0 1

0 - -

1 - x

and center is at x

LBerger gravatar imageLBerger ( 2015-06-27 11:07:56 -0500 )edit

@LBerger ahh.. Thanks for helping me.. I understand now... :)

Shiloh gravatar imageShiloh ( 2015-06-28 04:27:27 -0500 )edit

@LBerger, can I ask the last question? Is there maximum value for parameter iterations?

Shiloh gravatar imageShiloh ( 2015-06-28 05:21:44 -0500 )edit

If Iterations value is 25 itmeans that you are going to make 25 dilate. But in opencv source code at line 1698 you can find that image is dilated only once. But operator is resize 25 times in function getStructuringElement at line 1028 . Of course results are similar. In that case you must pay attention to border value.Hence Limit is relative to source image size something like max(height,rows) of source image divide size of structuring element

PS I hope that my reading is good may be you should check it if you have a specific ned for dilate

LBerger gravatar imageLBerger ( 2015-06-28 08:08:58 -0500 )edit

I can understand your explanation very well, thank you so much for explanation and the reference source code.. :)

Shiloh gravatar imageShiloh ( 2015-06-28 10:44:20 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2015-06-26 14:14:41 -0500

Seen: 724 times

Last updated: Jun 27 '15