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 運行效果

實際上一般都用黑白照片。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/212640.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/212640.shtml
英文地址,請注明出處:http://en.pswp.cn/news/212640.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

uniApp應用軟件在運行時,不符合華為應用市場審核標準。解決方案合集!

&#xff08;暫時用不到的也建議收藏一下&#xff0c;因為文章持續更新中&#xff09; 最新更改時間&#xff1a;20023-12-10 第一次做App應用開發相信大家一定都遇到過華為應用市場審核的“駁回”&#xff01; 有些問題一看就明白可以立馬修改&#xff0c;而有一些問題修改意…

Dubbo入門直接上手,結合微服務詳解

Dubbo 高性能、輕量級的 Java RPC 框架 RPC&#xff1a; Remote Procedure Call 遠程過程調用&#xff0c;簡單來說就是它允許一個計算機程序通過網絡請求調用另一個計算機上的程序&#xff0c;就像本地調用一樣。有非常多的協議和技術來都實現了RPC的過程&#xff0c;比如&a…

Elasticsearch 8.9 refresh刷Es緩沖區的數據到Lucene,更新segemnt,使數據可見

一、相關API的handler1、接受HTTP請求的hander(RestRefreshAction)2、往數據節點發送刷新請求的action(TransportRefreshAction)3、數據節點接收主節點refresh傳輸的action(TransportShardRefreshAction) 二、在IndexShard執行refresh操作1、根據入參決定是使用lucene提供的阻塞…

【華為數據之道學習筆記】3-8以確保合規遵從為核心的外部數據管理

一、以確保合規遵從為核心的外部數據管理 外部數據是指華為公司引入的外部組織或者個人擁有處置權利的 數據&#xff0c;如供應商資質證明、消費者洞察報告等。外部數據治理的出發點是合規遵從優先&#xff0c;與內部數據治理的目的不同。 外部數據的治理主要遵循以下原則。 1&…

【設計模式--創建型--原型模式】

設計模式--創建型--原型模式 原型模式概述結構實現結果 案例代碼結果使用場景 擴展&#xff08;深\淺克隆&#xff09;淺克隆演示&#xff1a;結果&#xff1a;使用深克隆&#xff08;利用對象流&#xff09;結果 原型模式 概述 用一個已經創建的實例作為原型&#xff0c;通過…

Go簡單了解

0.一直很好奇,go是不是像傳說中的速度快,解決了多線程問題,快速進行了解了解,和java進行對比,他是怎么解決語言發展的問題的…,所有語言都是差不多的,只是熟練程度不同而已 1.go圖標是土撥鼠,2009發行 docker使用go,解決了并發問題 google facebook 騰訊 百度 七牛云 京東 小米…

Spring Cloud Gateway + Nacos + LoadBalancer實現企業級網關

1. Spring Cloud Gateway 整合Nacos、LoadBalancer 實現企業級網關 前置工作&#xff1a; 創建 SpringBoot 多模塊項目創建網關&#xff08;gateway-service&#xff09;、用戶&#xff08;user-service&#xff09;模塊用戶模塊添加 Nacos discovery 支持以及 Spring Web&am…

gitbash下載安裝

參考教程 零、下載 官網地址 2.43.0win64 鏈接&#xff1a;https://pan.baidu.com/s/16urs_nmky7j20-qNzUTTkg 提取碼&#xff1a;7jaq 一、安裝 圖標組件&#xff08;Additional icons&#xff09;&#xff1a;選擇是否創建桌面快捷方式&#xff1b;桌面瀏覽&#xff08;Win…

設計模式--命令模式的簡單例子

引入&#xff1a;以一個對數組的增刪改查為例。通過命令模式可以對數組進行增刪改查以及撤銷回滾。 一、基本概念 命令模式有多種分法&#xff0c;在本文中主要分為CommandMgr、Command、Receiver. CommandMgr主要用于控制命令執行等操作、Command為具體的命令、Receiver為命…

逸迅科技丁紅陽:三種能力幫助企業打造GBI “護城河”

大數據產業創新服務媒體 ——聚焦數據 改變商業 近日&#xff0c;由上海市經濟和信息化委員會、上海市科學技術委員會指導&#xff0c;數據猿與上海大數據聯盟聯合主辦的“2023企業數智化轉型升級發展論壇”在上海舉行。本次論壇以“釋放數字價值驅動智能升級”為主題&#xf…

