使用Mat类型存储图像,我的一些笔记。
cv::Mat是个模板,里面保存的数据类型是单个元素或者vector。
Mat(rows, cols, type);
rows:高度
cols:宽度
type参数含义如下:
//There are tons of matrix type in CvMat defined in cvtypes.h and listed below.
#define CV_8U 0
#define CV_8S 1
#define CV_16S 2
#define CV_32S 3
#define CV_32F 4
#define CV_64F 5
#define CV_USRTYPE1 6
#define CV_USRTYPE2 7
#define CV_8UC1 (CV_8U + 0*8)
#define CV_8UC2 (CV_8U + 1*8)
#define CV_8UC3 (CV_8U + 2*8)
#define CV_8UC4 (CV_8U + 3*8)
#define CV_8SC1 (CV_8S + 0*8)
#define CV_8SC2 (CV_8S + 1*8)
#define CV_8SC3 (CV_8S + 2*8)
#define CV_8SC4 (CV_8S + 3*8)
#define CV_16SC1 (CV_16S + 0*8)
#define CV_16SC2 (CV_16S + 1*8)
#define CV_16SC3 (CV_16S + 2*8)
#define CV_16SC4 (CV_16S + 3*8)
#define CV_32SC1 (CV_32S + 0*8)
#define CV_32SC2 (CV_32S + 1*8)
#define CV_32SC3 (CV_32S + 2*8)
#define CV_32SC4 (CV_32S + 3*8)
#define CV_32FC1 (CV_32F + 0*8)
#define CV_32FC2 (CV_32F + 1*8)
#define CV_32FC3 (CV_32F + 2*8)
#define CV_32FC4 (CV_32F + 3*8)
#define CV_64FC1 (CV_64F + 0*8)
#define CV_64FC2 (CV_64F + 1*8)
#define CV_64FC3 (CV_64F + 2*8)
#define CV_64FC4 (CV_64F + 3*8)
访问元素:
方式1
比如是CV_8UC1类型的,
image.at<unsigned char>(1,1);//得到像素点的值。
方式2
(这部分稍后重新整理一下)
Mat& ScanImageAndReduceC(Mat& I, const uchar* const table)
{
// 只接受char类型矩阵
CV_Assert(I.depth() != sizeof(uchar));
int channels = I.channels();
int nRows = I.rows*channels;
int nCols = I.cols;
if (I.isContinuous())
{
nCols*= nRows;
nRows = 1;
}
int i,j;
uchar* p;
for( i = 0; i < nRows; ++i)
{
p = I.ptr<uchar>(i);
for ( j = 0; j < nCols; ++j)
{
p[j] = table[p[j]];
}
return I;
}