Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Display color image using Qlabel
Forum Updated to NodeBB v4.3 + New Features

Display color image using Qlabel

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 5 Posters 4.6k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • AndeolA Offline
    AndeolA Offline
    Andeol
    wrote on last edited by Andeol
    #2

    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;

    Developer for R++ : https://rplusplus.com/

    1 Reply Last reply
    4
    • S Offline
      S Offline
      sankar-110
      wrote on last edited by
      #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?

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kenchan
        wrote on last edited by
        #4

        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?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sankar-110
          wrote on last edited by
          #5

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

          K 1 Reply Last reply
          0
          • S sankar-110

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

            K Offline
            K Offline
            kenchan
            wrote on last edited by
            #6

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

            1 Reply Last reply
            0
            • S Offline
              S Offline
              sankar-110
              wrote on last edited by
              #7

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

              K 1 Reply Last reply
              0
              • S sankar-110

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

                K Offline
                K Offline
                kenchan
                wrote on last edited by kenchan
                #8

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

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  sankar-110
                  wrote on last edited by
                  #9

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

                  K 2 Replies Last reply
                  0
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

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

                    1 Reply Last reply
                    0
                    • S sankar-110

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

                      K Offline
                      K Offline
                      kenchan
                      wrote on last edited by
                      #11

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

                      1 Reply Last reply
                      0
                      • S sankar-110

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

                        K Offline
                        K Offline
                        kenchan
                        wrote on last edited by kenchan
                        #12

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

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          sankar-110
                          wrote on last edited by
                          #13

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

                          1 Reply Last reply
                          0
                          • S sankar-110
                            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

                            ??

                            J.HilkJ Online
                            J.HilkJ Online
                            J.Hilk
                            Moderators
                            wrote on last edited by
                            #14

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


                            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                            Q: What's that?
                            A: It's blue light.
                            Q: What does it do?
                            A: It turns blue.

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              sankar-110
                              wrote on last edited by
                              #15
                              This post is deleted!
                              J.HilkJ 1 Reply Last reply
                              0
                              • S sankar-110

                                This post is deleted!

                                J.HilkJ Online
                                J.HilkJ Online
                                J.Hilk
                                Moderators
                                wrote on last edited by
                                #16

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


                                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                Q: What's that?
                                A: It's blue light.
                                Q: What does it do?
                                A: It turns blue.

                                1 Reply Last reply
                                1
                                • S Offline
                                  S Offline
                                  sankar-110
                                  wrote on last edited by
                                  #17

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

                                  1 Reply Last reply
                                  0

                                  • Login

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • Users
                                  • Groups
                                  • Search
                                  • Get Qt Extensions
                                  • Unsolved