Vb.Net检查一张图片是否存在于另一张图片中。

5

我试图检查图片的一部分是否存在于另一张图片中。

解释:


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

我想要检查的第二部分图片,是否存在于完整的图片中:
http://imageshack.us/photo/my-images/706/part2p.png/

如果第二部分存在,则此函数返回true。
是否有一个函数可以检查它是否存在?


(如果只是单个像素,那就很容易了,但我想要检查图片的一部分是否存在于另一张图片中)
有一个可以工作的代码,但它只能检查单个像素是否存在于图片中

    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

有人吗?我需要将所有图像转换为数组,然后使用"instr"函数。 - roger janety
我想知道steghide是否可以完成这个任务(或类似的东西)?它用于查找图像中是否包含某些内容。并且它可以将一些东西添加到图像中。它是基于命令的,所以相对容易使用。在这种情况下,第二张图像没有隐藏在第一张图像中,但值得尝试。我会在今晚用你给我们的图片尝试一些东西。会及时告诉你结果。希望这会成功。如果成功了,应该可以省去很多工作。 - Minus
完全没有起作用(首先图像不是被识别的格式,其次它无法比较两个图像,但如果有东西可以提取,则从一个图像中创建一些内容)。 - Minus
如果你找到了其他可行的解决方案,我很乐意听取。谢谢你关于一切的支持。 - roger janety
2个回答

3
我编写了这个扩展程序来查找图像中的图像。但它有一些限制:1)图像必须没有颜色空间(或者至少是相同的颜色空间),2)它不能用于有损压缩的图像(例如JPEG格式),对于这种情况,您需要实现平均值和容差。

我已经优化了像素匹配例程。虽然它还没有完全调试,但似乎可以按预期工作。它忽略了alpha通道(故意这样做,根据需要进行扩展),并且您需要在调用方上使用try-catch(例如,内存不足异常)。

用法:

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
将其用作普通函数,它的效果非常好!而且在我的机器上速度惊人快速 :) - Niels van Reijmersdal

0
你可以编写一个函数来循环遍历第二个图像的每个可能的左上角像素,然后将像素复制到另一个位图对象中,然后逐像素比较新位图对象和第二个图像。
首先,您需要循环遍历以下像素:
  • x < mainImageWidth - subImageWidth
  • y < mainImageHeight - subImageHeight
如果主图像中(x,y)处的像素具有与子图像中(0,0)处的像素相同的颜色值,则使用此函数之类的函数从主图像中以与子图像相同的尺寸复制出从(x,y)开始的区域到一个新的位图对象中 - http://msdn.microsoft.com/en-us/library/aa457087.aspx 然后只需循环遍历新对象和子图像中的像素,并在进行相同坐标的颜色比较时中断循环。如果您到达此循环的末尾,则表示匹配成功并返回True,否则继续循环遍历主图像的像素,直到无法再适合子图像为止,然后返回False。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接