OPENCV ::查找两个同心圆之间的所有点

时间:2018-07-12 12:17:45

标签: c++ linux opencv histogram

我有两个同心圆a和b,我从图像中检测到这些圆,我知道了它们的半径。我想要的是两个同心圆之间的点,以便可以从这些点中提取RGB值。任何帮助都会有所帮助。

3 个答案:

答案 0 :(得分:2)

两个同心圆(中心(x, y),半径x_0, y_0r_0, r_1之间的点的索引(r_1 > r_0)必须满足以下条件:

(x-x_0) * (x-x_0) + (y-y_0) * (y-y_0) >= r_0 * r_0
(x-x_0) * (x-x_0) + (y-y_0) * (y-y_0) <= r_1 * r_1

因此,当遍历图像的所有点时,您可以确定要处理的点:

for (int x=0; x<img.rows; x++)
{
   for (int y=0; y<img.cols; y++)
   {
       double dd = (x-x_0) * (x-x_0) + (y-y_0) * (y-y_0);
       if (dd < r_0 * r_0 || dd > r_1 * r_1)
         continue;

       // Do what you have to do with the points between the two circles
   }
}

答案 1 :(得分:2)

伪代码:

Make a Mat of the same size as your source image. 
Draw the two filled circles with the built-in OpenCV circle drawing routine. 
  (large circle in white, then the small in black) 
Use that as a mask, that you multiply on your source image. 
(Depending on which pixels you wish to keep, the mask may have to be inverted.)

答案 2 :(得分:0)

如果您需要访问所有像素并以某种方式收集它们的颜色值,并且由于速度至关重要,您只想访问有问题的像素,我建议您研究midpoint circle algorithm authored by Bresenham。您可以对此进行专门化,以便每像素线仅获得2或4个点,并遍历每条线的成对点。应该是O(n),其中n是以像素为单位的高度(直径,即最大半径),因此您将看到o(m),m是两个像素之间的像素数,而不是n ^ 2。如果图像和半径彼此靠近,那将很重要。

enter image description here