findContours in OpenCV doesn't match bwboundaries in Matlab?

asked 2019-06-11 13:44:52 -0500

e1513377 gravatar image

I'm looking for C++ code (doesn't have to be in OpenCV) that gives the exact equivalent results to the Matlab bwboundaries command. I see there's findContours in OpenCV, but it is not giving the same results as bwboundaries is

The code I used for findContours is:

cv::Mat mat = cv::imread("lennabin.bmp");
vector<vector<cv::Point> > contours;
vector<cv::Vec4i> hierarchy;
vector<vector<cv::Point> > contours0;
cv::cvtColor(mat, mat, cv::COLOR_BGR2GRAY);

cv::Mat tMat;
cv::transpose(mat, tMat);
//findContours(mat, contours0, hierarchy, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE);
findContours(tMat, contours0, cv::RETR_LIST, cv::CHAIN_APPROX_NONE);
//findContours(mat, contours0, hierarchy, cv::RETR_CCOMP, cv::CHAIN_APPROX_SIMPLE);

std::cout << "\nopenCV findcontours L:"<< contours0.size()<<"\n";
for (int i = 0; i < contours0.size(); i++) {
    std::cout << "\ni: "<<i<<" points: "<< contours0[i].size();
    int jsize;
    if (contours0[i].size() >= 5) {
        jsize = 5;
    }
    else {
        jsize = contours0[i].size();
    }
    for (int j = 0; j < jsize; j++)
    {
        std::cout << "\n"<<contours0[i][j].x << " " << contours0[i][j].y;
    }
}

and the code for bwboundaries is:

[B,~,~] = bwboundaries(im);
disp(['Length(B): ', num2str(length(B))])
for i=1:3
    disp(['Length(B{i}): ', num2str(length(B{i}))])
    disp(['i: ',num2str(i)])
    boundary=B{i};
    [r,c]=size(boundary);
    if r<5
        jL=r;
    else
        jL=5;
    end
    disp('xy')
    for j=1:jL
        disp([num2str(boundary(j,1)), ...
            ' ',num2str(boundary(j,2))])
    end
end


for i=1:length(B)
    if length(B{i})==17
        disp(['Length(B{i}): ', num2str(length(B{i}))])
        disp(['i: ',num2str(i)])
        boundary=B{i};
        disp('xy')
        for j=1:length(B{i})
            disp([num2str(boundary(j,1)), ...
                ' ',num2str(boundary(j,2))])
        end
    end
end

and the outputs I get for C++ is:

openCV findcontours L:220

i:0 points 1
188 206
i:1 points:17
218 202
217 203
216 204
215 205
214 206
i:2 points: 4
8 194
9 193
10 194
9 195

and Matlab:

run_bwboundaries(lennabind);
Length(B): 220
Length(B{i}): 583
i: 1
xy
1 1
1 2
1 3
1 4
1 5
Length(B{i}): 107
i: 2
xy
172 1
172 2
173 3
174 4
175 5
Length(B{i}): 11
i: 3
xy
205 29
206 29
207 29
208 29
209 29

Length(B{i}): 17
i: 211
xy
139 127
139 128
139 129
139 130
140 130
140 131
140 132
141 132
141 133
141 132
141 131
140 131
140 130
140 129
140 128
140 127
139 127

The outputs of the x,y coordinates of the contours don't match, even for the contour with length 17

How can I get them to match? Or is there other C++ code that can get the same results as bwboundaries?

edit retag flag offensive close merge delete

Comments

that gives the exact equivalent results to the Matlab

why do you even expect that to happen ?

berak gravatar imageberak ( 2019-06-12 03:15:11 -0500 )edit