2011-05-10 78 views
10

我正在尋找檢測並獲取下圖中每個矩形的一個Rects數組。我怎麼可以在c#中做到這一點?c#檢測圖像中的矩形

基本上我試圖掃描屏幕拍攝的圖像並解析窗口數組。 (xloc,yloc,xsize,ysize) 返回數組:rectangles = ParseRects(image);返回數組:

Image

+0

你有沒有好的在線前夕截圖) – 2016-11-10 14:10:13

+0

@PicMickael)。 – 2016-11-11 03:03:46

回答

15

你最好的選擇將是使用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"); 
      } 
     } 
    } 
} 

原始輸入: original input

得到的圖像:enter image description here

+0

嘿!我真的設法得到幾乎這個結果,使用相同的程序。我接受你的答案,但如果你願意,我想獲得更多的信息,你是否認爲濾除了所有的顏色,但窗口的範圍會更好?我需要在它後面引入一個隨機背景,並且它仍然只能識別窗口。你怎麼看? – 2011-05-10 18:04:11

+3

@Christian我會非常誠實的:隨機背景會很難。你可以在我的答案中使用blob檢測,但背景扣除本身就是一門科學。我猜這個隨機背景是動畫的,所以我需要兩個幀,並將它們異或以獲得要使用的靜態圖像。 – 2011-05-11 00:20:57

+0

@GregBuehler:BlobCounter類是從一些Dll?如果是這樣,請分享dll的位置..謝謝。 – 2013-11-27 13:51:29