目录
效果
项目
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenCvSharp;
using OpenCvSharp.Extensions;
namespace OpenCvSharp_MatchTemplate_多目标匹配
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Image = new Bitmap("test.png");
String tempImg_path = ("t1.png");
String srcImg_path = ("test.png");
Bitmap bitmap = Recoganize(srcImg_path, tempImg_path, 0.95, 1, "target", 10);
pictureBox2.Image = bitmap;
}
Bitmap Recoganize(String srcImg_path, String tempImg_path, double threshold = 0.5, double compressed = 0.5, string name = "target", int space = 10)
{
DateTime beginTime = DateTime.Now; //获取开始时间
// 新建图变量并分配内存
Mat srcImg = new Mat();
// 读取要被匹配的图像
srcImg = Cv2.ImRead(srcImg_path);
// 更改尺寸
Cv2.Resize(srcImg, srcImg, new OpenCvSharp.Size((int)srcImg.Cols * compressed, (int)srcImg.Rows * compressed));
// 初始化保存保存匹配结果的横纵坐标列表
List<int> Xlist = new List<int> { };
List<int> Ylist = new List<int> { };
int order = 0;
Mat tempImg = Cv2.ImRead(tempImg_path);
Cv2.Resize(tempImg, tempImg, new OpenCvSharp.Size((int)tempImg.Cols * compressed, (int)tempImg.Rows * compressed));
Mat result = srcImg.Clone();
int dstImg_rows = srcImg.Rows - tempImg.Rows + 1;
int dstImg_cols = srcImg.Cols - tempImg.Cols + 1;
Mat dstImg = new Mat(dstImg_rows, dstImg_cols, MatType.CV_32F, 1);
Cv2.MatchTemplate(srcImg, tempImg, dstImg, TemplateMatchModes.CCoeffNormed);
int count = 0;
for (int i = 0; i < dstImg_rows; i++)
{
for (int j = 0; j < dstImg_cols; j++)
{
float matchValue = dstImg.At<float>(i, j);
if (matchValue >= threshold && Xlist.Count == 0)
{
count++;
Cv2.Rectangle(result, new Rect(j, i, tempImg.Width, tempImg.Height), new Scalar(0, 255, 0), 2);
Cv2.PutText(result, name, new OpenCvSharp.Point(j, i - (int)20 * compressed), HersheyFonts.HersheySimplex, 0.5, new Scalar(0, 0, 0), 1);
Xlist.Add(j);
Ylist.Add(i);
}
if (matchValue >= threshold && Xlist.Count != 0)
{
for (int q = 0; q < Xlist.Count; q++)
{
if (Math.Abs(j - Xlist[q]) + Math.Abs(i - Ylist[q]) < space)
{
order = 1;
break;
}
}
if (order != 1)
{
count++;
Cv2.Rectangle(result, new Rect(j, i, tempImg.Width, tempImg.Height), new Scalar(0, 255, 0), 2);
Cv2.PutText(result, name, new OpenCvSharp.Point(j, i - (int)20 * compressed), HersheyFonts.HersheySimplex, 0.5, new Scalar(0, 0, 0), 1);
Xlist.Add(j);
Ylist.Add(i);
}
order = 0;
}
}
}
Console.WriteLine("目标数量:{0}", count);
DateTime endTime = DateTime.Now; //获取结束时间
TimeSpan oTime = endTime.Subtract(beginTime); //求时间差的函数
Console.WriteLine("程序的运行时间:{0} 毫秒", oTime.TotalMilliseconds);
return result.ToBitmap();
}
}
}
cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenCvSharp;
using OpenCvSharp.Extensions;
namespace OpenCvSharp_MatchTemplate_多目标匹配
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Image = new Bitmap("test.png");
String tempImg_path = ("t1.png");
String srcImg_path = ("test.png");
Bitmap bitmap = Recoganize(srcImg_path, tempImg_path, 0.95, 1, "target", 10);
pictureBox2.Image = bitmap;
}
Bitmap Recoganize(String srcImg_path, String tempImg_path, double threshold = 0.5, double compressed = 0.5, string name = "target", int space = 10)
{
DateTime beginTime = DateTime.Now; //获取开始时间
// 新建图变量并分配内存
Mat srcImg = new Mat();
// 读取要被匹配的图像
srcImg = Cv2.ImRead(srcImg_path);
// 更改尺寸
Cv2.Resize(srcImg, srcImg, new OpenCvSharp.Size((int)srcImg.Cols * compressed, (int)srcImg.Rows * compressed));
// 初始化保存保存匹配结果的横纵坐标列表
List<int> Xlist = new List<int> { };
List<int> Ylist = new List<int> { };
int order = 0;
Mat tempImg = Cv2.ImRead(tempImg_path);
Cv2.Resize(tempImg, tempImg, new OpenCvSharp.Size((int)tempImg.Cols * compressed, (int)tempImg.Rows * compressed));
Mat result = srcImg.Clone();
int dstImg_rows = srcImg.Rows - tempImg.Rows + 1;
int dstImg_cols = srcImg.Cols - tempImg.Cols + 1;
Mat dstImg = new Mat(dstImg_rows, dstImg_cols, MatType.CV_32F, 1);
Cv2.MatchTemplate(srcImg, tempImg, dstImg, TemplateMatchModes.CCoeffNormed);
int count = 0;
for (int i = 0; i < dstImg_rows; i++)
{
for (int j = 0; j < dstImg_cols; j++)
{
float matchValue = dstImg.At<float>(i, j);
if (matchValue >= threshold && Xlist.Count == 0)
{
count++;
Cv2.Rectangle(result, new Rect(j, i, tempImg.Width, tempImg.Height), new Scalar(0, 255, 0), 2);
Cv2.PutText(result, name, new OpenCvSharp.Point(j, i - (int)20 * compressed), HersheyFonts.HersheySimplex, 0.5, new Scalar(0, 0, 0), 1);
Xlist.Add(j);
Ylist.Add(i);
}
if (matchValue >= threshold && Xlist.Count != 0)
{
for (int q = 0; q < Xlist.Count; q++)
{
if (Math.Abs(j - Xlist[q]) + Math.Abs(i - Ylist[q]) < space)
{
order = 1;
break;
}
}
if (order != 1)
{
count++;
Cv2.Rectangle(result, new Rect(j, i, tempImg.Width, tempImg.Height), new Scalar(0, 255, 0), 2);
Cv2.PutText(result, name, new OpenCvSharp.Point(j, i - (int)20 * compressed), HersheyFonts.HersheySimplex, 0.5, new Scalar(0, 0, 0), 1);
Xlist.Add(j);
Ylist.Add(i);
}
order = 0;
}
}
}
Console.WriteLine("目标数量:{0}", count);
DateTime endTime = DateTime.Now; //获取结束时间
TimeSpan oTime = endTime.Subtract(beginTime); //求时间差的函数
Console.WriteLine("程序的运行时间:{0} 毫秒", oTime.TotalMilliseconds);
return result.ToBitmap();
}
}
}