C#图像处理OpenCV开发指南(CVStar,09)——边缘识别之Scharr算法的实例代码

本文主要是介绍C#图像处理OpenCV开发指南(CVStar,09)——边缘识别之Scharr算法的实例代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1 边缘识别之Scharr算法

算法文章很多,不再论述。

1.1  函数原型

void Cv2.Scharr(src,dst,ddepth,dx,dy,scale,delta,borderType)
 

1.2 参数说明

  • src 代表原始图像。
  • dst 代表目标图像。
  • ddepth 代表输出图像的深度。CV_16S
  • dx 代表x方向上的求导阶数。
  • dy 代表y方向上的求导阶数。
  • scale 代表计算导数值时所采用的缩放因子,默认情况下该值是1,是没有缩放的。
  • delta 代表加在目标图像dst上的值,该值是可选的,默认为0。
  • borderType 代表边界样式。

2 核心代码

2.1 Scharr核心代码

public partial class CVUtility
{/// <summary>/// Scharr 边缘检测/// </summary>/// <param name="src"></param>/// <returns></returns>public static Mat Scharr(Mat src){// void Cv2.Scharr(src,dst,ddepth,dx,dy,scale,delta,borderType)// src 代表原始图像。// dst 代表目标图像。// ddepth 代表输出图像的深度。CV_16S// dx 代表x方向上的求导阶数。// dy 代表y方向上的求导阶数。// scale 代表计算导数值时所采用的缩放因子,默认情况下该值是1,是没有缩放的。// delta 代表加在目标图像dst上的值,该值是可选的,默认为0。// borderType 代表边界样式。Mat scharrx = new Mat();Cv2.Scharr(src: src,dst: scharrx,ddepth: MatType.CV_64F,xorder: 1,yorder: 0,scale: 1,delta: 0,borderType: BorderTypes.Default);Mat scharry = src.Scharr(MatType.CV_64F, 0, 1);Cv2.Scharr(src: src,dst: scharry,ddepth: MatType.CV_64F,xorder: 0,yorder: 1,scale: 1,delta: 0,borderType: BorderTypes.Default);Cv2.ConvertScaleAbs(scharrx, scharrx);Cv2.ConvertScaleAbs(scharry, scharry);Mat scharrxy = new Mat(scharrx.Size(), scharrx.Type());Cv2.AddWeighted(src1: scharrx,alpha: 0.5,src2: scharry,beta: 0.5,gamma: 0.0,dst: scharrxy,dtype: -1);return scharrxy;}
}

2.2 Scharr函数的使用

private void Scharr(object? sender, EventArgs? e)
{if (txtKSize.Text.Trim().Length < 1) { MessageBox.Show("KSize Required!"); return; }if (!int.TryParse(txtKSize.Text.Trim(), out int ksize)) { MessageBox.Show("Invalid KSize number!"); return; }if (ksize < 3 || ksize > 100) { MessageBox.Show("Invalid KSize number!"); return; }if ((ksize % 2) != 1) { MessageBox.Show("Odd number required for ksize!"); return; }Mat src = Cv2.ImRead(sourceImage);Mat dst = CVUtility.Scharr(src);picResult.Image = CVUtility.Mat2Bitmap(dst);PicAutosize(picResult);
}

2.3 完整Form1.cs

