user3049356
user3049356

Reputation: 47

access pixel values inside the detected object / c++

If I could detect a circle by using canny edge detector, How can I have access to all values which are inside the circle?

void Canny(InputArray image, OutputArray edges, double threshold1, double threshold2, int apertureSize=3, bool L2gradient=false ) 

output of this function will give me the edge values which is detected by the edge detector, but what I want is all values inside the circle.

thanks in advance

------after editing.......

     Mat mask = Mat::zeros(canny_edge.rows, canny_edge.cols, CV_8UC1);
     Mat crop(main.rows, main.cols, CV_32FC1);
     main.copyTo( crop, mask );

     for(unsigned int y=0; y< height; y++)
          for(unsigned int x=0; x< width; x++)
              if(mask.at<unsigned char>(y,x) > 0)
               {

               }

Upvotes: 1

Views: 3080

Answers (2)

Micka
Micka

Reputation: 20160

For a circle, as asked in the original question:

first you want to detect the circle (e.g. by using hough circle detection methods). If you've done that you have some kind of circle center and a radius. Have a look at http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/hough_circle/hough_circle.html

After that you have to test whether a pixel is inside the circle. So one idea (and with openCV quite fast) is to draw a filled circle on a mask image and test for each pixel in the original image, whether the mask pixel at the same image coordinates is set (then the pixel is inside the object). This works for any other drawable object, draw it (filled) on the mask and test mask values.

Assuming you have a circle center and a radius, and the size of your original image is image_height x image_width, try this:

cv::Mat mask = cv::Mat::zeros(image_height,image_width, CV_8U);
cv::circle(mask, center, radius, cv::Scalar(255), -1);
for(unsigned int y=0; y<image_height; ++y)
  for(unsigned int x=0; x<image_width; ++x)
    if(mask.at<unsigned char>(y,x) > 0)
    {
      //pixel (x,y) in original image is within that circle so do whatever you want.
    }

though it will be more efficient if you limit the mask region (circle center +/- radius in both dimensions) instead of looping over the whole image ;)

Upvotes: 2

Bull
Bull

Reputation: 11951

For circles you should use the Hough Circle Transform. From it you will get the centres and radii of circles in your image. A given pixel is inside a particular circle if its distance from the center is less than the radius of the circle.

For a general shape, use findCountours to get the outline of the shape, then you can usepointPolygonTest to determine wheether points are inside that shape. There is a tutorialon it.

Upvotes: 1

Related Questions