2016-11-14 37 views
0

所以我無法理解如何去調整我找到的代碼。基本上我想在圖像中尋找圖像(識別我的IP攝像機拍攝的照片中的某些物體)。我在網上找到了代碼,除了我只想查看圖像上的特定區域外,此代碼可以正常工作。目前整個圖像正在被掃描,我認爲這是不必要的。掃描特定圖像的圖像,限制範圍

的代碼如下:

unsafe 
      { 
       byte* pSmall = (byte*)(void*)HealthbarData.Scan0; 
       byte* pBig = (byte*)(void*)CaptureData.Scan0; 

       int smallOffset = HealthbarStride - HealthbarImage.Width * 3; 
       int bigOffset = CaptureStride - CaptureImage.Width * 3; 

       bool matchFound = true; 

       for (int y = 0; y < CaptureHeight; y++) 
       { 
        for (int x = 0; x < CaptureWidth; x++) 
        { 
         byte* pBigBackup = pBig; 
         byte* pSmallBackup = pSmall; 

         //Look for the small picture. 
         for (int i = 0; i < HealthbarHeight; i++) 
         { 
          int j = 0; 
          matchFound = true; 
          for (j = 0; j < HealthbarWidth; j++) 
          { 
           //With tolerance: pSmall value should be between margins. 
           int inf = pBig[0] - Margin; 
           int sup = pBig[0] + Margin; 
           if (sup < pSmall[0] || inf > pSmall[0]) 
           { 
            matchFound = false; 
            break; 
           } 

           pBig++; 
           pSmall++; 
          } 

          if (!matchFound) 
           break; 

          //We restore the pointers. 
          pSmall = pSmallBackup; 
          pBig = pBigBackup; 

          //Next rows of the small and big pictures. 
          pSmall += HealthbarStride * (1 + i); 
          pBig += CaptureStride * (1 + i); 
         } 

         //If match found, we return. 
         if (matchFound) 
         { 
          EnemyPosition.X = x; 
          EnemyPosition.Y = y; 
          break; 
         } 
         //If no match found, we restore the pointers and continue. 
         else 
         { 
          pBig = pBigBackup; 
          pSmall = pSmallBackup; 
          pBig += 3; 
         } 
        } 

        if (matchFound) 
         break; 

        pBig += bigOffset; 
       } 
      } 

我可以if (matchFound)下檢查,看它是否在允許範圍內,但後來它仍然會掃描整個圖像。

任何人可以給我任何提示或如何做到這一點?比方說,它只檢查圖像中間的300像素。

謝謝。

+0

如果您的捕獲數據存儲爲一個位圖,你可以穿越它之前先裁剪圖像 – jenovachild

回答

0

選項1.裁剪原始圖像

讓我們假設你拍攝的圖像CaptureImageBitmap

在處理之前,您可以裁剪您的CaptureImage

Bitmap CaptureImage = MethodWhereImageDataComesFrom(); 

// you asked for ~300 pixels around the center, you can calculate that rectangle 

int cropWidth = 300; 
int cropHeight = 300; 
int x = CaptureImage.Width/2 - (cropWidth/2); 
int y = CaptureImage.Height/2 - (cropHeight/2); 

// create your rectangle and bitmap of the cropped image bounds here 
Rectangle rect = new Rectangle(x, y, cropWidth, cropHeight); 
Bitmap croppedImage = new Bitmap(rect.Width, rect.Height); 

// grab the graphics component of your cropped image so we can draw pixels from the capture image to it. 
Graphics g = Graphics.FromImage(croppedImage); 

// now draw the cropped pixels from capture 
g.DrawImage(CaptureImage, new Rectangle(0, 0, croppedImage.Width, croppedImage.Height), rect, GraphicsUnit.Pixel); 

現在你可以遍歷croppedImage,它應該只是一個300x300像素的圖像。

請注意,此代碼未經測試,但它應該可以工作。

選項2.圖像循環

指定範圍,您應該可以指定你的形象循環內的寬度和高度範圍。

// define the dimensions we want to search in pixels 
int cropWidth = 300; 
int cropHeight = 300; 

// determine our start positions for x and y (this is the top left corner of the rectangle we're searching in) 
int startWidth = CaptureWidth/2 - (cropWidth/2); // start for x 
int startHeight = CaptureHeight/2 - (cropHeight/2); // start for y 
在你的代碼

現在在哪兒你開始你的循環,你可以簡單地指定以下內容:

for (int y = startHeight; y < startHeight + cropHeight; y++) 
{ 
    for (int x = startWidth; x < startWidth + cropWidth; x++) 
    { 
     // your code here... 
    } 
}