using OpenCvSharp;#pragma warning disable CS8602namespace Legal.Truffer.CVStar
{public partial class Form1 : Form{string[] ImgExtentions = {"*.*|*.*","JPEG|*.jpg;*.jpeg","GIF|*.gif","PNG|*.png","TIF|*.tif;*.tiff","BMP|*.bmp"};private int original_width { get; set; } = 0;private int original_height { get; set; } = 0;private string sourceImage { get; set; } = "";Panel? panelTop { get; set; } = null;Panel? panelBotton { get; set; } = null;PictureBox? picSource { get; set; } = null;PictureBox? picResult { get; set; } = null;Button? btnLoad { get; set; } = null;Button? btnSave { get; set; } = null;Button? btnFunction { get; set; } = null;Label? abKSize { get; set; } = null;TextBox? txtKSize { get; set; } = null;public Form1(){InitializeComponent();this.Text = "OPENCV C#编程入手教程 POWERED BY 深度混淆(CSDN.NET)";this.StartPosition = FormStartPosition.CenterScreen;GUI();this.Resize += FormResize;}private void FormResize(object? sender, EventArgs? e){if (this.Width < 200) { this.Width = 320; return; }if (this.Height < 200) { this.Height = 320; return; }GUI();}private void GUI(){if (panelTop == null) panelTop = new Panel();panelTop.Parent = this;panelTop.Top = 5;panelTop.Left = 5;panelTop.Width = this.Width - 26;panelTop.Height = 85;panelTop.BorderStyle = BorderStyle.FixedSingle;panelTop.BackColor = Color.FromArgb(200, 200, 255);if (panelBotton == null) panelBotton = new Panel();panelBotton.Parent = this;panelBotton.Top = panelTop.Top + panelTop.Height + 3;panelBotton.Left = 5;panelBotton.Width = panelTop.Width;panelBotton.Height = this.Height - panelBotton.Top - 55;panelBotton.BorderStyle = BorderStyle.FixedSingle;if (picSource == null) picSource = new PictureBox();picSource.Parent = panelBotton;picSource.Left = 5;picSource.Top = 5;picSource.Width = (panelBotton.Width - 10) / 2;picSource.Height = (panelBotton.Height - 10);picSource.BorderStyle = BorderStyle.FixedSingle;if (picResult == null) picResult = new PictureBox();picResult.Parent = panelBotton;picResult.Left = picSource.Left + picSource.Width + 5;picResult.Top = picSource.Top;picResult.Width = picSource.Width;picResult.Height = picSource.Height;picResult.BorderStyle = BorderStyle.FixedSingle;original_width = picSource.Width;original_height = picSource.Height;if (btnLoad == null) btnLoad = new Button();btnLoad.Parent = panelTop;btnLoad.Left = 5;btnLoad.Top = 5;btnLoad.Width = 90;btnLoad.Height = 38;btnLoad.Cursor = Cursors.Hand;btnLoad.Text = "Load";btnLoad.Click += Load_Image;btnLoad.BackColor = Color.LightCoral;if (btnSave == null) btnSave = new Button();btnSave.Parent = panelTop;btnSave.Left = panelTop.Width - btnSave.Width - 25;btnSave.Top = btnLoad.Top;btnSave.Width = 90;btnSave.Height = 38;btnSave.Cursor = Cursors.Hand;btnSave.Text = "Save";btnSave.Click += Save;btnSave.BackColor = Color.LightCoral;if (btnFunction == null) btnFunction = new Button();btnFunction.Parent = panelTop;btnFunction.Left = btnLoad.Left + btnLoad.Width + 5;btnFunction.Top = btnLoad.Top;btnFunction.Width = 120;btnFunction.Height = 38;btnFunction.Cursor = Cursors.Hand;btnFunction.Text = "Scharr";btnFunction.Click += Scharr;btnFunction.BackColor = Color.LightCoral;if (abKSize == null) abKSize = new Label();abKSize.Parent = panelTop;abKSize.Left = btnFunction.Left;abKSize.Top = btnFunction.Top + btnFunction.Height + 5;abKSize.Text = "KSIZE: ";if (txtKSize == null) txtKSize = new TextBox();txtKSize.Parent = panelTop;txtKSize.Left = abKSize.Left + abKSize.Width + 5;txtKSize.Top = abKSize.Top;txtKSize.Text = "3";PicAutosize(picSource);PicAutosize(picResult);}private void Load_Image(object? sender, EventArgs? e){OpenFileDialog openFileDialog = new OpenFileDialog();openFileDialog.Filter = String.Join("|", ImgExtentions);if (openFileDialog.ShowDialog() == DialogResult.OK){sourceImage = openFileDialog.FileName;picSource.Image = Image.FromFile(sourceImage);picResult.Image = picSource.Image;PicAutosize(picSource);PicAutosize(picResult);}}private void PicAutosize(PictureBox pb){if (pb == null) return;if (pb.Image == null) return;Image img = pb.Image;int w = original_width;int h = w * img.Height / img.Width;if (h > original_height){h = original_height;w = h * img.Width / img.Height;}pb.SizeMode = PictureBoxSizeMode.Zoom;pb.Width = w;pb.Height = h;pb.Image = img;pb.Refresh();}private void Save(object? sender, EventArgs? e){SaveFileDialog saveFileDialog = new SaveFileDialog();saveFileDialog.Filter = String.Join("|", ImgExtentions);if (saveFileDialog.ShowDialog() == DialogResult.OK){picResult.Image.Save(saveFileDialog.FileName);MessageBox.Show("Image Save to " + saveFileDialog.FileName);}}private void Scharr(object? sender, EventArgs? e){if (txtKSize.Text.Trim().Length < 1) { MessageBox.Show("KSize Required!"); return; }if (!int.TryParse(txtKSize.Text.Trim(), out int ksize)) { MessageBox.Show("Invalid KSize number!"); return; }if (ksize < 3 || ksize > 100) { MessageBox.Show("Invalid KSize number!"); return; }if ((ksize % 2) != 1) { MessageBox.Show("Odd number required for ksize!"); return; }Mat src = Cv2.ImRead(sourceImage);Mat dst = CVUtility.Scharr(src);picResult.Image = CVUtility.Mat2Bitmap(dst);PicAutosize(picResult);}}
}

