如何将鼠标位置放在其他图像中找到的图像的一部分?

时间:2014-05-10 23:06:57

标签: c# .net vb.net math coordinates

我使用AForge库使用this code example在其他图片中查找(部分)图片。

(以下图片只是举例)

我在1920x1080 px。:

中使用此桌面屏幕截图

enter image description here

我搜索并找到上面这张图片(55x557像素):

enter image description here

但我将两张图片的大小调整为25%(以获得比率),因此当我比较图像时,桌面截图为480x270 px。切割后的图像为13x14 px。

使用AForge库,它会返回调整大小的桌面屏幕内找到的(切割的)图像的相对坐标,坐标为x=86, y=200

现在我需要在我的dektop中设置鼠标位置,在VMWare图标的中心(更精确地位于找到的切割图像的中心),这里是我讨论的地方,算术是什么在那里设置鼠标位置的操作?

我记得:

  

分辨率:

我的桌面:1920x1080

图片1:1920x1080

要在Image1中找到的图像:55x57

调整后的图像1:480x270

要在Image1中找到的已调整大小的图像:13x14

  

在Image1中找到的已调整大小的图像的相对坐标:

x = 86,y = 200

3 个答案:

答案 0 :(得分:1)

如果你真的找到了这个位置,如果你真的使用完整的桌面减少到1/4而不是窗口,那么你可以简单地再增加到原始比例并像这样移动鼠标:

newPos= new Point(foundX * 4, foundY * 4);
Cursor.Position = newPos;

如果您的FoundPosition不是Middle,而是TopLeft,您可以像这样调整newPos:

newPos= new Point(foundX * 4 + originalWidth / 2, foundY * 4 + originalHeight / 2);

如果您在窗口中,则必须在设置鼠标位置之前使用PointToScreen()函数计算屏幕坐标的相对位置。

答案 1 :(得分:1)

缩小图像时,请执行以下操作:

intReducePct = 25
ReducedImg1 = ResizeImage(desktopBMP, intReducePct)

' save a restore factor
intFactor = (100 / intReducePct)      ' == 4

' I dont know what the AFOrge search returns, a Point probably
foundPt = Aforge.FindImgInImg(...)

' convert fountPt based on reduction factor (you reduced by 1/4th,
'    so scale up by 4x, basically)
Dim actualPoint As New Point(foundPt.X * intFactor, foundPt.Y * intFactor) 

AForge返回x = 86,y = 200;和86 * 4 = 344; 200 * 4 = 800,但这是原始图像中的上/左(?),你显然想要找到图像的中心,所以也调整原始位图:

' convert fountPt based on reduction factor + bmpFind size:
Dim actualPoint As New Point((foundPt.X * intFactor) + (bmpFind.Width \ 2),
                             (foundPt.Y * intFactor) + (bmpFind.Height \ 2))

bmpFind将是缩小前的原始图像。 Option Strict会坚持使用某些CT类型,但这应该是它的主旨。

答案 2 :(得分:0)

我想分享这个我用来简化事情的通用用法:

''' <summary>
''' Finds a part of an image inside other image and returns the top-left corner coordinates and it's similarity percent.
''' </summary>
''' <param name="BaseImage">
''' Indicates the base image.
''' </param>
''' <param name="ImageToFind">
''' Indicates the image to find in the base image.
''' </param>
''' <param name="Similarity">
''' Indicates the similarity percentage to compare the images.
''' A value of '100' means identical image. 
''' Note: High percentage values with big images could take several minutes to finish.
''' </param>
''' <returns>AForge.Imaging.TemplateMatch().</returns>
Private Function FindImage(ByVal BaseImage As Bitmap,
                           ByVal ImageToFind As Bitmap,
                           ByVal Similarity As Double) As AForge.Imaging.TemplateMatch()

    Dim SingleSimilarity As Single

    ' Translate the readable similarity percent value to Single value.
    Select Case Similarity

        Case Is < 0.1R, Is > 100.0R ' Value is out of range.
            Throw New Exception(String.Format("Similarity value of '{0}' is out of range, range is from '0.1' to '100.0'",
                                              CStr(Similarity)))

        Case Is = 100.0R ' Identical image comparission.
            SingleSimilarity = 1.0F

        Case Else ' Image comparission with specific similarity.
            SingleSimilarity = Convert.ToSingle(Similarity) / 100.0F

    End Select

    ' Set the similarity threshold to find all matching images with specified similarity.
    Dim tm As New AForge.Imaging.ExhaustiveTemplateMatching(SingleSimilarity)

    ' Return all the found matching images, 
    ' it contains the top-left corner coordinates of each one 
    ' and matchings are sortered by it's similarity percent.
    Return tm.ProcessImage(BaseImage, ImageToFind)

End Function

用法示例:

Private Sub Test() Handles MyBase.Shown

    ' A Desktop Screenshot, in 1920x1080 px. resolution.
    Dim DesktopScreenshoot As New Bitmap("C:\Desktop.png")

    ' A cutted piece of the screenshot, in 50x50 px. resolution.
    Dim PartOfDesktopToFind As New Bitmap("C:\PartOfDesktop.png")

    ' Find the part of the image in the desktop, with the specified similarity.
    For Each matching As AForge.Imaging.TemplateMatch In
        FindImage(BaseImage:=DesktopScreenshoot, ImageToFind:=PartOfDesktopToFind, Similarity:=80.5R) ' 80,5% Similarity.

        Dim sb As New System.Text.StringBuilder

        sb.AppendFormat("Top-Left Corner Coordinates: {0}", matching.Rectangle.Location.ToString())
        sb.AppendLine()
        sb.AppendFormat("Similarity Image Percentage: {0}%", (matching.Similarity * 100.0F).ToString("00.00"))

        MessageBox.Show(sb.ToString)

    Next matching

End Sub