将垫子转换为图像

问题描述

我正在尝试从 List 中获取一个矩形并绘制它。 很明显,矩形的颜色是红色的,但它是用白色打印的。我正在使用 cv2.imshow 检查中间结果,然后它以红色打印。我认为将 mat 更改为位图是错误的。我会附上代码。 请告诉我。谢谢

    public List<Rect> RegionOfInterest(Mat image)
    {
        List<Rect> boundRect = new List<Rect>();
        using (Mat img_gray = new Mat())
        using (Mat img_sobel = new Mat())
        using (Mat img_threshold = new Mat())
        {
            Cv2.CvtColor(image,img_gray,ColorConversionCodes.BGR2GRAY); //GrayScale
            Cv2.Sobel(img_gray,img_sobel,MatType.CV_8U,1,3,BorderTypes.Default); //Sobel Mask
            Cv2.Threshold(img_sobel,img_threshold,255,ThresholdTypes.Otsu | ThresholdTypes.Binary); //Binary
            using (Mat element = Cv2.GetStructuringElement(MorphShapes.Rect,new Size(20,20))) //ROI
            {
                Cv2.MorphologyEx(img_threshold,MorphTypes.Close,element);
                Point[][] edgesArray = img_threshold.Clone().FindContoursAsArray(RetrievalModes.External,ContourApproximationModes.ApproxNone);
                foreach (Point[] edges in edgesArray)
                {
                    Point[] normalizedEdges = Cv2.ApproxPolyDP(edges,17,true);
                    Rect appRect = Cv2.BoundingRect(normalizedEdges);
                    boundRect.Add(appRect);
                }
            }
        }

        return boundRect;
    }

    private void button6_Click(object sender,EventArgs e) //윤곽선 검출
    {
        List<OpenCvSharp.Rect> list = new List<OpenCvSharp.Rect>();
        Bitmap temp = new Bitmap(pictureBox1.Image);
        Mat img = OpenCvSharp.Extensions.BitmapConverter.ToMat(temp);
        list = ip.RegionOfInterest(img);
        for (int i = 0; i < list.Count; i++)
        {
            Cv2.Rectangle(img,list[i],Scalar.Red,3);
        }
        Cv2.ImShow("result",img);
        

        Bitmap bitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(img);
        pictureBox1.Image = bitmap;
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)