【问题标题】:How to find the orientation of the arrow?如何找到箭头的方向?
【发布时间】:2021-08-16 06:07:13
【问题描述】:

大家好,

我正在试图弄清楚如何获得图片中间箭头的方向。

如果箭头指向北方,方向为0,如果箭头指向南方,方向为pi,第一张图片为1.75 pi 第二张图片是 0.5 pi,第三张图片是 0.75 pi。

我希望方向尽可能准确。

我尝试过浏览 OpenCV 库,但没有找到方法。

你能帮忙吗?

更新:我可以找到箭头所在的位置。

【问题讨论】:

  • 你能检测到箭头吗?
  • 是的,我可以检测到箭头。只是不知道如何找出它旋转了多少。
  • 如果你能准确地检测出它的像素坐标,也许通过它们的颜色,我可以帮你确定方向。
  • 正确。有道理。我会试一试的。
  • 如果您能够检测/分割箭头的黑色圆圈和箭头的明亮区域,则方向为 TB,其中 T 是与黑色圆圈距离最大的亮点,B 是黑色圆圈中心。

标签: c# opencv image-processing


【解决方案1】:

您需要执行一些步骤。首先,像您一样识别箭头的边界框。其次,获取箭头像素的坐标,在这里我发现它们的红色、绿色和蓝色值都超过了 80,您可能需要为您的图像检查这一点。第三,获取箭头区域的中心。最后得到箭头IxIyIxy的面积属性,可以得到箭头的旋转角度。
你需要对这个角度做简单的修改,如果它是一个负值,加上 Pi,你还需要从它的中心得到最远的箭头点,如果它正好从质心,角度应该是大于 Pi,否则应该小于它。

int i, x, y, t;
double xc, yc, Ix, Iy, Ixy, xf, yf, d, ang;
Bitmap img = new Bitmap(path);   // load only the area that contains the arrow
List<int> px, py;
px = new List<int>();
py = new List<int>();
xc = yc = Ix = Iy = Ixy = xf = yf = d = t = 0;
for (x = 0; x < img.Width; x++)
    for (y = 0; y < img.Height; y++)
        if (img.GetPixel(x, y).R > 80 && img.GetPixel(x, y).G > 80 && img.GetPixel(x, y).B > 80)  // you will have to check this condition for your images
        {
            t++;        // get the number of pixels of arrow
            xc += x;
            yc += y;    
            px.Add(x);  // store x-coordinates of all arrow pixels
            py.Add(y);  // store y-coordinates of all arrow pixels
        }
// get the center of area of the arrow
xc /= t;
yc /= t;
// calculate the properties of area
for (i = 0; i < t; i++)
{
    if (Math.Pow(px[i] - xc, 2) + Math.Pow(py[i] - yc, 2) > d)
    {
        xf = px[i] - xc;
        yf = py[i] - yc;
        d = Math.Pow(xf, 2) + Math.Pow(yf, 2);
    }
    Ix += Math.Pow(py[i] - yc, 2);
    Iy += Math.Pow(px[i] - xc, 2);
    Ixy += (px[i] - xc) * (py[i] - yc);
}
//  calculate the angel
ang = Math.Atan2(-2 * Ixy, Ix - Iy) / 2;
//  correct the angle
if (ang < 0)
    ang += Math.PI;
if (xf > 0 && ang < Math.PI)
    ang += Math.PI;
if (xf < 0 && ang > Math.PI)
    ang -= Math.PI;

我在你的图片上试过这个,第一张是 1.769 Pi,第二张是 0.578 Pi,第三张是 0.832 Pi。

【讨论】:

  • 很棒的会尝试一下..我昨晚太困了:p
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-02
  • 2012-08-04
相关资源
最近更新 更多