Skip to content

视觉整理

约 465 字大约 2 分钟

2024-08-29

一、OpenCV

  1. HSV 颜色筛选
{
    //using (Mat img = Cv2.ImDecode(image, ImreadModes.Color))
    using (Mat img = Cv2.ImRead("图片地址"))//@"locate/locate2.jpg"
    using (Mat hsv = new Mat())
    using (Mat fliter = new Mat())
    using (Mat grayimg = new Mat())
    using (Mat binaryimg = new Mat())
    using (Mat openimg = new Mat())
    {
        Cv2.CvtColor(img, hsv, ColorConversionCodes.BGR2HSV);
        Scalar low = new Scalar(110, 8, 40), up = new Scalar(360, 255, 255);
        Cv2.InRange(hsv, low, up, fliter);

        Mat blueRegion = new Mat();
        Cv2.BitwiseAnd(img, img, blueRegion, fliter);

        Cv2.CvtColor(blueRegion, grayimg, ColorConversionCodes.BGR2GRAY);
        //Cv2.ImShow("grayimg", grayimg);

        Cv2.Threshold(grayimg, binaryimg, 50, 255, ThresholdTypes.Binary);
        //Cv2.ImShow("binaryimg", binaryimg);

        Cv2.MorphologyEx(binaryimg, openimg, MorphTypes.Open, kernel, iterations: 4);
        //Cv2.ImShow("openimg", openimg);


        //Cv2.WaitKey(0);
        //Cv2.DestroyAllWindows();

        Point[][] contours;
        Cv2.FindContours(openimg, out contours, out _, RetrievalModes.External, ContourApproximationModes.ApproxSimple, null);
        if (contours != null && contours.Length == 0) { return (null, new List<System.Drawing.Point>()); }

        var contour = contours.First(t => t.Length == contours.Max(m => m.Length));
        Rect rect = Cv2.BoundingRect(contour);
        //var points = GenerateFocusForRect(rect);
        double area = Cv2.ContourArea(contour);
        if (area < 50000) return (null, new List<System.Drawing.Point>());
        var r = new System.Drawing.Rectangle(rect.X, rect.Y, rect.Width, rect.Height);
        return r;
    }
}
  1. BGR2Lab 拆分颜色通道再合并
{
    using (Mat resourceimg = Cv2.ImRead("图片地址"))//@"locate/locate2.jpg"
    using (Mat lab = new Mat())
    using (Mat binary = new Mat())
    using (Mat binaryInv = new Mat())
    using (Mat commonRegions = new Mat())
    using (Mat openimg = new Mat())
    {
        Rect roi = slideAreaRect; //Rect 截图图片的区域
        using (Mat img = new Mat(resourceimg, roi))
        {
            Cv2.CvtColor(img, lab, ColorConversionCodes.BGR2Lab);//lab光照影响小
            var channels = lab.Split();
            channels[0].Dispose();
            Cv2.Threshold(channels[1], binary, 0, 255, ThresholdTypes.Binary | ThresholdTypes.Otsu);
            Cv2.Threshold(channels[2], binaryInv, 0, 255, ThresholdTypes.BinaryInv | ThresholdTypes.Otsu);
            Cv2.BitwiseAnd(binary, binaryInv, commonRegions);//取相同部位
            Cv2.MorphologyEx(commonRegions, openimg, MorphTypes.Open, kernel);
            channels[1].Dispose(); channels[2].Dispose();
            Point[][] contours;
            Cv2.FindContours(openimg, out contours, out _, RetrievalModes.External, ContourApproximationModes.ApproxSimple, null);
            if (contours != null && contours.Length == 0) { return (null, new List<System.Drawing.Point>()); }

            var contour = contours.First(t => t.Length == contours.Max(m => m.Length));
            Rect rect = Cv2.BoundingRect(contour);
            rect.X += slideAreaRect.X;
            rect.Y += slideAreaRect.Y;
            double area = Cv2.ContourArea(contour);
            if (area < 50000) return (null, new List<System.Drawing.Point>());
            //var points = GenerateFocusForRect(rect);
            var r = new System.Drawing.Rectangle(rect.X, rect.Y, rect.Width, rect.Height);
            return r;
        }
    }
}

还可以通过 拉普拉斯、直方图等方式试试

二、Yolov5 模型训练

yolo仓库: ultralytics/yolov5: YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite (github.com)

流程:

  1. 安装Conda基础环境 https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe

    • Conda 类似docker的基础容器,可在内部安装指定版本python等
  2. 数据标记工具

    • labelimg
  3. 训练(python-yolo train.py

  4. 识别(python-yolo detect.py

详细参考: Yolo | 记录 (cqdefang.com)