【发布时间】:2020-10-14 10:40:45
【问题描述】:
我有三个泛卡图像,用于使用 emgucv 和 c# 测试图像的倾斜。
顶部的第一张图像检测到 180 度正常工作。
中间的第二张图像检测到 90 度应检测为 180 度。
检测到 180 度的第三张图像应检测为 90 度。
我想在这里分享的一个观察结果是,当我使用画笔从平移卡的上下侧裁剪不需要的图像部分时,它使用下面提到的代码给了我预期的结果。
现在我想了解如何使用编程删除不需要的部分。 我玩过轮廓和投资回报率,但我无法弄清楚如何适应它们。我无法理解 emgucv 本身是选择轮廓还是我必须做些什么。
请推荐任何合适的代码示例。
请检查下面的代码以进行角度检测,请帮助我。提前致谢。
imgInput = new Image<Bgr, byte>(impath);
Image<Gray, Byte> img2 = imgInput.Convert<Gray, Byte>();
Bitmap imgs;
Image<Gray, byte> imgout = imgInput.Convert<Gray, byte>().Not().ThresholdBinary(new Gray(50), new Gray(125));
VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
Emgu.CV.Mat hier = new Emgu.CV.Mat();
var blurredImage = imgInput.SmoothGaussian(5, 5, 0 , 0);
CvInvoke.AdaptiveThreshold(imgout, imgout, 255, Emgu.CV.CvEnum.AdaptiveThresholdType.GaussianC, Emgu.CV.CvEnum.ThresholdType.Binary, 5, 45);
CvInvoke.FindContours(imgout, contours, hier, Emgu.CV.CvEnum.RetrType.External, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple);
if (contours.Size >= 1)
{
for (int i = 0; i <= contours.Size; 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 r = edge1.X + edge1.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;
double primaryMagnitude = edge1Magnitude > edge2Magnitude ? edge1Magnitude : edge2Magnitude;
PointF reference = new PointF(1, 0);
double refMagnitude = 1;
double thetaRads = Math.Acos(((primaryEdge.X * reference.X) + (primaryEdge.Y * reference.Y)) / (primaryMagnitude * refMagnitude));
double thetaDeg = thetaRads * 180 / Math.PI;
imgInput = imgInput.Rotate(thetaDeg, new Bgr());
imgout = imgout.Rotate(box.Angle, new Gray());
Bitmap bmp = imgout.Bitmap;
break;
}
}
【问题讨论】:
-
您是否测试过您检测到的唯一轮廓是否是卡的外部?
-
另外,
RotatedRect有一个属性,Angle,所以如果你使用它,所有其他代码都是多余的。 emgu.com/wiki/files/3.0.0/document/html/… -
在当前代码之前,我测试了 RotatedRect 的角度检测,但它没有给出预期的结果。
-
轮廓没有画在卡片外面,而是画在图像的边界上。
-
有什么建议吗?