English 中文(简体)
What is the memory structure of OpenCV s cvMat?
原标题:
  • 时间:2009-11-11 03:53:10
  •  标签:
  • opencv

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 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 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
",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;




相关问题
Python/OpenCV: Converting images taken from capture

I m trying to convert images taken from a capture (webcam) and do some processing on them with OpenCV, but I m having a difficult time.. When trying to convert the image to grayscale, the program ...

How to smooth a histogram?

I want to smooth a histogram. Therefore I tried to smooth the internal matrix of cvHistogram. typedef struct CvHistogram { int type; CvArr* bins; float thresh[CV_MAX_DIM][2]; /* ...

Testing complex datatypes?

What s are some ways of testing complex data types such as video, images, music, etc. I m using TDD and wonder are there alternatives to "gold file" testing for rendering algorithms. I understand that ...

Displaying OpenCV iplimage data structures with wxPython

Here is my current code (language is Python): newFrameImage = cv.QueryFrame(webcam) newFrameImageFile = cv.SaveImage("temp.jpg",newFrameImage) wxImage = wx.Image("temp.jpg", wx.BITMAP_TYPE_ANY)....

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 ...

Face detection and comparison

I m running a small research on face detection and comparison for my article. Currently, I m using rapid face detection based on haar like features based on OpenCV cascade (I ll implement learning ...

热门标签