繁体   English   中英

如何在 Opencvsharp 中将提取的 Mat Contours 转换为 Bitmap?

[英]How can I convert extracted Mat Contours to Bitmap in Opencvsharp?

在目标图像上应用阈值后,我得到一个 Mat 作为参考然后我调用 function FindContours() 来提取目标图像上所有存在的轮廓最后我尝试一次将提取的轮廓转换为 Bitmap 在这里点我得到一个错误:

System.ArgumentException:“通道数必须是 1、3 或 4。Parametername:src”通过将 Mat 转换为 Bitmap

private void button1_Click(object sender, EventArgs e)
{
    OpenFileDialog getImag = new OpenFileDialog();
    getImag.Filter = "PNG,JPG|*.png;*.jpg";

    DialogResult result = getImag.ShowDialog();
    string Source_Logo_Link = string.Empty;

    if (result == DialogResult.OK)
    {
        Source_Logo_Link = getImag.FileName;
        System.Drawing.Image image = System.Drawing.Image.FromFile(Source_Logo_Link);

        Mat src = new OpenCvSharp.Mat(Source_Logo_Link);
        OpenCvSharp.Mat gray = src.CvtColor(ColorConversionCodes.BGR2GRAY);

        OpenCvSharp.Mat binary = gray.Threshold(0, 255, ThresholdTypes.Otsu);
        OpenCvSharp.Mat[] contoursQuery;
            OutputArray hierarchyQ = InputOutputArray.Create(new List<Vec4i>());
        binary.FindContours(out contoursQuery, hierarchyQ, RetrievalModes.CComp, ContourApproximationModes.ApproxTC89KCOS);

        List<Bitmap> images = new List<Bitmap>();
        for (int i = 0; i <= contoursQuery.Length; i++)
            images.add(contoursQuery[i].toBitmap());
    }    
}

抱歉,这有点晚了,但我希望这仍然有所帮助。 除了一些其他问题(比如 for 循环的计数器),我想你的主要问题是,你误解了 FindContours 的结果。 它实际上只是为您提供了它在图像中找到的所有轮廓点的列表,而不是完整的图像。 查看文档FindContours ,它清楚地说明了轮廓参数:

每个轮廓都存储为点向量

所以这意味着如果你“一次一个地绘制位图”,你需要先创建这些位图,然后在其中绘制轮廓。 为了使绘图更容易(更快),可以使用DrawContours命令,您可以将其用于绘图部分。 总而言之,它最终应该看起来像这样:

private void button1_Click(object sender, EventArgs e)
{
   OpenFileDialog getImag = new OpenFileDialog();
   getImag.Filter = "PNG,JPG|*.png;*.jpg";

   DialogResult result = getImag.ShowDialog();
   string Source_Logo_Link = string.Empty;

   if (result == DialogResult.OK)
   {
        Source_Logo_Link = getImag.FileName;
        System.Drawing.Image image = System.Drawing.Image.FromFile(Source_Logo_Link);

        Mat src = new OpenCvSharp.Mat(Source_Logo_Link);
        OpenCvSharp.Mat gray = src.CvtColor(ColorConversionCodes.BGR2GRAY);

        OpenCvSharp.Mat binary = gray.Threshold(0, 255, ThresholdTypes.Otsu);

        // I'd really prefer to work with point lists and HierarchyIndex because 
        // it's much more descriptive that way
        OpenCvSharp.Point[][] contours;
        OpenCvSharp.HierarchyIndex[] hierarchyQ;

        binary.FindContours(out contours,out hierarchyQ, RetrievalModes.List, ContourApproximationModes.ApproxTC89KCOS);

        List<Bitmap> images = new List<Bitmap>();
        for (int i = 0; i < contoursQuery.Length; i++){
            var singleContour = new OpenCvSharp.Mat(src.Size(), MatType.CV_8U,0);
            singleContour.DrawContours(contours,i,Scalar.White);
            images.Add(singleContour.ToBitmap());
        }
    }    
}

我还将RetrievalMode更改为“List”,因为您似乎并不真正关心层次关系。

希望有所帮助;-)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM