I’m a tad confused.
I am just getting started with OpenCV and its image data is pointed to by a char pointer. I can’t quite work out how that works considering the actual data itself could be any number of data types, e.g. uint, float, double. As far as I knew, a pointer had to be of the same type as the pointer it represents.
It’s probably worth noting that openCV is a C library and my background is C++, so I am unaware of how these problems of needing variable types are solved in C.
For example the follwing code taken from Learning OpenCV illustrates my confusion:
void saturate_sv( IplImage* img ) {
for( int y=0; y<img->height; y++ ) {
uchar* ptr = (uchar*) (
img->imageData + y * img->widthStep
);
for( int x=0; x<img->width; x++ ) {
ptr[3*x+1] = 255;
ptr[3*x+2] = 255;
}
}
}
So this works, but when I try to operate on a iplImage of type IPL_DEPTH_64F and use ptr[3*x+1] = 1 The results are incorrect. So to distil my problems: how can I work on integer or floating point data through char pointers and specifically how could I rectify the above example to work with a double precision data.
Thanks
IPL_DEPTH_64Fordoubleimages will take care of the data from 0 – 1.Matto handle images, matrices, etc.Here’s a simple way to access elements in your image efficiently:
Since you’re working with a
doubleimage, it makes more sense to:doublepointer so you can easily assign elements in a row withptr[x]img->imageData + y * img->widthStep) and the cast it to adoublepointerAlso, it’s important that you do the pointer arithmetic in bytes (or
uchar, i.e.unsigned char) since OpenCV tends to pad the rows of the images with extra bytes for efficiency (especially fordoubleimages).So even if a
doubleelement is 8 bytes, and you have, say, 300 rows, a row is not guaranteed to end at 8*300 or 2400 bytes since OpenCV might pad the end.Therefore, this prevents you from initializing a pointer to the first element of the image and then using
ptr[y*img->height+x]to access elements since each row might have more than8*(y*img->height)bytes.That’s why the example code calculates the pointer to each row each time using
img->widthStepwhich represents the true size of each row in bytes.OpenCV 2.0
If you use the
Matclass, you can do the same thing along these lines:where img.step is the distance between successive rows in bytes
And if you want to directly access the element (slower):
img.at<double>(y,x)