C# OpenCvSharp 图片找茬
目录
效果
项目
代码
下载
效果
项目
代码
using OpenCvSharp;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
namespace OpenCvSharp_Demo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Stopwatch stopwatch = new Stopwatch();
Mat result_image;
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Image = new Bitmap("1.png");
pictureBox2.Image = new Bitmap("2.png");
}
private void button2_Click(object sender, EventArgs e)
{
stopwatch.Restart();
//加载两张图片
Mat image1 = Cv2.ImRead("1.png");
Mat image2 = Cv2.ImRead("2.png");
result_image = image2.Clone();
//创建一个空的Mat对象用于存储差异
Mat difference = new Mat();
//计算两张图片的差异
Cv2.Absdiff(image1, image2, difference);
//模糊
Cv2.Blur(difference, difference, new OpenCvSharp.Size(5, 5));
//转换为灰度图
Cv2.CvtColor(difference, difference, ColorConversionCodes.BGR2GRAY);
//膨胀5次
Mat kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(5, 5));
Cv2.Dilate(difference, difference, kernel, null, 5);
//二值化
Cv2.Threshold(difference, difference, 20, 200, ThresholdTypes.Binary);
//找出轮廓
OpenCvSharp.Point[][] contours;
HierarchyIndex[] hierarchly;
Cv2.FindContours(difference, out contours, out hierarchly, RetrievalModes.External, ContourApproximationModes.ApproxNone, new OpenCvSharp.Point(0, 0));
Rect rect;
for (int i = 0; i < contours.Length; i++)
{
rect = Cv2.BoundingRect(contours[i]);
Cv2.Rectangle(result_image, rect, new Scalar(0, 255, 0), 2, LineTypes.Link8);
}
double costTime = stopwatch.Elapsed.TotalMilliseconds;
textBox1.Text = $"耗时:{costTime:F2}ms";
pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
}
}
}
using OpenCvSharp;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
namespace OpenCvSharp_Demo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Stopwatch stopwatch = new Stopwatch();
Mat result_image;
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Image = new Bitmap("1.png");
pictureBox2.Image = new Bitmap("2.png");
}
private void button2_Click(object sender, EventArgs e)
{
stopwatch.Restart();
//加载两张图片
Mat image1 = Cv2.ImRead("1.png");
Mat image2 = Cv2.ImRead("2.png");
result_image = image2.Clone();
//创建一个空的Mat对象用于存储差异
Mat difference = new Mat();
//计算两张图片的差异
Cv2.Absdiff(image1, image2, difference);
//模糊
Cv2.Blur(difference, difference, new OpenCvSharp.Size(5, 5));
//转换为灰度图
Cv2.CvtColor(difference, difference, ColorConversionCodes.BGR2GRAY);
//膨胀5次
Mat kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(5, 5));
Cv2.Dilate(difference, difference, kernel, null, 5);
//二值化
Cv2.Threshold(difference, difference, 20, 200, ThresholdTypes.Binary);
//找出轮廓
OpenCvSharp.Point[][] contours;
HierarchyIndex[] hierarchly;
Cv2.FindContours(difference, out contours, out hierarchly, RetrievalModes.External, ContourApproximationModes.ApproxNone, new OpenCvSharp.Point(0, 0));
Rect rect;
for (int i = 0; i < contours.Length; i++)
{
rect = Cv2.BoundingRect(contours[i]);
Cv2.Rectangle(result_image, rect, new Scalar(0, 255, 0), 2, LineTypes.Link8);
}
double costTime = stopwatch.Elapsed.TotalMilliseconds;
textBox1.Text = $"耗时:{costTime:F2}ms";
pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
}
}
}
下载
源码下载