本文介绍了在屏幕上搜索矩形并与图像进行比较?图片搜寻的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在屏幕上搜索设置的矩形并将其与我指定的图像进行比较以查看其是否匹配?

I am wondering how I could search a set rectangle on the screen and have it compare to an image that I specify to see if it matches?

让我们说它可以搜索x1 y1到x2 y2并与图像进行比较吗?并返回布尔值?

Lets say it could Search x1 y1 to x2 y2 and compare against an image? and return the boolean?

我知道Auto-it在此处具有类似的功能: http://www .autohotkey.com/docs/commands/ImageSearch.htm

I know Auto-it has a similar function seen here: http://www.autohotkey.com/docs/commands/ImageSearch.htm

有人做过可以参考的事情吗?我正在使用vb.net.

Has anyone done this that they could reference? I am using vb.net.

Abdias,我将您的代码放入一个类中,而我这样称呼它:

Abdias, I have put your code into a class instead and I am calling it like this:

     Dim bm As Bitmap = Bitmap.FromFile(Label1.Text)
    Dim bm2 As Bitmap = Bitmap.FromFile(Label2.Text)
    Dim pnt As Point = ImageFinder.Contains(bm, bm2)
    If pnt <> Nothing Then
        MessageBox.Show("Possible match found at " & pnt.X.ToString() & " " & pnt.Y.ToString())
    Else
        MessageBox.Show("No match.")
    End If

看来,我尝试的每组图像都毫无意义.即使它们100%互相包含.我拍摄了一张图片,并裁剪了几像素,但仍然没有返回匹配结果.我确保来源更大.我尝试将几张图片另存为24位jpg的涂料,仍然一无所有.

It seems that every set of images I try return no point. Even though they 100% contain eachother. I took an image and cropped it by a couple px and still did not return a match. I have made sure the source is larger. I tried saving a couple images as 24 bit jpg in paint and still nothing.

这是两个示例图像.

Here are two sample images.

推荐答案

我创建了此函数,该函数可以查看较大的图像中是否存在图像.它是作为扩展编写的,但可以轻松修改为常规功能以及支持区域.

I made this function which can see if an image exist within a bigger image. It is written as an extension, but can easily be modified to a normal function as well as supporting a region.

要使用它:

  • 将主图像作为bmp加载到标准Bitmap
  • 加载图像以查找bmpSearch
  • Load main image into standard Bitmap as bmp
  • Load image to look for into bmpSearch

然后致电:

Dim pt as Point = bmp.Contains(bmpSearch)
If pt <> Nothing Then
    '... image found at pt
End If

扩展程序的代码(有优化空间,但在此站点上以另一个问题的20分钟练习形式编写):

The code for the extension (room for optimizations, but written as a 20 minute exercise for another question on this site):

'
'-- Extension for Bitmap
'
<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

这篇关于在屏幕上搜索矩形并与图像进行比较?图片搜寻的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 14:29