C# OpenCvSharp 图片找茬

2024-05-08 02:52

本文主要是介绍C# OpenCvSharp 图片找茬,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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());}}
}

下载 

源码下载

这篇关于C# OpenCvSharp 图片找茬的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/969104

相关文章

使用opencv优化图片(画面变清晰)

文章目录 需求影响照片清晰度的因素 实现降噪测试代码 锐化空间锐化Unsharp Masking频率域锐化对比测试 对比度增强常用算法对比测试 需求 对图像进行优化,使其看起来更清晰,同时保持尺寸不变,通常涉及到图像处理技术如锐化、降噪、对比度增强等 影响照片清晰度的因素 影响照片清晰度的因素有很多,主要可以从以下几个方面来分析 1. 拍摄设备 相机传感器:相机传

2. c#从不同cs的文件调用函数

1.文件目录如下: 2. Program.cs文件的主函数如下 using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using System.Windows.Forms;namespace datasAnalysis{internal static

C#实战|大乐透选号器[6]:实现实时显示已选择的红蓝球数量

哈喽,你好啊,我是雷工。 关于大乐透选号器在前面已经记录了5篇笔记,这是第6篇; 接下来实现实时显示当前选中红球数量,蓝球数量; 以下为练习笔记。 01 效果演示 当选择和取消选择红球或蓝球时,在对应的位置显示实时已选择的红球、蓝球的数量; 02 标签名称 分别设置Label标签名称为:lblRedCount、lblBlueCount

用命令行的方式启动.netcore webapi

用命令行的方式启动.netcore web项目 进入指定的项目文件夹,比如我发布后的代码放在下面文件夹中 在此地址栏中输入“cmd”,打开命令提示符,进入到发布代码目录 命令行启动.netcore项目的命令为:  dotnet 项目启动文件.dll --urls="http://*:对外端口" --ip="本机ip" --port=项目内部端口 例: dotnet Imagine.M

Android 10.0 mtk平板camera2横屏预览旋转90度横屏拍照图片旋转90度功能实现

1.前言 在10.0的系统rom定制化开发中,在进行一些平板等默认横屏的设备开发的过程中,需要在进入camera2的 时候,默认预览图像也是需要横屏显示的,在上一篇已经实现了横屏预览功能,然后发现横屏预览后,拍照保存的图片 依然是竖屏的,所以说同样需要将图片也保存为横屏图标了,所以就需要看下mtk的camera2的相关横屏保存图片功能, 如何实现实现横屏保存图片功能 如图所示: 2.mtk

Spring MVC 图片上传

引入需要的包 <dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.1</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-

Prompt - 将图片的表格转换成Markdown

Prompt - 将图片的表格转换成Markdown 0. 引言1. 提示词2. 原始版本 0. 引言 最近尝试将图片中的表格转换成Markdown格式,需要不断条件和优化提示词。记录一下调整好的提示词,以后在继续优化迭代。 1. 提示词 英文版本: You are an AI assistant tasked with extracting the content of

C# dateTimePicker 显示年月日,时分秒

dateTimePicker默认只显示日期,如果需要显示年月日,时分秒,只需要以下两步: 1.dateTimePicker1.Format = DateTimePickerFormat.Time 2.dateTimePicker1.CustomFormat = yyyy-MM-dd HH:mm:ss Tips:  a. dateTimePicker1.ShowUpDown = t

C#关闭指定时间段的Excel进程的方法

private DateTime beforeTime;            //Excel启动之前时间          private DateTime afterTime;               //Excel启动之后时间          //举例          beforeTime = DateTime.Now;          Excel.Applicat

C# 防止按钮botton重复“点击”的方法

在使用C#的按钮控件的时候,经常我们想如果出现了多次点击的时候只让其在执行的时候只响应一次。这个时候很多人可能会想到使用Enable=false, 但是实际情况是还是会被多次触发,因为C#采用的是消息队列机制,这个时候我们只需要在Enable = true 之前加一句 Application.DoEvents();就能达到防止重复点击的问题。 private void btnGenerateSh