首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在OpenCV.MatchTemplate中显示绘制矩形

如何在OpenCV.MatchTemplate中显示绘制矩形
EN

Stack Overflow用户
提问于 2020-01-24 06:38:07
回答 2查看 448关注 0票数 0

我正在尝试找到一个截图中的图像,并在它周围画一个矩形。我不明白的是,如何解释我的result矩阵来识别包含图像的区域。

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

代码语言:javascript
运行
复制
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);
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-01-28 00:16:02

在一遍又一遍地阅读文档之后,我意识到我的错误。我最终没有使用这个方法,因为我为我的用例找到了一个更简单、更可靠的方法,但为了其他人的利益:

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

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

代码语言:javascript
运行
复制
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的大小是一样的。

票数 0
EN

Stack Overflow用户

发布于 2020-01-24 08:27:55

我唯一看到的是表单的位置,您必须将StarPosition设置为手动

代码语言:javascript
运行
复制
            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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59888044

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档