PDA

View Full Version : Frames reversed horizontally



Jakle
June 11, 2012, 18:08:09
I have the Imaging Source frame grabber (device name: DFG/USB2-It, serial number: 0x2FC9F13) connected to an NTCS format camera source. I am using the FrameHandlerSink with the grabber in "snap mode". I am able to grab a colored image which looks fine, except the only problem is that it is flipped horizontally. It is flipped horizontally regardless of whether or not I have the setFlippedH() method of the Grabber set to true or false. I am reading in the image using OpenCV as follows:
-----------------------------
void CListener::frameReady( Grabber& caller, smart_ptr<MemBuffer> pBuffer, DWORD currFrame) {
DShowLib::MemBuffer::tBmInfHPtr bmpHeader = pBuffer->getBitmapInfoHeader();
LONG height = bmpHeader->biHeight;
LONG width = bmpHeader->biWidth;

BYTE* imptr = pBuffer->getPtr();
cv::Mat img((int)height, (int)width, CV_8UC3);
img.data = (unsigned char*)imptr;
cv::namedWindow("Display window", CV_WINDOW_AUTOSIZE);
cv::imshow("Display window", img);
cvWaitKey(3);
--------------------------

Does anyone have any ideas as to what the problem may be? Or is there some easy and efficient way that this could be fixed so that the image is read in properly?

Thanks in advance,
Jake

Stefan Geissler
June 12, 2012, 10:06:51
Hi,

This is usual behavior and depends on the settings in the BITMAPINFOHEADER. However, you can flip the image using the appropriate OpenCV function call, which is the most simple solution.



void CListener::frameReady( Grabber& caller, smart_ptr<MemBuffer> pBuffer, DWORD currFrame) {
DShowLib::MemBuffer::tBmInfHPtr bmpHeader = pBuffer->getBitmapInfoHeader();
LONG height = bmpHeader->biHeight;
LONG width = bmpHeader->biWidth;

BYTE* imptr = pBuffer->getPtr();
cv::Mat img((int)height, (int)width, CV_8UC3);
img.data = (unsigned char*)imptr;

cv::flip(img,img,0); // <-- flip the image horizontally.

cv::namedWindow("Display window", CV_WINDOW_AUTOSIZE);
cv::imshow("Display window", img);
cvWaitKey(3);

Jakle
June 14, 2012, 19:48:07
Hi,

This is usual behavior and depends on the settings in the BITMAPINFOHEADER. However, you can flip the image using the appropriate OpenCV function call, which is the most simple solution.



void CListener::frameReady( Grabber& caller, smart_ptr<MemBuffer> pBuffer, DWORD currFrame) {
DShowLib::MemBuffer::tBmInfHPtr bmpHeader = pBuffer->getBitmapInfoHeader();
LONG height = bmpHeader->biHeight;
LONG width = bmpHeader->biWidth;

BYTE* imptr = pBuffer->getPtr();
cv::Mat img((int)height, (int)width, CV_8UC3);
img.data = (unsigned char*)imptr;

cv::flip(img,img,0); // <-- flip the image horizontally.

cv::namedWindow("Display window", CV_WINDOW_AUTOSIZE);
cv::imshow("Display window", img);
cvWaitKey(3);

-----------

Thanks for your reply! I'm wondering, is there a more efficient way of doing this? I would prefer it if I could get the framegrabber to read in the image correctly so that there is very little overhead. Is there some setting that could modified to make this happen?

Thanks,
Jake

Jakle
June 14, 2012, 21:42:50
I actually found, by tracing through opencv, that the image is being flipped in cvShowImage() method regardless. The image is flipped upside-down as it is passed into the method initially. By calling cv::flip(img,img,-1) instead, the image looks correct.