【问题标题】:How to put the mouse position in a part of an image found in other image?如何将鼠标位置放在其他图像中找到的图像的一部分?
【发布时间】:2014-05-10 23:06:57
【问题描述】:

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

(以下图片仅供参考)

我正在使用这个 1920x1080 像素的桌面截图:

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

但我将两个图像的大小调整为 25%(以获得比较速度),所以当我比较图像时,桌面截图是 480x270 px。剪切后的图像是13x14 px。

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

现在我需要在我的桌面设置鼠标位置,在 VMWare 图标的中心(更确切地说是在找到的切割图像的中心),这就是我感到困惑的地方,算术运算是什么在那里设置鼠标位置?

我会记住的:

决议:

我的桌面:1920x1080

图片 1:1920x1080

在 Image1 中查找的图像:55x57

调整后的图像 1:480x270

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

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

x=86,y=200

【问题讨论】:

  • 第一个问题 - 你确定它找到了吗?如果您在 Photoshop 之类的工具中加载图像,它是否真的在那个位置(您有一个点错字:x=86, x=200 2 x's?)。也就是说,越大越小 4 倍,所以 x=344 y=800 似乎是正确的
  • x = (My Desktop.x / Resized Image1.x) * 相对坐标.x; y = (My Desktop.y / Resized Image1.y) * 相对坐标.y;
  • 抱歉打错了,我已经改正了,是的,我确定找到了图像,我在 MSPaint 中看到了坐标,我需要澄清一下 AForge 的坐标给我对应于找到的剪切图像的左上角
  • larger/smaller 不明确 = 原始桌面是您搜索的较小桌面的 4 倍,因此使用系数 4。我想知道它是否找到不同比例的图像 - 将桌面大小调整 X% 和图像按 Y% 查找并查看是否找到
  • @Plutonix 我将相对坐标检索到Rectangle,所以也许我可以使用矩形的Inflate 方法来简化事情?...但数学不是我的

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


【解决方案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 会坚持一些 CType,但这应该是它的要点。

【讨论】:

  • 很容易理解什么时候解释得这么好,谢谢。
【解决方案2】:

如果你真的找到了这个位置,并且你真的在使用缩小到 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() 函数计算屏幕坐标的相对位置。

【讨论】:

    【解决方案3】:

    我想分享一下我为简化事情而写的这个通用使用方法:

    ''' <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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-16
      • 1970-01-01
      • 2012-09-18
      • 2016-02-19
      • 2022-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多