目录
- 功能演示
 - 一、编程环境
 - 二、使用步骤
 - 1.程序逻辑
 - 2.图像转二值图像
 - 3.二值图像轮廓发现
 - 4.根据界面的设置,绘制符合标准的轮廓
 - 三 、完整代码演示
 
功能演示
实现模板:
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);
        }
          
			
	声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
		
评论(0)