Opencv通道数的理解

866 阅读1分钟

1.通道数指描述一个像素点用几个颜色去描述?

  如果用一个数字描述就是单通道
  通常的RBG是用三个值描述的为三通道
  

2.普通图片如何转为单通道

Mat reshape(int cn, int rows)
cn   为通道数,如果设为0,则表示保持通道数不变,否则则变为设置的通道数
rows 表示矩阵行数。 如果设为0,则表示保持原有的行数不变,否则则变为设置的行数。

Mat image;
int width = image.width();
int height = image.height();
int pointCount = width * height;

//通道数不变 1行 列为 pointCount
image.reshape(0, 1);

//N行1列的列向量
image.reshape(0, pointCount);

//三通道的数据 行数不变 列数/3
image.reshape(3, 0);

//三通道数据 行数为原有的1/5 列数 pointCount/height/5/3
//
image.reshape(3, height/5);

//通道数
int dims = image.channels();

//opencv 像素点取数 默认的读取的图片是按照BGR通道读取的
image.get(i, j)[0];
image.get(i, j)[1];
image.get(i, j)[2];

但是现在很多场景下使用的RGB
Mat in =new Mat();
//给in赋值
Mat out =new Mat();
Imgproc.cvtColor(in,out,Imgproc.COLOR_BGR2RGB);

//函数负责转换数据类型不同的Mat
//CV_[bite](USF)  
            CV_8U = 0,
            CV_8S = 1,
            CV_16U = 2,
            CV_16S = 3,
            CV_32S = 4,
            CV_32F = 5,
            CV_64F = 6,
            CV_16F = 7;
有 8bite,16bite,32bite,64bite,对应在 Mat 中,每个像素的所占的空间大小,8位即 CV_8

U : unsigned int , 无符号整形
S : signed int , 有符号整形
F : float , 单精度浮点型,float类型本身即有符号

例如这个:CV_8UC1 最后面1代表通道

points.convertTo(points, CvType.CV_32F);