如果我可以使用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)
{
}
答案 0 :(得分:2)
对于一个圆圈,如原始问题中所述:
首先要检测圆圈(例如,使用霍夫圆检测方法)。如果你已经这样做了,你有一些圆心和半径。看看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.
}
虽然如果限制遮罩区域(两个维度中的圆心+/-半径)而不是在整个图像上循环,效率会更高;)
答案 1 :(得分:1)
对于圈子,您应该使用Hough Circle Transform。通过它,您将获得图像中的圆心和半径。如果给定像素距离中心的距离小于圆的半径,则该特定像素位于特定圆内。
对于一般形状,使用findCountours 获取形状的轮廓,然后您可以使用pointPolygonTest来确定该形状内的小点。它上面有一个tutorial。