二、显示图片、提取边缘特征并保存(C# + OpenCV)

实现功能:
1,打开照片,并显示
2,对选择的照片进行Canny边缘检测
3,保存边缘检测之后的结果

一、布局

打开在视图下打开工具箱

选择一个PictureBox,仨Button

对Button改个名字

仨Button,分别对应三个功能的实现
最终的页面如下所示

二、引入命名空间

csharp 复制代码
using System;
using System.Drawing;
using System.Windows.Forms;

using OpenCvSharp;
using OpenCvSharp.Extensions;

要有这三个哈,没有就去浏览里面搜索下载安装就行

三、选择照片功能

定义全局变量picture用于存放读取的图片的位置

OpenFileDialog file = new OpenFileDialog();

new一个OpenFileDialog 类对象file,用于打开选择图片
OpenFileDialog 类官网API

if (file.FileName != string.Empty)

此时file.FileName就是图片的路径,是一个字符串数据

pictureBox1.Load(file.FileName);
pictureBox1就是布局里面的PictureBox控件名称

参数是图片的路径,这里传入用户选择的图片路径file.FileName

此时就可以将用户选择的照片显示在PictureBox控件上

csharp 复制代码
string picture = ""; // 全局变量存放读取图片的路径
private void button1_Click(object sender, EventArgs e)
{
    OpenFileDialog file = new OpenFileDialog();
    file.InitialDirectory = ".";
    file.Filter = "所有文件(*.*)|*.*";
    file.ShowDialog();
    if (file.FileName != string.Empty)
    {
        try
        {
        	picture = file.FileName;
            this.pictureBox1.Load(file.FileName);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }  
}

四、Canny边缘检测功能

OpenCV处理的图片都是Mat形式
Mat img1 = new Mat(picture, ImreadModes.Color);

读取用户选择的图片

参数一:图片的路径,picture

参数二:读取图片的形式,ImreadModes.Color三颜色通道的彩色图

Mat gray = new Mat();存放处理图片之后的灰度图
Mat canny = new Mat();存放Canny边缘检测之后的结果图片

Cv2.CvtColor(img1,gray, ColorConversionCodes.RGB2GRAY);

img1转换为灰度图,输出结果为gray

参数一:输入图像,img1

参数二:输出图像,gray

参数三:转换的操作,ColorConversionCodes.BGR2GRAY,BGR转灰度图

Cv2.Canny(gray, canny, 100, 200);

gray图像进行Canny边缘检测,输出结果为canny,minVal 为100,maxVal 为200
当然Canny算子还有其他参数,读者可自行拓展学习

参数一:输入图像,gray

参数二:输出图像,canny

参数三:第一个阈值,100

参数四:第二个阈值,200

Bitmap bitmap = canny.ToBitmap();
graycanny都是Mat 类型的数据,要想在PictureBox控件上展示,必须转换为Bitmap类型数据

pictureBox1.Image = bitmap;

此时bitmapBitmap类型数据,可以进行在控件上展示

csharp 复制代码
private void button2_Click(object sender, EventArgs e)
{
    Mat img1 = new Mat(picture, ImreadModes.Color);
    Mat gray = new Mat();
    Mat canny = new Mat();

    Cv2.CvtColor(img1,gray, ColorConversionCodes.BGR2GRAY);
    Cv2.Canny(gray, canny, 100, 200);
    
    Bitmap bitmap = canny.ToBitmap();
    pictureBox1.Image = bitmap;
}

五、保存图片功能

SaveFileDialog save = new SaveFileDialog();

new一个SaveFileDialog 类对象save ,用于选择保存图片的位置
SaveFileDialog 类官网API

pictureBox1.Image.Save(save.FileName);

将PictureBox控件显示的通过Canny算子进行边缘检测之后的照片结果进行保存

csharp 复制代码
private void button3_Click(object sender, EventArgs e)
{
    SaveFileDialog save = new SaveFileDialog();
    save.InitialDirectory = ".";
    save.Filter = "所有文件(*.*)|*.*";
    save.ShowDialog();
    if(save.FileName != string.Empty)
    {
        try
        {
            this.pictureBox1.Image.Save(save.FileName);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
     }
}

六、完整代码

复制的时候记得考虑实际的namespace
namespace opencv_test1跟你的项目名称一致哈

csharp 复制代码
using System;
using System.Drawing;
using System.Windows.Forms;

using OpenCvSharp;
using OpenCvSharp.Extensions;

namespace opencv_test1
{
   
    public partial class Form1 : Form
    {
       
        string picture = "";

        public Form1()
        {
            InitializeComponent();          
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
        }
       
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog file = new OpenFileDialog();
            file.InitialDirectory = ".";
            file.Filter = "所有文件(*.*)|*.*";
            file.ShowDialog();
            if (file.FileName != string.Empty)
            {
                try
                {
                    picture = file.FileName;
                    this.pictureBox1.Load(picture);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }  
        }


        private void button2_Click(object sender, EventArgs e)
        {
            Mat img1 = new Mat(picture, ImreadModes.Color);
            Mat gray = new Mat();
            Mat canny = new Mat();

            Cv2.CvtColor(img1,gray, ColorConversionCodes.BGR2GRAY);
            Cv2.Canny(gray, canny, 100, 200);
            
            Bitmap bitmap = canny.ToBitmap();
            pictureBox1.Image = bitmap;
        }


        private void button3_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.InitialDirectory = ".";
            save.Filter = "所有文件(*.*)|*.*";
            save.ShowDialog();
            if (save.FileName != string.Empty)
            {
                try
                {
                    this.pictureBox1.Image.Save(save.FileName);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }
}

七、效果展示

运行效果

选择照片

Canny边缘检测

保存照片

保存效果

相关推荐
yc_122410 分钟前
SqlHelper 实现类,支持多数据库,提供异步操作、自动重试、事务、存储过程、分页、缓存等功能。
数据库·c#
Q_Q196328847511 分钟前
python的家教课程管理系统
开发语言·spring boot·python·django·flask·node.js·php
夏天是冰红茶14 分钟前
图像处理:预览并绘制图像细节
图像处理·人工智能·opencv
Black_Cat_yyds17 分钟前
设计杂谈-工厂模式
java·开发语言
进击的雷神22 分钟前
Perl语言深度考查:从文本处理到正则表达式的全面掌握
开发语言·后端·scala
进击的雷神26 分钟前
Perl测试起步:从零到精通的完整指南
开发语言·后端·scala
点云SLAM29 分钟前
Python中in和is关键字详解和使用
开发语言·人工智能·python·python学习·in和is关键字·python中for循环
Kookoos40 分钟前
Redis + ABP vNext 构建分布式高可用缓存架构
redis·分布式·缓存·架构·c#·.net
郭尘帅6661 小时前
Vue3中实现轮播图
开发语言·前端·javascript
Thomas_YXQ2 小时前
Unity3D Overdraw性能优化详解
开发语言·人工智能·性能优化·unity3d