问题描述
想象一下我有以下东西:
Imagine I have the following:
CvMat* mat = cvCreateMat(3,3,CV_16SC3)
这是第3通道的3x3整数矩阵.
This is a 3x3 matrix of integers of channel 3.
现在,如果您查看OpenCV文档,则会发现以下内容是cvMat的减速情况:
Now if you look at OpenCV documentation you will find the following as the deceleration for cvMat:
typedef struct CvMat {
int type;
int step;
int* refcount;
union
{
uchar* ptr;
short* s;
int* i;
float* fl;
double* db;
} data;
union
{
int rows;
int height;
};
union
{
int cols;
int width;
};
} CvMat;
现在,我想玩一下data.ptr,它是指向存储在cvMat中的数据的指针.但是,我很难理解内存的布局方式.如果我有一个3通道矩阵,这是如何工作的?对于一个频道,它很简单,因为它只是一个简单的MxN矩阵,其中M为行,N为cols.但是对于3个频道,是否有3个这些MxN矩阵的??有人可以告诉我如何通过data.ptr初始化3通道矩阵以及如何访问这些值吗?谢谢.
Now, I want to play around with the data.ptr, which is the pointer to the data stored in cvMat. However, I'm having a hard time understanding how the memory is layed out. If I have a 3 channel matrix, how does this work? For one channel its simple because it's justa simple matrix of MxN where M is rows and N is cols. However for 3 channel, are there 3 ofthese MxN matrix's?? Can someone show me how I would go about initalizing a 3 channel matrix via data.ptr and how to access these values please? Thank you.
推荐答案
此网页是对OpenCV 1.1的出色介绍.我建议使用最新版本的 Open CV 2.0 ,该版本具有处理图像,矩阵等的常规Mat
类.与OpenCV 1.1不同.
This webpage is an excellent introduction to OpenCV 1.1. I would recommend using the latest version, Open CV 2.0 which has a general Mat
class which handles images, matrices, etc. unlike OpenCV 1.1.
以上网页详细介绍了以下用于在多通道图像中访问元素的方法:
The above webpage has detailed the following methods for element access in multi-channel images:
间接访问:(一般,但效率低下,可以访问任何类型的图像)
对于多通道浮动(或字节)图像:
For a multi-channel float (or byte) image:
IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_32F,3);
CvScalar s;
s=cvGet2D(img,i,j); // get the (i,j) pixel value
printf("B=%f, G=%f, R=%f\n",s.val[0],s.val[1],s.val[2]);
s.val[0]=111;
s.val[1]=111;
s.val[2]=111;
cvSet2D(img,i,j,s); // set the (i,j) pixel value
直接访问:(有效访问,但容易出错)
对于多通道浮动图片:
IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_32F,3);
((float *)(img->imageData + i*img->widthStep))[j*img->nChannels + 0]=111; // B
((float *)(img->imageData + i*img->widthStep))[j*img->nChannels + 1]=112; // G
((float *)(img->imageData + i*img->widthStep))[j*img->nChannels + 2]=113; // R
使用指针进行直接访问:(在有限的假设下简化且有效的访问)
对于多通道浮动图像(假定4字节对齐):
For a multi-channel float image (assuming a 4-byte alignment):
IplImage* img = cvCreateImage(cvSize(640,480),IPL_DEPTH_32F,3);
int height = img->height;
int width = img->width;
int step = img->widthStep/sizeof(float);
int channels = img->nChannels;
float * data = (float *)img->imageData;
data[i*step+j*channels+k] = 111;
使用c ++包装器直接访问:(简单而有效的访问)
为单通道字节图像,多通道字节图像和多通道浮动图像定义c ++包装器:
Define a c++ wrapper for single-channel byte images, multi-channel byte images, and multi-channel float images:
template<class T> class Image
{
private:
IplImage* imgp;
public:
Image(IplImage* img=0) {imgp=img;}
~Image(){imgp=0;}
void operator=(IplImage* img) {imgp=img;}
inline T* operator[](const int rowIndx) {
return ((T *)(imgp->imageData + rowIndx*imgp->widthStep));}
};
typedef struct{
unsigned char b,g,r;
} RgbPixel;
typedef struct{
float b,g,r;
} RgbPixelFloat;
typedef Image<RgbPixel> RgbImage;
typedef Image<RgbPixelFloat> RgbImageFloat;
typedef Image<unsigned char> BwImage;
typedef Image<float> BwImageFloat;
对于多通道浮动图片:
IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_32F,3);
RgbImageFloat imgA(img);
imgA[i][j].b = 111;
imgA[i][j].g = 111;
imgA[i][j].r = 111;
这篇关于OpenCV的cvMat的内存结构是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!