gpt4 book ai didi

c# - 如何避免 MinAreaRect 检测到的矩形旋转?

转载 作者:太空宇宙 更新时间:2023-11-03 22:16:31 26 4
gpt4 key购买 nike

我正在尝试检测 Windows 窗体上的文本字段,但 CvInvoke.MinAreaRect(contour) 返回矩形,旋转了 -7.29419661 角度。 enter image description here

我的代码是

        Image<Bgr, Byte> a =
new Image<Bgr, byte>(@"d:/Art/documents/Projects/InputFieldsDetector/Images/Form345_1.PNG");

imageBox1.Image = a;

UMat grayed = new UMat();
CvInvoke.CvtColor(a, grayed, ColorConversion.Bgr2Gray);
imageBox2.Image = grayed;

UMat canny = new UMat();
CvInvoke.Canny(grayed, canny, 50, 200, 3);
imageBox3.Image = canny;

VectorOfVectorOfPoint cnts = new VectorOfVectorOfPoint();

UMat hierarchy = new UMat();
CvInvoke.FindContours(canny, cnts, null, RetrType.Tree, ChainApproxMethod.ChainApproxSimple);

Image<Bgr, Byte> justCountor = a.Clone();

List<string> sizes = new List<string>();

int count = cnts.Size;
for (int i = 0; i < count; i++)
{
VectorOfPoint contour = cnts[i];
var area = CvInvoke.ContourArea(contour);
//if (area > 10000 && area < 15000)
if (area > 200 && area < 300)
{
sizes.Add(area.ToString());

Point[] pts = contour.ToArray();
var forDraw = CvInvoke.MinAreaRect(contour);
// forDraw.Angle = 0;
//forDraw.Center.Y += 10;

justCountor.Draw(forDraw, new Bgr(Color.DarkOrange), 2);
}
}
imageBox4.Image = justCountor;

List<double> result = sizes.Select(x => double.Parse(x)).ToList();

result.Sort();
sizes = result.Select(x => x.ToString()).ToList();

File.WriteAllLines("c:/temp/qqq.txt", sizes);

原图是:

enter image description here

如果我取消注释部分

            forDraw.Angle = 0;
forDraw.Center.Y += 10;

检测到的矩形的大小与字段的大小相似...

请告诉我,为什么返回的矩形会旋转以及如何解决?

最佳答案

您可以在 Canny 输出中看到该算法将阴影解释为边界。解决这个问题的最简单方法是使用接近白色框背景的高值阈值对图像进行预过滤。

Image<Bgr, Byte> a =
new Image<Bgr, byte>(@"d:/Art/documents/Projects/InputFieldsDetector/Images/Form345_1.PNG");

imageBox1.Image = a;

UMat grayed = new UMat();
CvInvoke.CvtColor(a, grayed, ColorConversion.Bgr2Gray);
imageBox2.Image = grayed;

UMat thresholded = new UMat();
CvInvoke.Threshold(grayed, thresholded, 128, 255, ThresholdType.Binary);
imageBox5.Image = thresholded;

UMat canny = new UMat();
CvInvoke.Canny(thresholded, canny, 50, 200, 3);
imageBox3.Image = canny;

VectorOfVectorOfPoint cnts = new VectorOfVectorOfPoint();

UMat hierarchy = new UMat();
CvInvoke.FindContours(canny, cnts, null, RetrType.Tree, ChainApproxMethod.ChainApproxSimple);

Image<Bgr, Byte> justCountor = a.Clone();
List<string> sizes = new List<string>();
int count = cnts.Size;
for (int i = 0; i < count; i++)
{
VectorOfPoint contour = cnts[i];
var area = CvInvoke.ContourArea(contour);
if (area > 200 && area < 300)
{
sizes.Add(area.ToString());
Point[] pts = contour.ToArray();
var forDraw = CvInvoke.MinAreaRect(contour);

// forDraw.Angle = 0;
//forDraw.Center.Y += 10;

if (forDraw.Angle==0)
justCountor.Draw(forDraw, new Bgr(Color.DarkOrange), 2);
}
}
imageBox4.Image = justCountor;

List<double> result = sizes.Select(x => double.Parse(x)).ToList();
result.Sort();
sizes = result.Select(x => x.ToString()).ToList();
File.WriteAllLines("c:/temp/qqq.txt", sizes);

关于c# - 如何避免 MinAreaRect 检测到的矩形旋转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58283786/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com