0
点赞
收藏
分享

微信扫一扫

opencv csharp工作学习笔记2

码农K 2022-02-13 阅读 49
opencv

主要学习csdn大佬haixin-561的笔记,继续学习opencvsharp

1.输出Mat对象像素

        private void Form1_Load(object sender, EventArgs e)
        {
            string path = "C:\\Users\\Dennis\\Desktop\\1.png";
            Mat m = new Mat(path,ImreadModes.AnyColor|ImreadModes.AnyDepth);
            //Output the value for each pixel line by line
            for (int i = 0;i < m.Height; i++)
            {
                for(int j = 0;j < m.Width; j++)
                {
                    Vec3b color = m.Get<Vec3b>(i,j);
                    Console.WriteLine(color.Item0 + " " + color.Item1 + " " + color.Item2);

                }
            }
            using (new Window("11", m))
            {
                Cv2.WaitKey(0);
            }
        }

2.在unsafe模式下利用指针来获取像素值和行列值

        unsafe private void Form1_Load(object sender, EventArgs e)
        {
            string path = "C:\\Users\\Dennis\\Desktop\\1.png";
            Mat src = new Mat(path,ImreadModes.AnyColor|ImreadModes.AnyDepth);
            Mat dst = new Mat();
            src.CopyTo(dst);
            Console.WriteLine("src颜色通道为{0}", src.Channels());
            Console.WriteLine("dst颜色通道为{0}", src.Channels());
            IntPtr c = dst.Ptr(0);//返回指向指定矩阵行的指针
            byte* c1 = (byte*)c;//将指针类型转换为byte型
            Console.WriteLine("第一个像素值为:{0}", *c1);

            int row = dst.Rows;//行
            int height = dst.Height;
            int clo = dst.Cols;//列
            int width = dst.Width;
            Console.WriteLine("Rows:{0};Cols:{1}",row, clo);
            Console.WriteLine("width:{0};height:{1}", height, width);
        }

在opencv中,Rows等价于height,cols等价于width

 

3.Mat的一些属性

 这里的一些属性还是不太理解,先放着,等后续边用边学。

参考:(5条消息) OpenCvSharp 学习笔记2 --Mat对象简单的像素操作_C++++ ++-CSDN博客

举报

相关推荐

0 条评论