【问题标题】:How to display draw rectangle from OpenCV.MatchTemplate如何从 OpenCV.MatchTemplate 显示绘制矩形
【发布时间】:2020-05-10 06:50:32
【问题描述】:

我正在尝试在屏幕截图中查找图像并在其周围绘制一个矩形。我不明白如何解释我的result 矩阵以识别包含图像的区域。

下面的代码将绘制一个矩形,但它的位置并不正确,我不知道这是因为我没有正确使用我的result 还是其他原因。

using (Mat templateImage = CvInvoke.Imread("\\top_1.png", Emgu.CV.CvEnum.ImreadModes.AnyColor))
using (Mat inputImage = CvInvoke.Imread(AppDomain.CurrentDomain.BaseDirectory + "\\currentScreen.png", Emgu.CV.CvEnum.ImreadModes.AnyColor))
{
    Mat result = new Mat();
    CvInvoke.MatchTemplate(inputImage, templateImage, result, Emgu.CV.CvEnum.TemplateMatchingType.SqdiffNormed);

    result.MinMax(out double[] minVal, out double[] maxVal, out Point[] minLoc, out Point[] maxLoc);

    int x = minLoc[0].X;
    int y = minLoc[0].Y;
    int w = maxLoc[0].X - minLoc[0].X;
    int h = maxLoc[0].Y - minLoc[0].Y;

    Form f = new Form
    {
        BackColor = Color.Red,
        //TransparencyKey = Color.Red,
        FormBorderStyle = FormBorderStyle.None,
        TopMost = true,
        Location = new Point(x, y),
        Size = new Size(w, h)
    };

    Application.EnableVisualStyles();
    Application.Run(f);
}

【问题讨论】:

    标签: c# opencv emgucv


    【解决方案1】:

    我唯一看到的是表格的位置,你必须将 StarPosition 设置为 Manual

                Form f = new Form
                {
                    StartPosition = FormStartPosition.Manual,
                    BackColor = Color.Red,
                    //TransparencyKey = Color.Red,
                    FormBorderStyle = FormBorderStyle.None,
                    TopMost = true,
                    Location = new Point(x, y),
                    Size = new Size(w, h)
                };
    

    This is the screen Image

    This is the template

    This is the result

    This is with out StartPosition

    【讨论】:

    • 我不确定我使用的表单是否有问题,就像我输入的位置一样。我认为 StartPosition 在这里并不重要。
    • 您可以尝试直接在图像上绘制矩形而不是在屏幕上使用表单吗?
    • 我最后想通了(我是对的,这与Form f无关,但无论如何感谢您的帮助。如果有兴趣,请参阅我的回答。
    【解决方案2】:

    在阅读和重新阅读文档几次后,我意识到我的错误。我最终没有使用它,因为我为我的用例找到了一种更简单、更可靠的方法,但为了其他人的利益:

    函数MinMax() 为您提供最小值和最大值,因为您使用哪一个取决于所使用的匹配类型。例如Emgu.CV.CvEnum.TemplateMatchingType.SqdiffNormed 返回最小值为最大匹配的结果。

    在 minLoc 中返回的位置只是 templateImage 的左上角坐标,因此它与 templateImage 在相同大小的区域上匹配,这意味着我只需要这样做:

    int x = minLoc[0].X;
    int y = minLoc[0].Y;
    int w = maxLoc[0].X + templateImage.Width;
    int h = maxLoc[0].Y + templateImage.Height;
    

    我被抓住了,因为我不认为在 inputImage 中找到的 templateImage 大小相同。

    【讨论】:

      猜你喜欢
      • 2022-11-12
      • 1970-01-01
      • 2011-10-01
      • 1970-01-01
      • 2018-04-05
      • 1970-01-01
      • 2016-10-05
      • 2012-03-05
      • 1970-01-01
      相关资源
      最近更新 更多