0

I am trying to classify a connected component in a video based on its location. I am using the connectedComponentsWithStats function from opencv 3. I am trying to access values from the stats matrix which is a 1x5 matrix. When i use the following code I access the area of the connected component successfully.

connectedComponentsWithStats(median_difference3, labels, stats, centroids, 8, CV_16U);

int area= stats.at<int>(0,4);
cout << area << endl;

When i try to access the first value of the matrix using

int x= stats.at<int>(0,0);

All the values I get are zero.

Just to confirm when I output the full stats matrix the values in the 0,0 location of the matrix are not always zero so they should vary the same way the area matrix does. The values in the stat matrix are always integers also.

I would appreciate some help if you can see what i have done wrong!

Miki
  • 40,887
  • 13
  • 123
  • 202
  • 1
    This seems correct. Can you setup a [mcve] so we can reproduce your problem? – Miki Dec 21 '15 at 16:19
  • I have just realised is a 1x5 vector when there are no connected components in the frame. In the event that there are the matrix is a 2 x 5 matrix with the first row containing the default values and the second row containing useful information about the CC. This is why I have been getting zeros all the time. Do you know how I can access the the second row in the matrix as there is not always two rows and stats.at(1, 0); will not work. – user5608415 Dec 21 '15 at 16:50
  • `if(stats.rows > 1) {cout << stats.at(1,0);}` – Miki Dec 21 '15 at 16:52
  • 1
    the first row is for the background, which has always label 0, btw :D – Miki Dec 21 '15 at 16:53
  • Haha yes I see now I can't believe that confused me for so long. Thank you so much for the help! – user5608415 Dec 21 '15 at 16:58

1 Answers1

0

The first row of the statistics refer to the background:

From the doc

statsv – statistics output for each label, including the background label

You can simply check the number of rows of stats to know if you have some foreground component, or check the number of labels returned by connectedComponentsWithStats.

Miki
  • 40,887
  • 13
  • 123
  • 202