C#:用OpenCV实现缺陷检测

2023-02-17  本文已影响0人  大龙10

一、简介

二、FindContours函数

void findContours( InputOutputArray image, 
      OutputArrayOfArrays contours,
      OutputArray hierarchy, int mode,
      int method, Point offset = Point());

三、检测条件

先进行二值化、高斯滤波、平滑等处理。再进行轮廓分析。

四、程序源码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
using OpenCvSharp;
using OpenCvSharp.Extensions;

namespace Ky_FindContours
{
    public partial class Form1 : UIForm
    {
        public Form1()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint, true);

        }
        private Image image = null;
        private Mat dst = new Mat();
        private Mat src_img;
        string filePath = "";
        private List<Mat> reList = new List<Mat>();
        private int step = 1;

        private void openImage_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "选择操作的图片";
            openFileDialog.Filter = "图片 *.jpg|*.jpg|图像*.png|*.png";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                filePath = openFileDialog.FileName;
                image = Image.FromFile(filePath);
                src_img = Cv2.ImRead(filePath);
                Mat tem1 = new Mat();
                src_img.CopyTo(tem1);
                if (reList.Count > 0)
                {
                    reList[0] = tem1;

                }
                else
                {
                    reList.Add(tem1);
                }

            }
            if (filePath != "")
            {
                picBoxShowDel.Image = image;
                picShowOri.Image = image;
            }

        }

        /// <summary>
        /// 脏污缺陷检测
        /// </summary>
        /// <param name="img">测试图像</param>
        /// <returns>结果图</returns> //也可设置bool类型表示OK或NG
        static Mat DirtyDetection(Mat img)
        {
            Mat result = img.Clone();
            Mat gray = new Mat();
            Cv2.CvtColor(img, gray, ColorConversionCodes.BGR2GRAY);
            Cv2.GaussianBlur(gray, gray, new OpenCvSharp.Size(7, 7), 0);
            Cv2.Canny(gray, gray, 10, 30);
            OpenCvSharp.Point[][] contours; //轮廓查找结果变量
            HierarchyIndex[] hierarchy; //轮廓拓扑结构变量

            Cv2.FindContours(gray, out contours, out hierarchy, RetrievalModes.External,
                            ContourApproximationModes.ApproxNone);
            //Console.WriteLine("contour_size = {0}", contours.Length); //输出轮廓个数

            for (int i = 0; i < contours.Length; i++)
            {
                double length = Cv2.ArcLength(contours[i], true);
                if (length >= 1)
                    Cv2.DrawContours(result, contours, i, new Scalar(0, 0, 255), 2);
            }
            return result;
        }

        /// <summary>
        /// 油污缺陷检测
        /// </summary>
        /// <param name="img">测试图像</param>
        /// <returns>结果图</returns> //也可设置bool类型表示OK或NG
        static Mat OilDetection(Mat img)
        {
            Mat result = img.Clone();
            Mat imgLab = new Mat();
            Cv2.CvtColor(img, imgLab, ColorConversionCodes.BGR2Lab);
            Mat[] labArray = Cv2.Split(imgLab); //L, a, b
            Mat blur = new Mat();
            Mat thres = new Mat();
            Cv2.GaussianBlur(labArray[2], blur, new OpenCvSharp.Size(3, 3), 0); //b通道
            Cv2.Threshold(blur, thres, 130, 255, ThresholdTypes.Binary);
            Mat element = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(3, 3), new OpenCvSharp.Point(-1, -1));
            Cv2.MorphologyEx(thres, thres, MorphTypes.Open, element, new OpenCvSharp.Point(-1, -1), 1,
                             BorderTypes.Default, new Scalar());

            OpenCvSharp.Point[][] contours; //轮廓查找结果变量
            HierarchyIndex[] hierarchy; //轮廓拓扑结构变量

            Cv2.FindContours(thres, out contours, out hierarchy, RetrievalModes.External,
                            ContourApproximationModes.ApproxNone);
            //Console.WriteLine("contour_size = {0}", contours.Length); //输出轮廓个数

            for (int i = 0; i < contours.Length; i++)
            {
                double area = Cv2.ContourArea(contours[i]);
                if (area >= 50)
                    Cv2.DrawContours(result, contours, i, new Scalar(0, 0, 255), 2);
            }
            return result;
        }

        /// <summary>
        /// 线条破损缺陷检测
        /// </summary>
        /// <param name="img">测试图像</param>
        /// <returns>结果图</returns> //也可设置bool类型表示OK或NG
        static Mat LineDefectDetection(Mat img)
        {
            Mat result = img.Clone();
            Mat imgLab = new Mat();
            Cv2.CvtColor(img, imgLab, ColorConversionCodes.BGR2Lab);
            Mat[] labArray = Cv2.Split(imgLab); //L, a, b
            Mat blur = new Mat();
            Mat edged = new Mat();
            Cv2.GaussianBlur(labArray[2], blur, new OpenCvSharp.Size(3, 3), 0); //b通道
            Cv2.Canny(blur, edged, 5, 10);
            OpenCvSharp.Point[][] contours; //轮廓查找结果变量
            HierarchyIndex[] hierarchy; //轮廓拓扑结构变量

            Cv2.FindContours(edged, out contours, out hierarchy, RetrievalModes.External,
                            ContourApproximationModes.ApproxNone);
            //Console.WriteLine("contour_size = {0}", contours.Length); //输出轮廓个数

            for (int i = 0; i < contours.Length; i++)
            {
                double length = Cv2.ArcLength(contours[i], true);
                if (length >= 10)
                    Cv2.DrawContours(result, contours, i, new Scalar(0, 0, 255), 2);
            }
            return result;
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            Mat zw_result = DirtyDetection(src_img); //脏污缺陷检测
            picBoxShowDel.Image = zw_result.ToBitmap();
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            Mat yw_result = OilDetection(src_img); //油污缺陷检测
            picBoxShowDel.Image = yw_result.ToBitmap();
        }

        private void uiButton3_Click(object sender, EventArgs e)
        {
            Mat yw_result = LineDefectDetection(src_img); //线条破损缺陷检测
            picBoxShowDel.Image = yw_result.ToBitmap();
        }
    }
}

油污检测

五、参考资料

博客:https://blog.51cto.com/stq054188/5543992
来源公众号:OpenCV与AI深度学习
上一篇下一篇

猜你喜欢

热点阅读