在图像处理的程序中,有时候会有一些参数要得到图像的宽高和通道等,这是一个小问题,但是如果让你自己来实现,也得想一下,后来查阅OpenCV,发现其自带的数据类型中就有这个参数,可以直接获得。
第一种 IplImage数据类型
typedef struct _IplImage
{
int nSize; /* IplImage大小,=sizeof(IplImage)*/
int ID; /* 版本 (=0)*/
int nChannels; /* 大多数OPENCV函数支持1,2,3 或 4 个通道 */
int alphaChannel; /* 被OpenCV忽略 */
int depth; /* 像素的位深度: IPL_DEPTH_8U, IPL_DEPTH_8S, IPL_DEPTH_16U,
IPL_DEPTH_16S, IPL_DEPTH_32S, IPL_DEPTH_32F and IPL_DEPTH_64F 可支持 */
char colorModel[4]; /* 被OpenCV忽略 */
char channelSeq[4]; /* 被OpenCV忽略 */
int dataOrder; /* 0 - 交叉存取颜色通道,对三通道RGB图像,像素存储顺序为BGR BGR BGR ... BGR;
1 - 分开的颜色通道,对三通道RGB图像,像素存储顺序为RRR...R GGG...G BBB...B。
cvCreateImage只能创建交叉存取图像 */
int origin; /* 0 - 顶—左结构,
1 - 底—左结构 (Windows bitmaps 风格) */
int align; /* 图像行排列 (4 or 8). OpenCV 忽略它,使用 widthStep 代替 */
int width; /* 图像宽像素数 */
int height; /* 图像高像素数*/
struct _IplROI *roi;/* 图像感兴趣区域. 当该值非空只对该区域进行处理 */
struct _IplImage *maskROI; /* 在 OpenCV中必须置NULL */
void *imageId; /* 同上*/
struct _IplTileInfo *tileInfo; /*同上*/
int imageSize; /* 图像数据大小(在交叉存取格式下imageSize=image->height*image->widthStep),单位字节*/
char *imageData; /* 指向排列的图像数据 */
int widthStep; /* 排列的图像行大小,以字节为单位 */
int BorderMode[4]; /* 边际结束模式, 被OpenCV忽略 */
int BorderConst[4]; /* 同上 */
char *imageDataOrigin; /* 指针指向一个不同的图像数据结构(不是必须排列的),是为了纠正图像内存分配准备的 */
}
IplImage;
其中,变量width height nChannels和widthstep就表示这些,因此,在程序中,使用如下代码就可以获得这些图像的参数值。
nWidth=src>width;
nHeight=src->height;
DibWidth=src->widthStep;
nChannel=src->nChannels;
第二种 Mat数据类型
因为IplImage的手动释放内存以及某些函数的速度相比C++版本的,较慢,现在越来越提倡在程序中使用Mat数据类型,查询Mat可以得知
enum { MAGIC_VAL=0x42FF0000, AUTO_STEP=0, CONTINUOUS_FLAG=CV_MAT_CONT_FLAG, SUBMATRIX_FLAG=CV_SUBMAT_FLAG };
/*! includes several bit-fields:
- the magic signature
- continuity flag
- depth
- number of channels
*/
int flags;
//! the matrix dimensionality, >= 2
int dims;
//! the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions
int rows, cols;
//! pointer to the data
uchar* data;
MSize size;
MStep step;
//! returns type of sparse matrix elements
int type() const;
//! returns the depth of sparse matrix elements
int depth() const;
//! returns the number of channels
int channels() const;
因而,在实际程序中,只需要输入以下代码,就可以获得这些参数值。
width=pIn.rows;
height=pIn.cols;
DibWidth=pIn.step;
channels=pIn.channels;