views:

38

answers:

2

I encountered a problem with passing Image object (captured with Point Grej FlyCapture2 SDK) to QImage object. I am getting a pointer associated with Image data by function:

virtual unsigned char* FlyCapture2::GetData  (   ) 

and then loading the data by:

QImage::QImage ( uchar * data, int width, int height, int bytesPerLine, Format format )

Formats of data of both Image objects are 8-bit monocolor. BytesPerLine parameter should be equal to width of the Image (I've already checked it by saving FlyCapture2::Image to .bmp and then loading it to QImage).

Do you thing the problem is casting from unsigned char* to uchar*? Do you have any other ideas? Copying image pixel by pixel is much too slow.

EDIT: I am converting Image captured by FlyCapture into the FlyCapture2::PIXEL_FORMAT_RGB8 , for which: R = G = B = 8 bits, within PGR::SnapShot() function. SnapShot() returns unsigned char* const. and here is a part of my Qt display function:

unsigned char *const img = PGRSystem->SnapShot();
QImage Img(img, 1024, 768, QImage::Format_RGB888);
QGraphicsScene *Scene = new QGraphicsScene();
Scene->addPixmap(QPixmap::fromImage(Img));
ui.ImageView->setScene(Scene);
ui.ImageView->fitInView(ui.ImageView->itemAt(100,100));
delete [] Scene;

I also tried to save Img to file, but got unhandled exception then. I tried other pixel format pairs (FlyCapture2::PIXEL_FORMAT_RGB - 24 bit RGB with QImage::RGB888 and FlyCapture2::PIXEL_FORMAT_RGBU32 with QImage::RGB32)

It is also worth to mention that QImage constuctor, which I am using, does not set the colorTable (I am getting exception when checking if QImage is in grayScale). I need a little more help I guess.

+4  A: 

First thing - QImage doesn't support a native greyscale image, which is what it sounds as if you're getting as output - so I would be curious what Format argument you're using. Probably the easiest solution, though memory-inefficent, will be to expand your greyscale image to RGB by copying each value three times (into a new QByteArray).

An additional concern is that the particular QImage constructor you're using, does not copy the underlying data, so you need be sure the pointer returned from GetData() outlives the QImage - or force the QImage to make a copy internally, using, say, QImage::copy.

Seeing more code would help, as other respondents noted above.

James Turner
I would bet on the data going away before drawing, myself.
Caleb Huitt - cjhuitt
I am using QImage::Format_Indexed8. I found yesterday that this is not native grayscale format. Do you think QImage::setColorCount and setColorTable will solve the problem?
Marcin
You could create a colour table, mapping each tonal value to an equivalent RGB value, but given that RAM is cheap, and indexed images are slightly more complicated for the system to deal with, thats why I proposed expanding each pixel value to RGB before building the QImage. (QImage really should support greyscale images as you need, but that's another issue). Either approach should work, so if populating the colour table is easier for you, go for it.
James Turner
I just looked at the FlyCapture API, and it has image format conversion helpers - flycaptureConvertImage - so if you can find an image format that's equivalent to one of the QImage formats, you're done.
James Turner
I added some more information to my original post. Could you please take a look on it as it is still not working?
Marcin
+1  A: 

Thanks a lot for your help, you were right about the Image formats. Unfortunatelly the main problem was associated with passing the pointer between functions. In PGR::SnapShot() I was creating FlyCapture2::Image and then I was getting the pointer to data. FlyCapture2::Image was detructed when exit the function so returned pointer was BadPtr.

Marcin