Ask Your Question
0

IplImage data access problem

asked 2013-01-31 16:08:09 -0600

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?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-01-31 23:33:33 -0600

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.

edit flag offensive delete link more

Comments

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

manmedia gravatar imagemanmedia ( 2013-02-01 09:40:48 -0600 )edit

Question Tools

Stats

Asked: 2013-01-31 16:08:09 -0600

Seen: 3,405 times

Last updated: Jan 31 '13