opencvsharp瑕疵檢測的實現(xiàn)示例
功能演示
實現(xiàn)模板:
1.檢測這板件面的凹坑 ,并把這些凹坑繪制出來
2.界面上可以選擇,標(biāo)注面積大于指定值 的凹坑

測試圖像

面積小于10個像素凹坑標(biāo)注

面積小于40個像素凹坑標(biāo)注
提示:以下是本篇文章正文內(nèi)容,下面案例可供參考
一、編程環(huán)境
C#2015+opencvsharp4.0
二、使用步驟
1.程序邏輯
1.先將圖像高斯雙邊濾波 ;
代碼如下(示例):
Cv2.BilateralFilter(imageorg, gs, 0, 20, 5); //高斯雙邊模糊 //imageorg為源圖 Mat; //gs是濾波后 Mat;

高斯雙邊濾波后的圖像
2.圖像轉(zhuǎn)二值圖像
//先轉(zhuǎn)灰度圖像 Cv2.CvtColor(gs, image_gray,ColorConversionCodes.RGB2GRAY); //在轉(zhuǎn)二值圖像 Cv2.Threshold(image_gray, bin, 100, 255, ThresholdTypes.BinaryInv);

二值圖像
3.二值圖像輪廓發(fā)現(xiàn)
//發(fā)現(xiàn)輪廓
OpenCvSharp.Point[][] contours2;
HierarchyIndex[] hierarchy2;
Cv2.FindContours(bin, out contours2, out hierarchy2, RetrievalModes.External, ContourApproximationModes.ApproxNone);
4.根據(jù)界面的設(shè)置,繪制符合標(biāo)準(zhǔn)的輪廓
//繪制輪廓
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學(xué)習(xí)\\opencvsharp2\\opencvsharp2\\data9.jpg");
Mat image_gray = new Mat();
Mat gs = new Mat();
Mat bin=new Mat();
Cv2.BilateralFilter(imageorg, gs, 0, 20, 5); //高斯雙邊模糊
//圖紙轉(zhuǎn)換
Cv2.CvtColor(gs, image_gray,ColorConversionCodes.RGB2GRAY);
Cv2.Threshold(image_gray, bin, 100, 255, ThresholdTypes.BinaryInv);
//發(fā)現(xiàn)輪廓
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);
}
到此這篇關(guān)于opencvsharp瑕疵檢測的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)opencvsharp瑕疵檢測內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#中的自動類型轉(zhuǎn)換和強制類型轉(zhuǎn)換
這篇文章主要介紹了C#中的自動類型轉(zhuǎn)換和強制類型轉(zhuǎn)換,非常不錯,具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-08-08
C#中IsNullOrEmpty和IsNullOrWhiteSpace的使用方法及區(qū)別解析
今天我們將探討C#中兩個常用的字符串處理方法:IsNullOrEmpty和IsNullOrWhiteSpace,本文中,我們將詳細(xì)解釋這兩個方法的功能和使用場景,并幫助您更好地理解它們之間的區(qū)別,本文結(jié)合實例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-07-07
C#中使用DevExpress中的ChartControl實現(xiàn)極坐標(biāo)圖的案例詳解
這篇文章主要介紹了在C#中使用DevExpress中的ChartControl實現(xiàn)極坐標(biāo)圖,本案例是使用的是DevExpress 18.1.3版本,之前在14版本上也試過,但是有一個弊端就是實現(xiàn)極坐標(biāo)圖的時候,第一個點和最后一個點總是自動多一條閉合線,會形成一個閉合的多邊形,因此升級了一下版2022-02-02

