c# - 如何找到箭头的方向?
问题描述
嗨,大家好,
我试图弄清楚如何获得图片中间箭头的方向。
如果箭头指向北方,则方向为0,如果箭头指向南方,则方向为pi,第一张图片为1.75pi,第二张图片为0.5pi,第三张图片为0.75pi。
我希望方向尽可能准确。
我试图通过 OpenCV 库,但未能找到一种方法来做到这一点。
你能帮忙吗?
更新:我可以找到箭头在哪里。
解决方案
您将需要执行一些步骤。首先,像您一样识别箭头的边界框。其次,获取箭头像素的坐标,在这里我发现它们的红色、绿色和蓝色值超过 80,您可能需要为您的图像检查这一点。第三,获取箭头区域的中心。最后得到箭头的面积Ix
、Iy
、 和的属性Ixy
,可以用来得到箭头的旋转角度。
你需要对这个角度做简单的修改,如果它是一个负值,加上 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。
推荐阅读
- java - REST API 客户端实现,最佳实践是什么?
- android - 启动 AVD 时 Ubuntu 18.04.5 LTS 系统冻结
- oracle - oracle的where子句中没有硬编码
- reactjs - React Formik - 在输入字段中按 Enter 键后应用程序执行不需要的请求(提交)
- python-3.x - .items() 循环与 if 的组合
- c# - 从 C# Visualstudio 中的数据表中获取一行,然后将该行存储在数据行中
- python - 拆分值并在熊猫数据框中使用拆分值重命名列
- c++ - QPdfDocument/QPdfView:如何显示动画 PDF?
- sql-server - 在同一列中排序 Asc 和 desc
- javascript - window.dataLayer.push 在本地机器上工作,但不在生产环境中