C#检测矩形在图像 [英] c# Detect Rectangles in Image
问题描述
我在找检测,并得到一个阵列Rects中,每个矩形,下面的图像中。我怎么可能会做这在C#?
I'm looking to detect and get a array of Rects, one for each rectangle, in the image below. How might I do this in c#?
基本上我尝试扫描拍摄的画面的图像和分析窗口的数组。
Basically I'm trying to scan the image taken of the screen and parse the array of windows.
矩形是某种形式(XLOC,yloc,xsize,ysize)
返回数组中:矩形= ParseRects(图像);
Rect being some form of (xloc,yloc,xsize,ysize) Returned array: rectangles = ParseRects(image);
推荐答案
您最好的选择将是使用 AForge.Net库。
Your best option will be to use the AForge.Net library.
下面的代码是从派生为 ShapeChecker
类,你可能想看看文件,以进一步熟悉。
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.
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:
这篇关于C#检测矩形在图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!