Building QImage from unsigned char * data

I ran into the problem of passing an Image object (captured using the Point Grej FlyCapture2 SDK) to a QImage object. I get a pointer associated with image data by function:

virtual unsigned char* FlyCapture2::GetData ( ) 

and then upload the data:

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

The data formats of both Image objects are 8-bit monocolors. The BytesPerLine parameter should be equal to the width of the image (I already checked it by saving FlyCapture2 :: Image to .bmp and then loading it into QImage).

Is there a problem that you are throwing from unsigned char * to uchar *? Do you have any other ideas? Copying pixel by pixel pixels is too slow.

EDIT: I am converting an image captured by FlyCapture to FlyCapture2::PIXEL_FORMAT_RGB8 for which: R = G = B = 8 bits, inside the PGR::SnapShot() function. SnapShot () returns an unsigned char * const. and here is part of my Qt mapping 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 saving Img to a file, but got an unhandled exception. I tried other pairs of pixel pixels ( FlyCapture2::PIXEL_FORMAT_RGB - 24 bit RGB with QImage::RGB88 8 and FlyCapture2::PIXEL_FORMAT_RGBU32 with QImage::RGB32 )

It is also worth mentioning that the QImage constuctor that I use does not set colorTable (I get an exception by checking if QImage is in grayScale). I need a little more help, I think.

+4
source share
2 answers

First: QImage does not support your own grayscale image, which sounds like you are getting the result - so I would be curious what format argument you use. Probably the easiest solution, albeit memory inefficient, would be to expand your grayscale image in RGB by copying each value three times (to a new QByteArray).

Another problem is that the specific QImage constructor you are using does not copy the underlying data, so you need to be sure that the pointer returned from GetData () survives QImage - or force QImage to make a copy inside, using, let's say QImage :: copy.

Observing more code will help, as other respondents noted.

+5
source

Thank you very much for your help, you were right in the image formats. Unfortunately, the main problem was with passing a pointer between functions. In PGR :: SnapShot (), I created FlyCapture2 :: Image and then got a pointer to the data. FlyCapture2 :: Image was deleted when the function exited, so BadPtr was the returned pointer.

+1
source

All Articles