我正在尝试找到一个截图中的图像,并在它周围画一个矩形。我不明白的是,如何解释我的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);
}
发布于 2020-01-28 00:16:02
在一遍又一遍地阅读文档之后,我意识到我的错误。我最终没有使用这个方法,因为我为我的用例找到了一个更简单、更可靠的方法,但为了其他人的利益:
函数MinMax()
为您提供了最小值和最大值,因为您使用哪一个取决于所使用的匹配类型。例如,Emgu.CV.CvEnum.TemplateMatchingType.SqdiffNormed
返回min为最大匹配的结果。
在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
的大小是一样的。
发布于 2020-01-24 08:27:55
我唯一看到的是表单的位置,您必须将StarPosition设置为手动
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)
};
https://stackoverflow.com/questions/59888044
复制相似问题