piakachu越權漏洞

水平越權 首先打開這一關&#xff0c;在右側有一些提示&#xff0c;我們可以看到 然后我們隨便輸入一組信息即可&#xff0c;可以在url中看到這樣的字段 當我們嘗試在url中直接更換另一個用戶名時可以發現&#xff0c;直接切換到了另一個用戶的身份 垂直越權 這里可以看到右邊…

QML和C++交互中,實現C++中connect到qml的信號,再從qml發射信號傳遞數據給C++的一種方式

1.需求&#xff1a; 假設我們有一個需求&#xff0c;要求在用戶點擊列表中的項目時&#xff0c;不僅在控制臺上輸出項目的名稱&#xff0c;還要在C端進行一些處理。我們希望在C端能夠接收到用戶點擊的項目名稱&#xff0c;并進行相應的處理。 2.分析&#xff1a; 在這種情況…

Android 10.0 系統framework修改低電量關機值為2%

1.前言 在10.0的系統產品開發中,在系統關于低電量關機的值,每個平臺都不同,根據實際開發底層硬件的要求看實際情況來調整這個值, 所以需要分析相關的電量變化執行的代碼流程,來實現這個功能 2.系統framework修改低電量關機值為2%的核心類 frameworks\base\services\cor…

一文學會使用 PyInstaller 將 Python 腳本打包為 .exe 可執行文件

文章目錄 前言PyInstaller特點跨平臺支持自動依賴項處理單文件發布支持圖形用戶界面&#xff08;GUI&#xff09;和命令行界面&#xff08;CLI&#xff09;應用支持多種打包選項 基本用法常用參數其它參數 版本 & 環境實現步驟安裝 PyInstaller創建 Python 腳本使用 PyInst…

Strange-Towers-of-Hanoi

title: Strange Towers of Hanoi date: 2023-12-11 03:20:05 tags: 遞推 categories: 算法進階指南 題目大意 解出 n n n 個盒子 4 4 4 座塔的漢諾塔問題最少需要多少次&#xff1f; 思路 首先考慮 n n n 個盒子 3 3 3 座塔的經典漢諾塔問題&#xff0c;設 d [ n ] d[n] …

第三十章 控制到 XML 模式的映射 - Array of Classname

文章目錄 第三十章 控制到 XML 模式的映射 - Array of ClassnameArray of Classname 第三十章 控制到 XML 模式的映射 - Array of Classname Array of Classname 本部分顯示了從啟用 XML 的類生成的XML 架構的一部分&#xff0c;此時該類包含定義為類名數組的屬性。例如&…

【SpringBoot教程】SpringBoot 創建定時任務(配合數據庫動態執行)

作者簡介&#xff1a;大家好&#xff0c;我是擼代碼的羊駝&#xff0c;前阿里巴巴架構師&#xff0c;現某互聯網公司CTO 聯系v&#xff1a;sulny_ann&#xff08;17362204968&#xff09;&#xff0c;加我進群&#xff0c;大家一起學習&#xff0c;一起進步&#xff0c;一起對抗…

transformer模型結構|李宏毅機器學習21年

來源&#xff1a;https://www.bilibili.com/video/BV1Bb4y1L7FT?p4&vd_sourcef66cebc7ed6819c67fca9b4fa3785d39 文章目錄 概述seq2seqtransformerEncoderDecoderAutoregressive&#xff08;AT&#xff09;self-attention與masked-self attentionmodel如何決定輸出的長度…

【親測有效】支持橫豎屏 微信小程序video禁止進度條拖動,微信小程序遮罩進度條,

背景&#xff1a;部分課程禁止客戶拖動視頻進度條直至播放結束 紅色是遮罩區域遮罩區域 實際遮罩效果&#xff08;有一個很淺的陰影區域&#xff09; 實現代碼 .wxml文件 <video enable-progress-gesture"false" ><cover-view class"cover">…

基于深度學習的yolov7植物病蟲害識別及防治系統

歡迎大家點贊、收藏、關注、評論啦 &#xff0c;由于篇幅有限&#xff0c;只展示了部分核心代碼。 文章目錄 一項目簡介簡介YOLOv7 系統特性工作流程 二、功能三、系統四. 總結 一項目簡介 # YOLOv7植物病蟲害識別及防治系統介紹 簡介 該系統基于深度學習技術&#xff0c;采…