網站首頁 編程語言 正文
功能演示
實現模板:
1.檢測這板件面的凹坑 ,并把這些凹坑繪制出來
2.界面上可以選擇,標注面積大于指定值 的凹坑
測試圖像
面積小于10個像素凹坑標注
面積小于40個像素凹坑標注
提示:以下是本篇文章正文內容,下面案例可供參考
一、編程環境
C#2015+opencvsharp4.0
二、使用步驟
1.程序邏輯
1.先將圖像高斯雙邊濾波 ;
代碼如下(示例):
Cv2.BilateralFilter(imageorg, gs, 0, 20, 5); //高斯雙邊模糊
//imageorg為源圖 Mat;
//gs是濾波后 Mat;
高斯雙邊濾波后的圖像
2.圖像轉二值圖像
//先轉灰度圖像
Cv2.CvtColor(gs, image_gray,ColorConversionCodes.RGB2GRAY);
//在轉二值圖像
Cv2.Threshold(image_gray, bin, 100, 255, ThresholdTypes.BinaryInv);
二值圖像
3.二值圖像輪廓發現
//發現輪廓
OpenCvSharp.Point[][] contours2;
HierarchyIndex[] hierarchy2;
Cv2.FindContours(bin, out contours2, out hierarchy2, RetrievalModes.External, ContourApproximationModes.ApproxNone);
4.根據界面的設置,繪制符合標準的輪廓
//繪制輪廓
for (int i = 0; i < contours2.Length; i++)
{
double size = Cv2.ArcLength(contours2[i], true);
if(size > Double.Parse(textBox1.Text))
Cv2.DrawContours(imageorg, contours2,i, new Scalar(0, 0, 255), 3);
}
5.顯示最終圖像
//顯示
Bitmap bitmap = BitmapConverter.ToBitmap(gs);
pictureBox1.Image = bitmap;
Cv2.ImWrite("12.jpg", imageorg);
三 、完整代碼演示
private void button6_Click(object sender, EventArgs e)
{
Mat imageorg = Cv2.ImRead("E:\\CS學習\\opencvsharp2\\opencvsharp2\\data9.jpg");
Mat image_gray = new Mat();
Mat gs = new Mat();
Mat bin=new Mat();
Cv2.BilateralFilter(imageorg, gs, 0, 20, 5); //高斯雙邊模糊
//圖紙轉換
Cv2.CvtColor(gs, image_gray,ColorConversionCodes.RGB2GRAY);
Cv2.Threshold(image_gray, bin, 100, 255, ThresholdTypes.BinaryInv);
//發現輪廓
OpenCvSharp.Point[][] contours2;
HierarchyIndex[] hierarchy2;
Cv2.FindContours(bin, out contours2, out hierarchy2, RetrievalModes.External, ContourApproximationModes.ApproxNone);
//繪制指定輪廓
for (int i = 0; i < contours2.Length; i++)
{
double size = Cv2.ArcLength(contours2[i], true);
if(size > Double.Parse(textBox1.Text))
Cv2.DrawContours(imageorg, contours2,i, new Scalar(0, 0, 255), 3);
}
//顯示
Bitmap bitmap = BitmapConverter.ToBitmap(imageorg);
pictureBox1.Image = bitmap;
Cv2.ImWrite("12.jpg", bin);
}
原文鏈接:https://blog.csdn.net/qq_42756257/article/details/121884255
相關推薦
- 2022-05-18 Python3的正則表達式詳解_python
- 2022-06-24 C#中緩存System.Web.Caching用法總結_C#教程
- 2022-08-07 shell進度條追蹤指令執行時間的場景分析_linux shell
- 2022-08-16 Containerd容器運行yum安裝與二進制安裝_云其它
- 2022-02-17 在react中使用antv g2繪制帶有sider滑塊的chart圖表
- 2022-03-31 C語言中冒泡排序算法詳解_C 語言
- 2022-05-19 pytorch中的?.view()函數的用法介紹_python
- 2023-01-15 Go語言使用組合的思想實現繼承_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支