我得到以下代码来在源图像中查找模板:
private void button1_Click(object sender, EventArgs e)
{
toggleStatus();
using (Mat template = CvInvoke.Imread("C:\\Users\\Hendr\\Desktop\\temp.png", Emgu.CV.CvEnum.ImreadModes.Grayscale))
using (Mat source = CvInvoke.Imread("C:\\Users\\Hendr\\Desktop\\yVLsd.png", Emgu.CV.CvEnum.ImreadModes.Grayscale))
{
log("Image loaded into memory...");
// pictureBox1.Image = template.Bitmap;
pictureBox1.Image = source.Bitmap;
var width = source.Width - template.Width + 1;
var height = source.Height - template.Height + 1;
// Mat result = new Mat((new System.Drawing.Size(width,height), Emgu.CV.CvEnum.IplDepth.IplDepth32F, 1);
Mat result = new Mat(width, height, DepthType.Cv8U, 1);
CvInvoke.MatchTemplate(source, template, result ,Emgu.CV.CvEnum.TemplateMatchingType.SqdiffNormed);
var THRESHOLD = 0.0;
double minVal = 0, maxVal = 0;
System.Drawing.Point minLoc = new Point(), maxLoc = new Point();
CvInvoke.MinMaxLoc(result, ref minVal, ref maxVal, ref minLoc, ref maxLoc);
Rectangle rect = new Rectangle(minLoc.X,minLoc.Y, width,height);
var outlineColor = (minVal > THRESHOLD) ?Color.Green : Color.Red;
CvInvoke.Rectangle(result, rect, new MCvScalar(0,0,0));
log(rect.Location.ToString());
pictureBox1.Image = result.Bitmap;
}
foreach (object itemChecked in checkedListBox1.CheckedItems)
{
// Use the IndexOf method to get the index of an item.
MessageBox.Show("Item with title: \"" + itemChecked.ToString() +
"\", is checked. Checked state is: " +
checkedListBox1.GetItemCheckState(checkedListBox1.Items.IndexOf(itemChecked)).ToString() + ".");
}
}
但是,我的结果全都涂黑了:
我验证了template
和source
Mat 已正确加载到内存中。见pictureBox1.Image = source.Bitmap; ...
。
最后,我从文档中找不到任何信息,为什么我的result
Mat( pictureBox1.Image = result.Bitmap;
) 是黑色的。其他人也有/遇到过这个问题,可以给我一些参考链接或快速修复吗?