C#:Canny边缘检测

2023-01-14  本文已影响0人  大龙10

  边缘检测目的是在保留原有图像属性的情况下,显著减少图像的数据规模。
  有多种算法可以进行边缘检测,虽然Canny算法年代久远,但可以说它是边缘检测的一种标准算法。

一、Canny边缘检测

程序运行结果

二、检测步骤

  完整的Canny边缘检测由如下步骤组成。

三、函数Cv2.Canny

Cv2.Canny(src_img, dst, tkBarCannyMin.Value, kBarCannyMax.Value, hole, rbBtnTrue.Checked);
参数:

四、程序

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenCvSharp;
using OpenCvSharp.Extensions;
using Sunny.UI;

namespace Ky_Cv2Canny
{
    public partial class Form1 : UIForm
    {
        private Image image = null;
        private Mat dst = new Mat();
        private Mat src_img;
        string filePath = "";
        //private static int Num = 20;
        private List<Mat> reList = new List<Mat>();
        private int step = 1;

        public Form1()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint, true);
        }

        private void openImage_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "选择操作的图片";
            openFileDialog.Filter = "图片 *.jpg|*.jpg|图像*.png|*.png";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                filePath = openFileDialog.FileName;
                image = Image.FromFile(filePath);
                src_img = Cv2.ImRead(filePath);
                Mat tem1 = new Mat();
                src_img.CopyTo(tem1);
                if (reList.Count > 0)
                {
                    reList[0] = tem1;

                }
                else
                {
                    reList.Add(tem1);
                }

            }
            if (filePath != "")
            {
                picBoxShowDel.Image = image;
                picShowOri.Image = image;
            }


        }

        /// <summary>
        /// 边缘检测Canny
        /// </summary>
        private void Canny()
        {
            int hole = 0;
            if (!int.TryParse(txtBoxCannyHole.Text, out hole))
            {
                return;
            }
            try
            {
                Cv2.Canny(src_img, dst, tkBarCannyMin.Value, tkBarCannyMax.Value, hole, rbBtnTrue.Checked);
                picBoxShowDel.Image = dst.ToBitmap();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }

        private void tkBarCannyMin_Scroll(object sender, EventArgs e)
        {
            if (tkBarCannyMin.Value < 0)
            {
                tkBarCannyMin.Value = 0;
            }
            uiLabel_Cs3.Text = tkBarCannyMin.Value.ToString();
        }

        private void tkBarCannyMax_Scroll(object sender, EventArgs e)
        {
            if (tkBarCannyMax.Value < 0)
            {
                tkBarCannyMax.Value = 0;
            }
            uiLabel_Cs4.Text = tkBarCannyMax.Value.ToString();
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            Canny();
        }
    }
}

五、资料

xaiqpl的博客  https://blog.csdn.net/xaiqpl/article/details/118597812?spm=1001.2014.3001.5502
上一篇下一篇

猜你喜欢

热点阅读