using OpenCvSharp;
using OpenCvSharp.Extensions;
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 Point = OpenCvSharp.Point;
namespace WindowsFormsApp10
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//BackgroundImage= duopattent(openfilestring(),openfilestring(),0.7);
}
string openfilestring()
{
OpenFileDialog fileDialog = new OpenFileDialog();
if (fileDialog.ShowDialog() == DialogResult.OK)
return System.IO.Path.GetFullPath(fileDialog.FileName);//将选中的文件的路径传递给TextBox “FilePath”
return "";
}
static Bitmap duopattent (string s1,string s2 , double threshold = 0.91)
{
//读取图片
Mat temp = new Mat(s1, ImreadModes.AnyColor);//模板图片
//Cv2.ImWrite("temp_write.jpg",temp);//写入图片
Mat wafer = new Mat(s2, ImreadModes.AnyColor);//被匹配图
return pattenshibie(temp,wafer,threshold);
}
static Bitmap pattenshibie(Mat temp, Mat wafer, double threshold = 0.91)
{
Mat result = new Mat(); //匹配结果
//模板匹配
Cv2.MatchTemplate(wafer, temp, result, TemplateMatchModes.CCoeffNormed);//最好匹配为1,值越小匹配越差
Double minVul;
Double maxVul;
Point minLoc = new Point(0, 0);
Point maxLoc = new Point(0, 0);
Point matchLoc = new Point(0, 0);
Cv2.Normalize(result, result, 0, 1, NormTypes.MinMax, -1);//归一化
Cv2.MinMaxLoc(result, out minVul, out maxVul, out minLoc, out maxLoc);//查找极值
matchLoc = maxLoc;//最大值坐标
//result.Set(matchLoc.Y, matchLoc.X, 0);//改变最大值为最小值
Mat mask = wafer.Clone();//复制整个矩阵
//画框显示
Cv2.Rectangle(mask, matchLoc, new Point(matchLoc.X + temp.Cols, matchLoc.Y + temp.Rows), Scalar.Green, 2);
Console.WriteLine("最大值:{0},X:{1},Y:{2}", maxVul, matchLoc.Y, matchLoc.X);
Console.WriteLine("At获取最大值(Y,X):{0}", result.At<float>(matchLoc.Y, matchLoc.X));
Console.WriteLine("result的类型:{0}", result.GetType());
//循环查找画框显示
Mat maskMulti = wafer.Clone();//复制整个矩阵
for (int i = 1; i < result.Rows - temp.Rows; i += temp.Rows)//行遍历
{
for (int j = 1; j < result.Cols - temp.Cols; j += temp.Cols)//列遍历
{
Rect roi = new Rect(j, i, temp.Cols, temp.Rows); //建立感兴趣
Mat RoiResult = new Mat(result, roi);
Cv2.MinMaxLoc(RoiResult, out minVul, out maxVul, out minLoc, out maxLoc);//查找极值
matchLoc = maxLoc;//最大值坐标
if (maxVul > threshold)
{
//画框显示
Cv2.Rectangle(maskMulti, new Point(j + maxLoc.X, i + maxLoc.Y), new Point(j + maxLoc.X + temp.Cols, i + maxLoc.Y + temp.Rows), Scalar.Green, 2);
string axis = '(' + Convert.ToString(i + maxLoc.Y) + ',' + Convert.ToString(j + maxLoc.X) + ')';
Cv2.PutText(maskMulti, axis, new Point(j + maxLoc.X, i + maxLoc.Y), HersheyFonts.HersheyPlain, 1, Scalar.Red, 1, LineTypes.Link4);
}
}
}
return maskMulti.ToBitmap();
//新建窗体显示图片
//using (new Window("temp image", temp))
//using (new Window("wafer image", wafer))
//using (new Window("mask image", mask))
//using (new Window("maskMulti image", maskMulti))
//{
// Cv2.WaitKey();
//}
}
private void pictureBox3_Click(object sender, EventArgs e)
{
pictureBox3.Image = pattenshibie(BitmapConverter.ToMat((Bitmap)pictureBox1.Image),
BitmapConverter.ToMat((Bitmap)pictureBox2.Image),0.6);
}
}
}