首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用c#在emgucv中获取图像角度的NaN

用c#在emgucv中获取图像角度的NaN
EN

Stack Overflow用户
提问于 2020-06-20 01:04:56
回答 1查看 109关注 0票数 0

我已经参考了下面的链接来开发下面的csharp代码来检测图像的角度。https://stackoverflow.com/a/34285205/7805023

代码语言:javascript
运行
复制
 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作为角度的输出。请通过代码让我知道我做错了什么?

我使用emgucv进行图像处理。

任何帮助都将受到高度的感谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-20 01:58:26

当提供的值不在-11之间时,Math.Acos()将返回NaN,而不是数字。

您的thetaRads计算错误,您需要计算向量的点积除以向量的幅值的乘积的Acos,如下所示:

代码语言:javascript
运行
复制
double thetaRads = Math.Acos(((primaryEdge.X * reference.X) + (primaryEdge.Y * reference.Y)) / (primaryMagnitude * refMagnitude));
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62475253

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档