One way to go about it is to take a binary image (pixels with value 0.0 or 1.0) and 'colour' in the black sections (regions): use floodFill
by looping over the whole image and flood filling every black pixel. In the end, you are left with a white outline with a value of 1, and each distinct section filled with a value larger than 1 (a unique ID). It doesn't have to use floats, I just like floats. :)
int section_count = 0;
int fill_colour = 2;
for(int j = 0; j < flt_canny.rows; j++)
{
for(int i = 0; i < flt_canny.cols; i++)
{
float colour = flt_canny.at<float>(j, i);
if(colour == 0)
{
floodFill(flt_canny, Point(i, j), Scalar(fill_colour));
section_count++;
fill_colour++;
}
}
}
// Get number of pixels per section colour
map<float, int> section_sizes;
for(int j = 0; j < flt_canny.rows; j++)
{
for(int i = 0; i < flt_canny.cols; i++)
{
float colour = flt_canny.at<float>(j, i);
if(colour > 1)
section_sizes[colour]++;
}
}
connrctedComponents() might be an idea