2012-06-05 16 views
5

我試圖檢查,如果零件圖像存在於另一圖片Vb.Net檢查,如果圖片存在於另一圖片

解釋:


完整的圖像:
http://imageshack.us/photo/my-images/526/part1g.png/

第二部分我想要的圖片檢查他是否存在於完整圖片
http://imageshack.us/photo/my-images/706/part2p.png/

如果第二部分是存在,那麼該函數返回true
存在,如果它存在,可以檢查功能?


(如果它只是一個像素那麼它是verey容易,但我要檢查,如果局部圖像存在於另一圖片)
有一個可行的代碼,但它檢查,如果單像素存在圖像

Dim bmp As Bitmap = PictureBox1.Image 
    For x As Integer = 0 To bmp.Width - 1 
     For y As Integer = 0 To bmp.Height - 1 
      If bmp.GetPixel(x, y) = Color.FromArgb(48, 48, 48) Then 
       msgbox("Pixel Exist In Image!!!") 
      End If 
     Next 
    Next 
+0

有人??我應該將所有圖像轉換爲數組,然後「instr」 –

+0

我想知道steghide是否會完成這項工作(或類似的任何工作)?它用於查找圖像中是否包含某些內容。它可以將某些東西添加到圖像中。它的命令相對容易使用。在這種情況下,第二個圖像不會隱藏在第一個圖像中,但值得嘗試。 我會試試你今晚給我們的照片。會通知你。我希望這會起作用。它應該節省大量的工作。 – Minus

+0

它根本沒有工作(第一張圖片沒有被識別的格式,第二張圖片無法比較2張圖片,但是如果有東西需要提取,請從圖片中創建一些東西) – Minus

回答

0

你可以只通過第二圖像的每一個可能的左上角像素寫入循環功能,然後將像素複製到另一個位圖對象,然後將新的位圖對象進行比較,以你的第二個像素的圖像像素。

所以,首先你將通過像素環路在

  • X < mainImageWidth - subImageWidth
  • Ÿ< mainImageHeight - subImageHeight

如果在(X,Y)的主要圖像中的像素與子圖像中(0,0)處的像素具有相同的顏色值,使用與您的主圖像中的子圖像相同的尺寸從(x,y)開始將區域複製到新的位圖對象像這樣的功能 - http://msdn.microsoft.com/en-us/library/aa457087.aspx

然後只是循環遍歷新對象和子圖像中的像素,比較相同座標下的顏色。如果遇到不同,請打破循環。如果你到了這個循環的結尾,你有一個匹配,並且可以返回True,否則繼續循環遍歷主圖像的像素,直到你達到子圖像不可能再適合的點,然後返回False 。

3

我寫了這個擴展名在圖像中找到圖像。它有一些限制,但是:1)圖像必須保存,沒有色彩空間(反正也是一樣的),2)它不適用於有損壓縮的圖像(即.jpeg,因爲你需要平均和容忍實現)。

我已經優化了像素匹配程序。它沒有完全調試,但似乎按預期工作。它忽略了alpha通道(目的是根據需要進行擴展),並且需要嘗試捕獲調用者(即內存不足的異常)。

用法:

Dim p As Point = yourBitmap.Contains(bmpYouLookFor) 
If p <> Nothing Then 
'... 
End If 

代碼:如果你不想把它作爲一個擴展(需要.NET 3.5+)只是刪除擴展屬性,並把它作爲一個普通的功能而不是源位圖作爲參數。

複製並粘貼以下到一個模塊(許可證:CC-歸屬):

'******************************************************************************* 
'* 
'*  Epistemex 
'* 
'*  Bitmap extension: .Contains(bmp) 
'*  KF 
'* 
'*  2012-09-26  Initial version 
'*  2012-09-26  Minor optimization, exit for's impl. 
'* 
'******************************************************************************* 

Imports System.Drawing 
Imports System.Runtime.CompilerServices 
Imports System.Drawing.Imaging 
Imports System.Runtime.InteropServices 

