c# - 使用C#在Emgucv中获取图像角度的NaN

标签 c# .net opencv ocr emgucv

我引用了以下链接来开发以下csharp代码以检测Image的 Angular 。 https://stackoverflow.com/a/34285205/7805023

 Image<Gray, byte> imgout = imgInput.Convert<Gray, byte>().Not().ThresholdBinary(new 
 Gray(50), new Gray(255));
 VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
 Mat hier = new Mat();

CvInvoke.FindContours(imgout, contours, hier, Emgu.CV.CvEnum.RetrType.External, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple);
     for (int i = 0; i <= 1; i++)
     {
        Rectangle rect = CvInvoke.BoundingRectangle(contours[i]);
        RotatedRect box = CvInvoke.MinAreaRect(contours[i]);
        PointF[] Vertices = box.GetVertices();
        PointF point = box.Center;
        PointF edge1 = new PointF(Vertices[1].X - Vertices[0].X, 
        Vertices[1].Y - Vertices[0].Y);
        PointF edge2 = new PointF(Vertices[2].X - Vertices[1].X,Vertices[2].Y - Vertices[1].Y);                     
        double edge1Magnitude = Math.Sqrt(Math.Pow(edge1.X, 2) + Math.Pow(edge1.Y, 2));
        double edge2Magnitude = Math.Sqrt(Math.Pow(edge2.X, 2) + Math.Pow(edge2.Y, 2));
        PointF primaryEdge = edge1Magnitude > edge2Magnitude ? edge1 : edge2;
        PointF reference = new PointF(Vertices[1].X, Vertices[0].Y);
        double thetaRads = Math.Acos((primaryEdge.X * reference.X) + (primaryEdge.Y * reference.Y))/(edge1Magnitude* edge2Magnitude);
        double thetaDeg = thetaRads * 180 / Math.PI;
    }

我得到NaN作为 Angular 输出。请检查代码,让我知道我做错了什么?

我已将emgucv用于图像处理。

任何帮助将不胜感激。

最佳答案

当提供的值不在NaNMath.Acos()之间时,-1返回1(不是数字)。

您对thetaRads的计算是错误的,您需要按照以下公式计算 vector 的点积的Acos除以 vector 的幅度的乘积:

enter image description here

double thetaRads = Math.Acos(((primaryEdge.X * reference.X) + (primaryEdge.Y * reference.Y)) / (primaryMagnitude * refMagnitude));

关于c# - 使用C#在Emgucv中获取图像角度的NaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62475253/

相关文章:

c# - 为什么 .Net 词典看起来像是经过排序的?

python - 如何不通过对话框直接保存图片?

python - 使用opencv检测矩形

c# - 异步方法中未显示忙碌指示符

c# - 从客户端检测到具有潜在危险的 Request.Form 值

c# - 从 List<T> 更新 XML 文件

c# - 通过 Win + M windows 快捷方式最小化无边框窗体

c# - C# 类实现中接口(interface)变量的用途

.net - LDAP 查询返回组中的所有用户

c++ - 使用 OpenCV 的目标检测项目