Display color image using Qlabel



  • 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 gray

    there is no color

    how do i get the color on qlabel?
    any suggestions

    ??



  • Hi,

    I'm not sure I fully understand everything in your code, what is this 'arr' you are calling? It looks like it should have a weird format.
    Also, you declare a QColor c at the begining, and then never use it? Memory from old code?

    I think your issue is not really with the qLabel, but rather with the qRgb "value" you compute.

    But anyway, did you check that arrayIndex was correctly incremented (3 times) at each iteration? It looks like maybe the compiler is optimizing the 3 calls to arr[arrayIndex++] into one, thus losing the incremental effect.

    I'd try to replace the line:

    QRgb value = qRgb(arr[arrayIndex++], arr[arrayIndex++], arr[arrayIndex++]);

    With:

    QRgb value = qRgb(arr[arrayIndex], arr[arrayIndex+1], arr[arrayIndex+2]);
    arrayIndex = arrayIndex+3;

    Edit: Thinking back, I also can't find qRgb as a function. Maybe try:
    c = QColor(arr[arrayIndex], arr[arrayIndex+1], arr[arrayIndex+2]);
    Qrgb value = c.rgb();
    arrayIndex = arrayIndex+3;



  • @Andeol
    Thanks for your reply.

    Initially 'arr' is a dynamic array which i am creating using
    uint8_t *arr = new uint8_t[size];
    where 'size' is
    uint32_t size = g_qt_width * g_qt_high *sizeof(RGB);
    RGB is my structure to generate a color bar.

    The problem is not with the arrayIndex, because when i see the saved image, i get the color bar, but, when i display it on a Qlabel, i get the bars which are all grayed.

    I tried your piece of code and the output is the same.
    Any suggestions?



  • if the saved image is good it means the QImage is good and the pixmap is the problem.
    Did you try passing the image to fromImage() with the default flags i.e. Qt::AutoColor?
    Also, did you try to load the pixmap directly from the image you saved just to confirm it can show it?



  • @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?



  • @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.



  • @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.



  • @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.



  • @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?


  • Qt Champions 2017

    Hi
    Is it possible to get a copy of the image to experiment with ?



  • @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.



  • @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?



  • @kenchan RGB is just my structure here
    typedef struct
    {
    uint8_t r;
    uint8_t g;
    uint8_t b;
    }RGB;



  • @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, setPixel very "heavy" operation, keep that in mind when you plan do modify images multiple times per second or something like that.



  • This post is deleted!


  • @sankar-110 What @mrjj meant, I believe, is the content of your Array arr so that we could create the , supposedly correct, QImage ourself and test it.



  • @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.