3 运行效果

实际上一般都用黑白照片。

这篇关于C#图像处理OpenCV开发指南(CVStar,09)——边缘识别之Scharr算法的实例代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

SQLite3命令行工具最佳实践指南

《SQLite3命令行工具最佳实践指南》SQLite3是轻量级嵌入式数据库,无需服务器支持,具备ACID事务与跨平台特性,适用于小型项目和学习,sqlite3.exe作为命令行工具,支持SQL执行、数... 目录1. SQLite3简介和特点2. sqlite3.exe使用概述2.1 sqlite3.exe

OpenCV实现实时颜色检测的示例

《OpenCV实现实时颜色检测的示例》本文主要介绍了OpenCV实现实时颜色检测的示例,通过HSV色彩空间转换和色调范围判断实现红黄绿蓝颜色检测,包含视频捕捉、区域标记、颜色分析等功能,具有一定的参考... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

使用Python开发一个现代化屏幕取色器

《使用Python开发一个现代化屏幕取色器》在UI设计、网页开发等场景中,颜色拾取是高频需求,:本文主要介绍如何使用Python开发一个现代化屏幕取色器,有需要的小伙伴可以参考一下... 目录一、项目概述二、核心功能解析2.1 实时颜色追踪2.2 智能颜色显示三、效果展示四、实现步骤详解4.1 环境配置4.

Java中Map.Entry()含义及方法使用代码

《Java中Map.Entry()含义及方法使用代码》:本文主要介绍Java中Map.Entry()含义及方法使用的相关资料,Map.Entry是Java中Map的静态内部接口,用于表示键值对,其... 目录前言 Map.Entry作用核心方法常见使用场景1. 遍历 Map 的所有键值对2. 直接修改 Ma

从基础到进阶详解Pandas时间数据处理指南

《从基础到进阶详解Pandas时间数据处理指南》Pandas构建了完整的时间数据处理生态,核心由四个基础类构成,Timestamp,DatetimeIndex,Period和Timedelta,下面我... 目录1. 时间数据类型与基础操作1.1 核心时间对象体系1.2 时间数据生成技巧2. 时间索引与数据

Python基于微信OCR引擎实现高效图片文字识别

《Python基于微信OCR引擎实现高效图片文字识别》这篇文章主要为大家详细介绍了一款基于微信OCR引擎的图片文字识别桌面应用开发全过程,可以实现从图片拖拽识别到文字提取,感兴趣的小伙伴可以跟随小编一... 目录一、项目概述1.1 开发背景1.2 技术选型1.3 核心优势二、功能详解2.1 核心功能模块2.