Search code examples
c++matlabopencvimage-processingmathematical-morphology

Disk Structuring Element opencv vs Matlab


I want to create a disk shaped structuring element on OpenCv. I need my SE to be similar with

sel = strel('disk',5);

I want to do this using

cvstructuringElementEx(cols,rows,anchor_x,anchor_y,shape,*values);

What do I need to do to achieve this and which values of anchor_x and anchor_y give the same center point of SE with MATLAB?


Solution

  • According to the docs, you could try:

    cv::Mat sel = cv::getStructuringElement(MORPH_ELLIPSE, cv::Size(9,9));
    

    This gave me the following structuring element:

    0    0    0    0    1    0    0    0    0
    0    1    1    1    1    1    1    1    0
    0    1    1    1    1    1    1    1    0
    1    1    1    1    1    1    1    1    1
    1    1    1    1    1    1    1    1    1
    1    1    1    1    1    1    1    1    1
    0    1    1    1    1    1    1    1    0
    0    1    1    1    1    1    1    1    0
    0    0    0    0    1    0    0    0    0
    

    While in MATLAB I got:

    >> getnhood(strel('disk',5))
    ans =
         0     0     1     1     1     1     1     0     0
         0     1     1     1     1     1     1     1     0
         1     1     1     1     1     1     1     1     1
         1     1     1     1     1     1     1     1     1
         1     1     1     1     1     1     1     1     1
         1     1     1     1     1     1     1     1     1
         1     1     1     1     1     1     1     1     1
         0     1     1     1     1     1     1     1     0
         0     0     1     1     1     1     1     0     0
    

    So not exactly the same but close enough :)