OpenCVSharp 笔记6 Mat对象的创建与赋值

        static void Main(string[] args)
        {
            Mat src = Cv2.ImRead("lenna.png", ImreadModes.AnyColor);
            Cv2.ImShow("src image", src);

            Mat m1 = src.Clone();
            Mat m2 = new Mat();
            src.CopyTo(m2);

            //创建空白图像
            Mat m3 = Mat.Zeros(10, 10, MatType.CV_8UC1);//无论单通道三通道所有值都初始化为0
            // Mat m3 = Mat.Ones(10, 10, MatType.CV_8UC3);//单通道初始化为1,三通道只有第一通道被初始化为1
            Mat_Print(m3);

            Mat m4 = new Mat(100,100, MatType.CV_8UC3, new Scalar(255,0,0));
            Cv2.NamedWindow("m4",WindowFlags.Normal);
            Cv2.ImShow("m4", m4);

            Mat m5 = new Mat(100, 100, MatType.CV_8UC1, new Scalar(0));
            Cv2.NamedWindow("m5", WindowFlags.Normal);
            Cv2.ImShow("m5", m5);
           
            Cv2.WaitKey();
            Cv2.DestroyAllWindows();
        }

        public static void Mat_Print(Mat mat)
        {
            Console.WriteLine("宽度:{0} 高度:{1} 通道数:{2} ", mat.Width,mat.Height,mat.Channels());
            Console.WriteLine(Cv2.Format(mat));
            /*if (mat.Channels() == 3)
            {
                for (int i = 0; i < mat.Height; i++)
                {
                    for (int j = 0; j < mat.Width; j++)
                    {
                        Vec3b color = mat.Get(i, j); //new Vec3b(); 颜色通道类型 (字节的三元组),直接视同Get泛型方法返回指定类型
                        Console.Write(color.Item0 + " " + color.Item1 + " " + color.Item2 + " ");
                    }
                    Console.WriteLine(" "); //输出一个换行
                }
            }
            else
            {
                for (int i = 0; i < mat.Height; i++)
                {
                    for (int j = 0; j < mat.Width; j++)
                    {
                        byte pv = mat.Get(i, j);                  
                        Console.Write(pv + " ");
                    }
                    Console.WriteLine(" "); //输出一个换行
                }
            }*/
        }

你可能感兴趣的:(OpenCVSharp,opencv,c#)