What is the memory structure of OpenCV's cvMat?
Imagine I have the following:
CvMat* mat = cvCreateMat(3,3,CV_16SC3)
This is a 3x3 matrix of integers of channel 3.
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;
Now, I want to play around with the data.ptr, which is 开发者_如何学Cthe 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 just a simple matrix of MxN where M is rows and N is cols. However for 3 channel, are there 3 of these 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.
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:
Indirect access: (General, but inefficient, access to any type image)
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
Direct access: (Efficient access, but error prone)
For a multi-channel float image:
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
Direct access using a pointer: (Simplified and efficient access under limiting assumptions)
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;
Direct access using a c++ wrapper: (Simple and efficient access)
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;
For a multi-channel float image:
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;
精彩评论