主要根据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的一些属性
data:uchar类型的指针,指向Mat数据矩阵的首地址。
dims:Mat矩阵的维度,若Mat是一个二维矩阵,则dims=2,三维则dims=3,大多数情况下处理的都是二维矩阵,是一个平面上的矩阵。
rows:Mat矩阵的行数
cols:Mat矩阵的列数
size():Mat矩阵的类型,包含有矩阵中元素的类型以及通道数信息
channels():Mat矩阵元素拥有的通道数。
depth:度量每一个像素中每一个通道的精度,但它本身与图像的通道数无关
elemSize:矩阵中每一个元素的数据大小
type:Mat矩阵的类型,包含有矩阵中元素的类型以及通道数信息
这里的一些属性还是不太理解,先放着,等后续边用边学。