gpt4 book ai didi

c# - 检测图像 AForge 中的小矩形

转载 作者:行者123 更新时间:2023-11-30 13:38:07 24 4
gpt4 key购买 nike

我正在尝试检测这张图片上的矩形:

enter image description here

使用此代码:

static void Main(string[] args)
{
// Open your image
string path = "test.png";
Bitmap image = (Bitmap)Bitmap.FromFile(path);

// locating objects
BlobCounter blobCounter = new BlobCounter();

blobCounter.FilterBlobs = true;
blobCounter.MinHeight = 5;
blobCounter.MinWidth = 5;

blobCounter.ProcessImage(image);
Blob[] blobs = blobCounter.GetObjectsInformation();

// check for rectangles
SimpleShapeChecker shapeChecker = new SimpleShapeChecker();

foreach (var blob in blobs)
{
List<IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blob);
List<IntPoint> cornerPoints;

// use the shape checker to extract the corner points
if (shapeChecker.IsQuadrilateral(edgePoints, out cornerPoints))
{
// only do things if the corners form a rectangle
if (shapeChecker.CheckPolygonSubType(cornerPoints) == PolygonSubType.Rectangle)
{
// here i use the graphics class to draw an overlay, but you
// could also just use the cornerPoints list to calculate your
// x, y, width, height values.
List<Point> Points = new List<Point>();
foreach (var point in cornerPoints)
{
Points.Add(new Point(point.X, point.Y));
}

Graphics g = Graphics.FromImage(image);
g.DrawPolygon(new Pen(Color.Red, 5.0f), Points.ToArray());

image.Save("result.png");
}
}
}
}

但它不识别矩形(墙)。它只识别大正方形,当我减小 minHeight 和 minWidth 时,它识别文字上的梯形..

最佳答案

我提出了一种不同的算法方法,在使用图像处理算法将近一年之后,我可以告诉你的是,要创建一个高效的算法,你必须“反射(reflect)”你作为一个人会怎么做,这里是建议的方法:

  1. 我们并不真正关心纹理,我们关心边缘(矩形是边缘),因此我们将应用边缘检测>差异(http://www.aforgenet.com/framework/docs/html/d0eb5827-33e6-c8bb-8a62-d6dd3634b0c9.htm),这给我们:enter image description here

  2. 我们想夸大墙壁,因为人类知道我们正在寻找墙壁,但计算机不知道这一点,因此应用两轮形态学>扩张 ( http://www.aforgenet.com/framework/docs/html/88f713d4-a469-30d2-dc57-5ceb33210723.htm ),这给出我们:enter image description here

  3. 我们只关心什么是墙什么不是,应用二值化>阈值 ( http://www.aforgenet.com/framework/docs/html/503a43b9-d98b-a19f-b74e-44767916ad65.htm ),我们得到: enter image description here

  4. (可选)我们可以应用 blob 提取来删除标签(“QUARTO、BANHEIRO”等)

  5. 我们应用 Color>Invert,这是刚刚完成的,因为下一步检测的是白色而不是黑色。

  6. Apply a Blob>Processing>Connected Components Labeling ( http://www.aforgenet.com/framework/docs/html/240525ea-c114-8b0a-f294-508aae3e95eb.htm ),这将为我们提供所有矩形,如下所示:enter image description here

请注意,对于每个彩色框,您都有其坐标、中心、宽度和高度。因此,您可以使用该坐标从真实图像中提取片段。

PS:强烈建议使用 AForge Image Processing Lab 程序来测试您的算法。

关于c# - 检测图像 AForge 中的小矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18872415/

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