Why does imclose(Image,nhood) in MATLAB give different output than MORP.CLOSE in OpenCV?

289 Views Asked by At

I am trying to convert some MATLAB code to Python, related to image-processing.

When I did

% matlab R2017a
nhood = true(5);     % will give 5x5 matrix containing 1s size 5x5
J = imclose(Image,nhood);

in MATLAB, the result is different than when I did

import cv2 as cv
kernel = np.ones((5,5),np.uint8)  # will give result like true(5)
J = cv.morphologyEx(Image,cv.MORPH_CLOSE,kernel)

in Python.

This is the result of MATLAB:

MATLAB results

And this is for the Python:

Python results

The difference is 210 pixels, see below. The red circle shows the pixels that exist in Python with 1 value but not in the MATLAB.

the difference results

Sorry if it’s so small, my image size is 2048x2048 and have values 0 and 1, and the error just 210 pixels.

When I use another library such as skimage.morphology.closing and mahotas.close with the same parameter, it will give me the same result as MORPH.CLOSE.

What I want to ask is:

  1. Am I using the wrong parameter in Python like the kernel = np.ones((5,5),np.uint8)?
  2. If not, is there any library that will give me the same exact result like imclose() MATLAB?
  3. Which of the MATLAB and Python results is correct?

I already looked at this Q&A. When I use borderValue = 0 in MORPH.CLOSE, my result will give me error 2115 pixels that contain 1 value in MATLAB but not in the Python.

[ UPDATE ]

  1. the input image is Input Image

  2. the cropped of the difference pixels is cropped difference image

  3. So for the difference pixels image, it turns out that the pixels are not only in that position but scattered in several positions. You can see it here more specific image of difference pixels

And if seen from the results, the location of the pixel error coincides at the ends of the row or column of the matrix. I hope it can make more hints for this question.

  1. This is the program in MATLAB that i use to check the error,
mask = zeros(2048,2048); %inisialisasi error matrix  
error = 0;
for x = 1:size(J_Matlab,1)
    for y = 1:size(J_Matlab,2)
        if J_Matlab(x,y)== J_Python(x,y)
            mask(x,y) = 0; % no differences
        else
            mask(x,y) = 1;
            error = error + 1;
        end
    end
end

so i load the Python data into MATLAB, then i compare it in with the MATLAB data. And if you want to check the data that i use for the input in closing function, you can look it in the comment section ( in drive link )

  1. so for this problem, my teacher said that it was ok to use either MATLAB or Python program because the error is not significant. but if i found the solution, i will post it here ASAP. Thanks for the instruction, suggestions, and critics for my first post.
0

There are 0 best solutions below