Ask Your Question
0

connectedComponentsWithStats using problem

asked 2018-11-30 02:42:00 -0600

wxchen gravatar image

hi all :

I recently used the function to detecting object by using "connectedComponentsWithStats" operator with binary image. I found a problem in that operator.

Here is my code:

//create an all white binary image
cv::Mat binTest = cv::Mat(1000 , 2000 , CV_8UC1) ;
std::memset(binTest.data , 255 , 1000 * 2000) ;
//find out connected component
cv::Mat _dstImg , __stat , _centroid ;
int cc = cv::connectedComponentsWithStats( binTest , _dstImg , __stat , _centroid, 8 , CV_32S , cv::ConnectedComponentsAlgorithmsTypes::CCL_GRANA) ;

It will return 2 objects, and get INT_MAX,INT_MAX,2,2,0 stat at first object. At the second object, it will return full image object.(right object)

So, i wanna ask : 1. What's problem in my code? 2. Is that operator must ignore the first detected object?

My testing environment as follow: Operating System: Windows 10 Compiler: VS 2012 x64(VC11.0) OpenCV Version: 3.2 & 3.4.0

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-11-30 03:21:54 -0600

LBerger gravatar image

updated 2018-11-30 03:25:49 -0600

First don't use memset :

    cv::Mat binTest = cv::Mat(1000, 2000, CV_8UC1,Scalar(255));
    //find out connected component
    cv::Mat _dstImg, __stat, _centroid;
    int cc = cv::connectedComponentsWithStats(binTest, _dstImg, __stat, _centroid, 8, CV_32S,            cv::ConnectedComponentsAlgorithmsTypes::CCL_GRANA);

Now results is for __stat :

stat
[2147483647, 2147483647, 2, 2, 0;
 0, 0, 2000, 1000, 2000000]
centroid 
[-nan(ind), -nan(ind);
 999.5, 499.5]

last column of stat is surface then surface for connected components with 0 label is 0. Results are good , weird but good 2 components but one empty!

Now with cv::Mat binTest = cv::Mat(1000, 2000, CV_8UC1,Scalar(0));

results :

 stat
[0, 0, 2000, 1000, 2000000]
centroid
 [999.5, 499.5]

May be you can post an issue

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-11-30 02:42:00 -0600

Seen: 1,815 times

Last updated: Nov 30 '18