我想在下面的图像中检测并获取一个矩形数组,每个矩形一个.我如何在c#中执行此操作?
基本上我试图扫描屏幕上拍摄的图像并解析窗口数组.
Rect是某种形式的(xloc,yloc,xsize,ysize)
返回数组:rectangles = ParseRects(image);
解决方法
你最好的选择是使用
AForge.Net library.
以下代码派生自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");
- }
- }
- }
- }
原来的输入:
得到的图像: