opencvsharp瑕疵检测的实现示例
更新时间:2022年05月06日 10:05:31 作者:工控cc
本文主要介绍了opencvsharp瑕疵检测的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
功能演示
实现模板:
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); }
到此这篇关于opencvsharp瑕疵检测的实现示例的文章就介绍到这了,更多相关opencvsharp瑕疵检测内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
C#调用海康工业相机SDK采集图像并在Halcon窗口中显示方式
这篇文章主要介绍了C#调用海康工业相机SDK采集图像并在Halcon窗口中显示方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2023-02-02C#基于HttpWebRequest实现发送HTTP请求的方法分析
这篇文章主要介绍了C#基于HttpWebRequest实现发送HTTP请求的方法,结合实例形式分析了C#使用HttpWebRequest类与System.IO类实现发送HTTP请求相关操作技巧与注意事项,需要的朋友可以参考下2019-02-02
最新评论