opencv HSV;OpenCvSharp HSV ;opencv 颜色识别;OpenCvSharp 颜色识别;C# opencv HSV

opencv HSV;OpenCvSharp HSV ;opencv 颜色识别;OpenCvSharp 颜色识别;C# opencv HSV

源码下载地址:https://download.csdn.net/download/TangLingBo/12593161

按照10种颜色来划分,10种颜色为一级,二级颜色需要自己去定义,可也知道各种颜色占的百分百比。

可也全图解析颜色,和选择一个区域解析颜色。

效果图:

opencv HSV;OpenCvSharp HSV ;opencv 颜色识别;OpenCvSharp 颜色识别;C# opencv HSV_第1张图片

opencv HSV;OpenCvSharp HSV ;opencv 颜色识别;OpenCvSharp 颜色识别;C# opencv HSV_第2张图片

opencv HSV;OpenCvSharp HSV ;opencv 颜色识别;OpenCvSharp 颜色识别;C# opencv HSV_第3张图片

核心代码:

            Mat srcImg =new Mat(strImg1, ImreadModes.Color);
           
            Mat imgHSV=new Mat();
            string strType =cboType.Text.Trim();
            ColorConversionCodes colorType = (ColorConversionCodes)Enum.Parse(typeof(ColorConversionCodes), strType);
            Cv2.CvtColor(srcImg, imgHSV, colorType); //Convert the captured frame from BGR to HSV

         
            //不需要
            因为我们读取的是彩色图,直方图均衡化需要在HSV空间做
            //Mat[] hsvSplit= Cv2.Split(imgHSV);
            //Cv2.EqualizeHist(hsvSplit[2], hsvSplit[2]);
            //Cv2.Merge(hsvSplit, imgHSV);

            Mat imgThresholded=new Mat();

            Cv2.InRange(imgHSV,new Scalar(iLowH, iLowS, iLowV), new Scalar(iHighH, iHighS, iHighV), imgThresholded); //Threshold the image

            if (checkBox1.Checked)
            {
                //开操作 (去除一些噪点)
                Mat element = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(5, 5));
                Cv2.MorphologyEx(imgThresholded, imgThresholded, MorphTypes.Open, element);

                //闭操作 (连接一些连通域)
                Cv2.MorphologyEx(imgThresholded, imgThresholded, MorphTypes.Close, element);
            }
            //输出图像分配内存
            Mat dst = null;
            if (checkBox2.Checked)
            {
                dst = new Mat(srcImg.Size(), srcImg.Type());
                //掩模到原图的转换
                for (int r = 0; r < srcImg.Rows; r++)
                {
                    for (int c = 0; c < srcImg.Cols; c++)
                    {
                        if (imgThresholded.At(r, c) == 255)
                        {
                            Vec3b vec3B = srcImg.At(r, c);
                            if (vec3B.Item0==0&& vec3B.Item1==0&& vec3B.Item2==0)
                            {
                                vec3B.Item0 = 255;
                                vec3B.Item1 = 255;
                                vec3B.Item2 = 255;
                            }
                            dst.Set(r, c, vec3B);
                        }
                    }
                }
            }
            else
            {
                dst = imgThresholded;
            }

            pictureBox3.BackgroundImage = BitmapConverter.ToBitmap(imgHSV);
            pictureBox2.BackgroundImage = BitmapConverter.ToBitmap(dst);

源码下载地址:https://download.csdn.net/download/TangLingBo/12593161​​​​​​​

你可能感兴趣的:(opencv,OpenCvSharp,C#,opencv,opencv,HSV,OpenCvSharp,HSV,C#,opencv,HSV,OpenCvSharp颜色识别,Opencv颜色识别)