Display color image using Qlabel
-
@kenchan
Yes, the problem is with the set pixmap or Qlabel because when I load the image, even then it only shows me in gray.
Yes i have also tried the default flag, Qt::AutoColor.I do not understand where I am going wrong, as even when i load any image, It comes in Grayscale.
Any solutions?wrote on 13 Feb 2018, 04:51 last edited by@sankar-110 OK. so why not try a simple colour format such as QImage::Format_RGB888, a 24 bit one.
It could be that the window system does not like the 32 bit with alpha. -
wrote on 13 Feb 2018, 05:02 last edited by
@kenchan ,
I have already tried that out, and the result is the same.
Do i have to change any settings on my windows PC or do I have to change any Qlabel properties or settings?
I am really stuck here. -
@kenchan ,
I have already tried that out, and the result is the same.
Do i have to change any settings on my windows PC or do I have to change any Qlabel properties or settings?
I am really stuck here.wrote on 13 Feb 2018, 05:47 last edited by kenchan@sankar-110 OK, I do a similar thing when getting a pixmap from an image i have changed.
I use an RGBA8888 format but when I use the QRgb i use the qRgba macro and set the alpha to zero like this.QRgb col = qRgba(r,g,b,0);then i use the QPixmap::fromImage(the Image); with the default conversion argument. I don't put it on a QLabel but it does get used
by a QPainter on a window eventually.Hope that might help.
-
wrote on 13 Feb 2018, 06:48 last edited by
@kenchan , sorry, but can you explain in more detail as I am still a beginner to QT.
If I do not display the image on a label, how will the image be displayed on the UI? -
Hi
Is it possible to get a copy of the image to experiment with ? -
@kenchan , sorry, but can you explain in more detail as I am still a beginner to QT.
If I do not display the image on a label, how will the image be displayed on the UI?wrote on 13 Feb 2018, 07:10 last edited by@sankar-110 Nothing wrong with putting a QPixmap on a QLabel.
I think you should play around with different pixel formats and the appropriate qRgb functions. The code looks OK to me but as you can see the image is not being shown as expected. -
@kenchan , sorry, but can you explain in more detail as I am still a beginner to QT.
If I do not display the image on a label, how will the image be displayed on the UI?wrote on 13 Feb 2018, 07:14 last edited by kenchan@sankar-110
I just noticed you code again here...uint8_t *arr = new uint8_t[size]; where 'size' is uint32_t size = g_qt_width * g_qt_high *sizeof(RGB);Should that not be uint32_t size = g_qt_width * g_qt_high *sizeof(RGB) * 4 if you are using a QImage::Format_RGB32?
or am i missing what RGB means? -
wrote on 13 Feb 2018, 07:24 last edited by
@kenchan RGB is just my structure here
typedef struct
{
uint8_t r;
uint8_t g;
uint8_t b;
}RGB; -
uint32_t arrayIndex = 0; QColor c; Qt::ImageConversionFlags flag1 = Qt::ColorOnly; QImage *img = new QImage( g_qt_high ,g_qt_width, QImage::Format_RGB32); for(uint32_t i = 0; i < g_qt_high; i++) { for(uint32_t u = 0; u < g_qt_width; u++) { QRgb value = qRgb(arr[arrayIndex++], arr[arrayIndex++], arr[arrayIndex++]); img->setPixel(i, u,value); } } ui->label->setPixmap(QPixmap::fromImage(*img,flag1)); img->save("z123.jpg");This is my piece of code
when i see the saved image here, i get the color bar with respective colors
but on qlabel i get the bars, but no color
meaning, all the bars are only shades of graythere is no color
how do i get the color on qlabel?
any suggestions??
@sankar-110
I just run this basic code#include <QApplication> #include <QLabel> int main(int argc, char *argv[]) { QApplication a(argc, argv); Qt::ImageConversionFlags flag1 = Qt::ColorOnly; QImage img(100,100,QImage::Format_RGB32); for(int i(0); i < 100; i++){ for(int j(0); j < 100; j++){ QRgb value = qRgb(255,0,0); img.setPixel(i,j,value); } } QLabel lbl; lbl.show(); lbl.resize(100,100); lbl.setPixmap(QPixmap::fromImage(img, flag1)); return a.exec(); }works perfectly fine.
Try to show your QPixmap in a new, independent QLabel, maybe yours gets overwritten/ modfied later in the code?
Btw,
setPixelvery "heavy" operation, keep that in mind when you plan do modify images multiple times per second or something like that. -
wrote on 13 Feb 2018, 07:37 last edited byThis post is deleted!
-
This post is deleted!
@sankar-110 What @mrjj meant, I believe, is the content of your Array
arrso that we could create the , supposedly correct, QImage ourself and test it. -
wrote on 13 Feb 2018, 08:21 last edited by
@mrjj , @J-Hilk
typedef struct
{
uint8_t r;
uint8_t g;
uint8_t b;
}RGB;
uint32_t get_video_data(RGB *data,uint32_t *size)
{
const RGB BAR_COLOUR[8] =
{
{ 255, 255, 255 }, // 100% White
{ 255, 255, 0 }, // Yellow
{ 0, 255, 255 }, // Cyan
{ 0, 255, 0 }, // Green
{ 255, 0, 255 }, // Magenta
{ 255, 0, 0 }, // Red
{ 0, 0, 255 }, // Blue
{ 0, 0, 0 }, // Black
};
}
uint32_t size = g_qt_width * g_qt_high *sizeof(RGB);
uint8_t *arr = new uint8_t[size];
RGB *video_frame = (RGB *)malloc(size);
get_video_data(video_frame,&video_size);
memcpy(arr,video_frame,size);
This is the content of my arr.
15/17