This post discussed about how to transpose an image with OpenCV, and here I want to go further: suppose the image is gray-scale one, what is the fastest way of transposing it (or matrix) using C++? My solution is as follows:
// image data is stored in an image buffer image*buffer_
unsigned char *mem = (unsigned char *) malloc(image.bufferSize_);
int height = image.Height();
int width = image.Width();
for(int i=0; i<height; i++)
{
unsigned char *ptr =image.buffer_+i*width;
for(int j=0; j<width; j++)
*(mem+j*height+i) = *(ptr+j);
}
memcpy(image.buffer_,mem,image.bufferSize_);
free(mem);
Some explanations above the above code: we create an image object that contains basic image information as well as the image pixels (in image.buffer_
). When image pixels are stored in image.buffer_
, we assume the image pixels are kept row by row. Any ideas on further improving the above codes?