Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cvTranspose Gives a Different Image Size?

Tags:

c

opencv

I am new to OpenCV, and I want to transpose a grayscale image but I am getting the wrong output size.

// img is an unsigned char image
IplImage *img = cvLoadImage("image.jpg"); // where image is of width=668 height=493
int width  = img->width;
int height = img->height;

I want to transpose it:

IplImage *imgT = cvCreateImage(cvSize(height,width),img->depth,img->nChannels);

cvTranspose(img,imgT);

When I check the images I see that the original image img has a size of 329324, which is correct: 493*668* 1 byte as it is an unsigned char. However imgT has a size of 331328.

I am not really sure where this happened.

EDIT: 1- I am using Windows XP and OpenCV 2.2. 2- By when i check the image, i meant when i see the values of the variable imgT. Such as the imgT->width, imgT->heigt, imgT->size, etc.

like image 597
Louis Avatar asked Dec 29 '25 02:12

Louis


1 Answers

This is due to the fact, that OpenCV aligns the rows of the images at 4-byte boundaries. In the first image a row is 668 bytes wide, which is dividable by 4, so your image elements are contiguous.

The second image has a width of 493 (due to the transposing), which is not dividable by 4. The next higher number dividable by 4 is 496, so your rows are actually 496 bytes wide, with 3 unused bytes at the end of each row, to align the rows at 4-byte boundaries. And in fact 496*668 is indeed 331328. So you should always be aware of the fact, that your image elements need not be contiguous (at least they should be contiguous inside a single row).

like image 135
Christian Rau Avatar answered Dec 31 '25 17:12

Christian Rau