Module BitmapExtension 

    <Extension()> 
    Public Function Contains(src As Bitmap, ByRef bmp As Bitmap) As Point 
     ' 
     '-- Some logic pre-checks 
     ' 
     If src Is Nothing OrElse bmp Is Nothing Then Return Nothing 

     If src.Width = bmp.Width AndAlso src.Height = bmp.Height Then 
      If src.GetPixel(0, 0) = bmp.GetPixel(0, 0) Then 
       Return New Point(0, 0) 
      Else 
       Return Nothing 
      End If 

     ElseIf src.Width < bmp.Width OrElse src.Height < bmp.Height Then 
      Return Nothing 

     End If 
     ' 
     '-- Prepare optimizations 
     ' 
     Dim sr As New Rectangle(0, 0, src.Width, src.Height) 
     Dim br As New Rectangle(0, 0, bmp.Width, bmp.Height) 

     Dim srcLock As BitmapData = src.LockBits(sr, Imaging.ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb) 
     Dim bmpLock As BitmapData = bmp.LockBits(br, Imaging.ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb) 

     Dim sStride As Integer = srcLock.Stride 
     Dim bStride As Integer = bmpLock.Stride 

     Dim srcSz As Integer = sStride * src.Height 
     Dim bmpSz As Integer = bStride * bmp.Height 

     Dim srcBuff(srcSz) As Byte 
     Dim bmpBuff(bmpSz) As Byte 

     Marshal.Copy(srcLock.Scan0, srcBuff, 0, srcSz) 
     Marshal.Copy(bmpLock.Scan0, bmpBuff, 0, bmpSz) 

     ' we don't need to lock the image anymore as we have a local copy 
     bmp.UnlockBits(bmpLock) 
     src.UnlockBits(srcLock) 

     Dim x, y, x2, y2, sx, sy, bx, by, sw, sh, bw, bh As Integer 
     Dim r, g, b As Byte 

     Dim p As Point = Nothing 

     bw = bmp.Width 
     bh = bmp.Height 

     sw = src.Width - bw  ' limit scan to only what we need. the extra corner 
     sh = src.Height - bh  ' point we need is taken care of in the loop itself. 

     bx = 0 : by = 0 
     ' 
     '-- Scan source for bitmap 
     ' 
     For y = 0 To sh 
      sy = y * sStride 
      For x = 0 To sw 

       sx = sy + x * 3 
       ' 
       '-- Find start point/pixel 
       ' 
       r = srcBuff(sx + 2) 
       g = srcBuff(sx + 1) 
       b = srcBuff(sx) 

       If r = bmpBuff(2) AndAlso g = bmpBuff(1) AndAlso b = bmpBuff(0) Then 
        p = New Point(x, y) 
        ' 
        '-- We have a pixel match, check the region 
        ' 
        For y2 = 0 To bh - 1 
         by = y2 * bStride 
         For x2 = 0 To bw - 1 
          bx = by + x2 * 3 

          sy = (y + y2) * sStride 
          sx = sy + (x + x2) * 3 

          r = srcBuff(sx + 2) 
          g = srcBuff(sx + 1) 
          b = srcBuff(sx) 

          If Not (r = bmpBuff(bx + 2) AndAlso 
            g = bmpBuff(bx + 1) AndAlso 
            b = bmpBuff(bx)) Then 
           ' 
           '-- Not matching, continue checking 
           ' 
           p = Nothing 
           sy = y * sStride 
           Exit For 
          End If 

         Next 
         If p = Nothing Then Exit For 
        Next 
       End If 'end of region check 

       If p <> Nothing Then Exit For 
      Next 
      If p <> Nothing Then Exit For 
     Next 

     bmpBuff = Nothing 
     srcBuff = Nothing 

     Return p 

    End Function 

End Module 
+1

使用它作爲一個普通的功能,它的作品就像魅力!此外,它是我的機器上令人驚訝的快速:) –

相關問題