好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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瑕疵检测内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

查看更多关于opencvsharp瑕疵检测的实现示例的详细内容...

  阅读:48次