55502f40dc8b7c769880b10874abc9d0

I am working with OpenCV and Qt, Opencv use BGR while Qt uses RGB , so I have to swap those 2 bytes for very big images.

Apart from just skipping two byte and hope nearby pixels have nearby values, there is a better way of doing the following?

int width = iplImage->width;
	int height = iplImage->height;

	uchar *iplImagePtr = (uchar *) iplImage->imageData;
	uchar buf;
	int limit = height * width;
	
	for (int y = 0; y < limit; ++y) {
		buf = iplImagePtr[2];
		iplImagePtr[2] = iplImagePtr[0];
		iplImagePtr[0] = buf;
		iplImagePtr += 3;
	}

	QImage img((uchar *) iplImage->imageData, width, height,
		   QImage::Format_RGB888);

Refactorings

No refactoring yet !

F9a9ba6663645458aa8630157ed5e71e

Ants

April 2, 2010, April 02, 2010 09:10, permalink

No rating. Login to rate!

Your code will only work given the following conditions:
iplImage->nChannels == 3
iplImage->depth == IPL_DEPTH_8U or IPL_DEPTH_8S
iplImage->imageData is 32-bit aligned (to satisfy QImage constructor requirements)
iplImage->widthStep % 4 == 0 (to satisfy QImage constructor requirements)
iplImage->width % 4 == 0 otherwise your for() loop will lose tracking with widthStep

Anyway, what's wrong with using cvConvertImage(src, dst, CV_CVTIMG_SWAP_RB)?

Your refactoring





Format Copy from initial code

or Cancel