c#检测图像中的矩形

前端之家收集整理的这篇文章主要介绍了c#检测图像中的矩形前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在下面的图像中检测并获取一个矩形数组,每个矩形一个.我如何在c#中执行此操作?

基本上我试图扫描屏幕上拍摄的图像并解析窗口数组.

Rect是某种形式的(xloc,yloc,xsize,ysize)
返回数组:rectangles = ParseRects(image);

解决方法

你最好的选择是使用 AForge.Net library.

以下代码派生自ShapeChecker类的文档,您可能需要查看文档以进一步了解您自己.

  1. static void Main(string[] args)
  2. {
  3. // Open your image
  4. string path = "test.png";
  5. Bitmap image = (Bitmap)Bitmap.FromFile(path);
  6.  
  7. // locating objects
  8. BlobCounter blobCounter = new BlobCounter();
  9.  
  10. blobCounter.FilterBlobs = true;
  11. blobCounter.MinHeight = 5;
  12. blobCounter.MinWidth = 5;
  13.  
  14. blobCounter.ProcessImage(image);
  15. Blob[] blobs = blobCounter.GetObjectsInformation();
  16.  
  17. // check for rectangles
  18. SimpleShapeChecker shapeChecker = new SimpleShapeChecker();
  19.  
  20. foreach (var blob in blobs)
  21. {
  22. List<IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blob);
  23. List<IntPoint> cornerPoints;
  24.  
  25. // use the shape checker to extract the corner points
  26. if (shapeChecker.IsQuadrilateral(edgePoints,out cornerPoints))
  27. {
  28. // only do things if the corners form a rectangle
  29. if (shapeChecker.CheckPolygonSubType(cornerPoints) == PolygonSubType.Rectangle)
  30. {
  31. // here i use the graphics class to draw an overlay,but you
  32. // could also just use the cornerPoints list to calculate your
  33. // x,y,width,height values.
  34. List<Point> Points = new List<Point>();
  35. foreach (var point in cornerPoints)
  36. {
  37. Points.Add(new Point(point.X,point.Y));
  38. }
  39.  
  40. Graphics g = Graphics.FromImage(image);
  41. g.DrawPolygon(new Pen(Color.Red,5.0f),Points.ToArray());
  42.  
  43. image.Save("result.png");
  44. }
  45. }
  46. }
  47. }

原来的输入:

得到的图像:

猜你在找的C#相关文章