日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

opencvsharp瑕疵檢測的實現示例_C#教程

作者:工控cc ? 更新時間: 2022-06-30 編程語言

功能演示

實現模板:

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

欄目分類
最近更新