First time here? Check out the FAQ!

Ask Your Question
0

IplImage data access problem

asked Jan 31 '13

manmedia gravatar image

Hello, I am newbee to OpenCV so if I am making silly mistakes and assumptions, please ignore me!

I am trying access the data contents of IplImage structure. To clarify, I am trying to access the data contents that are pointed by IplImage.imageData. I am trying to access it using widthStep and height like this:

uchar* ptr;
for(int i=0; i<myIplImage.height; i++) {
    ptr = (uchar*)(myIplImage.imageData + (i*myIplImage.widthStep));
   for(int j=0; j < myIplImage.width; j++) {
        std::cout << ptr[j];
   }
}

However, i am constantly getting exception and cannot even catch it to print the actual problem message so that I can fix it. Is it the correct way to access IplImage data? If not, what is it that I am doing wrong and where can I find more information about this?

Preview: (hide)

1 answer

Sort by » oldest newest most voted
1

answered Feb 1 '13

Haris gravatar image

If you are using IplImage * you need to use myIplImage->height, myIplImage->widht etc. You can use image data using below code

int width     = img->width;
int height    = img->height;
int nchannels = img->nChannels;
int step      = img->widthStep;

uchar *data = ( uchar* )img->imageData;    
for(int  i = 0 ; i < height ; i++ ) {
    for(int j = 0 ; j < width ; j++ ) {
        int r = data[i*step + j*nchannels + 0];
        int g = data[i*step + j*nchannels + 1];
        int b = data[i*step + j*nchannels + 2];   
    }
}

Or using another method

for(int i=pix; i<img->height; i++)
    {
      for(int j=0; j<img->width; j++)
       {
         CvScalar ele;
         ele=cvGet2D(img,i,j);
         int B= ele.val[0]; //Blue channel
         int G=ele.val[1]; //Green channel
         int R=ele.val[2]; //Red channel

       }
     }

And if you are using Mat, See OpenCV documentation and this discussion for more details.

Preview: (hide)

Comments

@HarisMoonamkunnu Thanks for this...I actually use a grayscale image and sorted out the problem....thanks

manmedia gravatar imagemanmedia (Feb 1 '13)edit

Question Tools

Stats

Asked: Jan 31 '13

Seen: 3,436 times

Last updated: Jan 31 '13