[英]c# Detect Rectangles in Image
I'm looking to detect and get a array of Rects, one for each rectangle, in the image below. 我想在下面的图像中检测并获取一个Rects数组,每个矩形一个。 How might I do this in c#?
我怎么能在c#中这样做?
Basically I'm trying to scan the image taken of the screen and parse the array of windows. 基本上我正在尝试扫描屏幕拍摄的图像并解析窗口数组。
Rect being some form of (xloc,yloc,xsize,ysize) Returned array: rectangles = ParseRects(image); Rect是某种形式的(xloc,yloc,xsize,ysize)返回的数组:rectangles = ParseRects(image);
Your best option will be to use the AForge.Net library . 您最好的选择是使用AForge.Net库 。
The following code is derived from the documentation for the ShapeChecker
class, and you may want to look at the documentation to further familiarize yourself. 以下代码派生自
ShapeChecker
类的文档,您可能需要查看文档以进一步熟悉自己。
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");
}
}
}
}
The original input: 原始输入:
The resultant image: 结果图像:
You can see the line Detection by Hough transformation and Corner detection. 您可以看到Hough变换检测线和角点检测。 Some algorithms have probably already been implemented in C#
有些算法可能已经在C#中实现了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.