访问检测到的对象/c++内部的像素值

access pixel values inside the detected object / c++

本文关键字:内部 像素 c++ 对象 检测 访问      更新时间:2023-10-16

如果我可以通过使用canny边缘检测器来检测圆,我如何访问圆内的所有值?

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

这个函数的输出会给我边缘检测器检测到的边缘值,但我想要的是圆内的所有值。

提前感谢

------编辑后。。。。。。。

     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)
               {
               }

对于圆形,如原始问题中所问:

首先要检测圆(例如,使用hough圆检测方法)。如果你已经这样做了,你就有了某种圆心和半径。看看http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/hough_circle/hough_circle.html

之后,你必须测试一个像素是否在圆圈内。因此,一个想法(使用openCV非常快)是在遮罩图像上绘制一个实心圆,并测试原始图像中的每个像素,是否设置了相同图像坐标的遮罩像素(然后像素在对象内部)。这适用于任何其他可绘制对象,在遮罩上绘制(填充)对象并测试遮罩值。

假设你有一个圆圈center和一个radius,并且你的原始图像的大小是image_height x image_width,那么试试这个:

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.
    }

不过,如果限制遮罩区域(两个维度中的圆心+/-半径)而不是在整个图像上循环,则效率会更高;)

对于圆,应该使用霍夫圆变换。从中可以得到图像中的圆心和半径。如果给定像素与圆心的距离小于圆的半径,则该像素位于特定圆内。

对于一般形状,使用findCountours获取形状的轮廓,然后可以使用pointPolygonTest来确定该形状内的点。有一个tutorialon。