How to place the mouse position in the part of the image found in another image?

I am using a library AForgeto find (part of an image) inside another image using this code example .

(The images below are for reference only)

I use this desktop screenshot in 1920x1080 px .:

enter image description here

And I searched and found this fragment of the image above (55x557 px.):

enter image description here

But I change both images by 25% (to get the speed of comparison), so when I compare the images, the screenshot on the desktop is 480x270px. and cut off 13x14px image .

Using the library AForge, she returns me the relative coordinates of the found (cut) image inside the resized screen for the screen, coordinates x=86, y=200.

Now I need to set the mouse position in my desktop, in the center of the VMWare icon (more precisely, in the center of the found cropped image), and this is where I am confused, what is the arithmetic operation to set the mouse position there?

I'll remember:

Resolution:

My desktop: 1920x1080

Image 1: 1920x1080

Image to search in Image1: 55x57

Image resized 1: 480x270

Changed the image that can be found in Image1: 13x14

Relative coordinates of the found resized Image to search in Image1:

x = 86, y = 200

+4
source share
3 answers

When you zoom out, you do this:

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) 

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 . Strict CTypes, .

+1

, , 1/4th, , :

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

FoundPosition , TopLeft, newPos :

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

, PointToScreen() .

+1

I want to share this common use method that I wrote to simplify:

''' <summary>
''' Finds a part of an image inside other image and returns the top-left corner coordinates and it 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 similarity percent.
    Return tm.ProcessImage(BaseImage, ImageToFind)

End Function

Usage example:

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
0
source

Source: https://habr.com/ru/post/1540077/


All Articles