Ask Your Question
0

How to get the number of pixels in an ROI c++?

asked 2019-02-26 21:24:01 -0600

TheBlackViper_Alpha gravatar image

Hello, I have a sample image, my goal is to find the number of red pixels (black if turned to binary) per circle. A sample output like:

Circle 1: 900
Circle 2: 400
Circle 3: 300

Here is the sample image: image description

I have been successful in using the countNonZero function for individually cropped circle and using an area of about 30*30 to get the results. However I want to use the base image and get ROIs for each circle then calculate the pixels. Thanks!

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2019-02-28 07:54:48 -0600

HYPEREGO gravatar image

updated 2019-02-28 08:01:51 -0600

If I've understand the question correctly, you need first to find circles in your images, you can do it using the Hough Circle Transform, here a tutorial in C++

Setting the function with the proper parameters (as I can see, you know the size of the circles, more or less..) you'll obtain the circles center coordinates back from the function:

HoughCircles( src_gray, circles, HOUGH_GRADIENT, 1, src_gray.rows/8, 200, 100, 0, 0 );

circles: A vector that stores sets of 3 values: Xc, Yc, r for each detected circle.

so using that you'll be able to get the proper ROIs for each circle and then apply the function that @ducvd wrote.

The function give to you also the radius, so what you get at the end is the center of the ROI. Is pretty easy to understand that getting the rectangle coordinate is easy. For each circles (ROI), the top left corner of the rectangle will be (Xc-r-thresh, Yc-r-thresh), while the bottom right corner will be (Xc+r+thres, Yc+r+thresh) where Xc and Yc are the circle center gave back from the function, r is the radius( again given back from the function) and thresh is a parameter that you can set to handle the uncertainty regarding the radius of the circle.

Hope that help

edit flag offensive delete link more
1

answered 2019-02-28 03:54:20 -0600

ducvd gravatar image

If you are able to find ROI, the remains problem is counting the Read-pixels inside ROI rectangle.

You could do it by enumerate every pixels in the images then count (please do check whether pixels is inside ROI yourself):

const int channels = I.channels();
count = 0; // count is the number of Red pixels
switch(channels)
{
case 1:
    {
        MatIterator_<uchar> it, end;
        for( it = I.begin<uchar>(), end = I.end<uchar>(); it != end; ++it)
            if (*it == 0) // black
            {
                  count++;
            }
        break;
    }
case 3:
    {
        MatIterator_<Vec3b> it, end;
        for( it = I.begin<Vec3b>(), end = I.end<Vec3b>(); it != end; ++it)
        {
            if ((*it[2])==255) // Red
            {
                   count++;
            }
        }
    }
}
edit flag offensive delete link more

Comments

1

I think that getting the ROI areas is the question. Count it is more than easy. If I have understand correctly, the question is: how can he detect the circles so that he can count the red values, given an arbitrary image as input? So in that case, the algorithm should be able to identify the 6 circles, crop them (getting the ROI area) automatically and then count the red pixels. I think that is what the OP was looking for. In that case, you can take a look at THIS tutorial.

HYPEREGO gravatar imageHYPEREGO ( 2019-02-28 07:48:30 -0600 )edit
1

Thanks you for your idea!

ducvd gravatar imageducvd ( 2019-03-01 02:36:43 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-02-26 21:24:01 -0600

Seen: 988 times

Last updated: Feb 28 '19