Display RGB HALCON image as QImage
-
Compare the CountChannels values, the width and height values.
-
@SGaist I can answer the Width and Height question. When I use the InterleaveChannels method, the width is 4x larger than the original image, while the height stays the same. I tried to divide by 4 when constructing the QImage as shown in the matching example, but no luck.
-
What do you get if using the sizes returned by your interleaved image ?
-
Are you also sure that you choose the correct image format for QImage ?
-
@SGaist said in Display RGB HALCON image as QImage:
What do you get if using the sizes returned by your interleaved image ?
While showing the interleaved image, it is "wider" compared with the screen shot image. I think is due to the fact that the width is 4x higher.
-
This is because instead of an image that contains 3 channels each one containing only R or G or B values, you now have one image that have only 1 channel containing RGBA values. When you get the image pointer you have width*4 that is correct. "width" is not really the image width, is the "bytes per line" in this case.
What happens if you use this QImage contructor?
QImage qimage(imgRawPtr, width / 4, height, width, QImage::Format_RGB32);
-
This is because instead of an image that contains 3 channels each one containing only R or G or B values, you now have one image that have only 1 channel containing RGBA values. When you get the image pointer you have width*4 that is correct. "width" is not really the image width, is the "bytes per line" in this case.
What happens if you use this QImage contructor?
QImage qimage(imgRawPtr, width / 4, height, width, QImage::Format_RGB32);
@ollarch said in Display RGB HALCON image as QImage:
This is because instead of an image that contains 3 channels each one containing only R or G or B values, you now have one image that have only 1 channel containing RGBA values. When you get the image pointer you have width*4 that is correct. "width" is not really the image width, is the "bytes per line" in this case.
What happens if you use this QImage contructor?
QImage qimage(imgRawPtr, width / 4, height, width, QImage::Format_RGB32);
The image is exactly the same as the one on the screenshot, nothing changed.
-
@ollarch said in Display RGB HALCON image as QImage:
Could you try to save the processed image to disk and check it?
I have saved the images using the following code:
_currentHImage.WriteImage("png", 0, "images/himage"); HalconCpp::HImage interleavedImg = _currentHImage.InterleaveChannels("argb", "match", 0); // Only if the image is RGB unsigned char* imgRawPtr = (unsigned char *)interleavedImg.GetImagePointer1(&type, &width, &height); QImage qimage(imgRawPtr, width / 4, height, width, QImage::Format_RGB32); qimage.save("images/qimage.png");
The images are equal! The only problem is showing them on the paint.drawImage....
-
Looks like I was using the wrong format and not scaling the image correctly.
This is the fix:if(_currentHImage.CountChannels().I() == 3) { HalconCpp::HImage interleavedImg = _currentHImage.InterleaveChannels("argb", "match", 0); // Only if the image is RGB unsigned char* imgRawPtr = (unsigned char *)interleavedImg.GetImagePointer1(&type, &width, &height); QImage qimage(imgRawPtr, width / 4, height, width, QImage::Format_RGB32); QImage newqimage = qimage.scaled(400, 300, Qt::KeepAspectRatio); QPainter painter(this); painter.drawImage(QPoint(0, 0), newqimage); } else { unsigned char* imgRawPtr = (unsigned char*)_currentHImage.GetImagePointer1(&type, &width, &height); QImage qimage(imgRawPtr, width, height, QImage::Format_Grayscale8); QImage newqimage = qimage.scaled(400, 300, Qt::KeepAspectRatio); QPainter painter(this); painter.drawImage(QPoint(0, 0), newqimage); } }
Thanks for all your help!
-
Nice !
Since you have it working now, please mark the thread as solved so other forum members may know a solution has been found :-)