Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to display 8-bit grayscale raw image to a QLabel and display it in x11 board?
Forum Update on Monday, May 27th 2025

How to display 8-bit grayscale raw image to a QLabel and display it in x11 board?

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
qt5.2.1x11qimagegrayscale8bitgrayscale
6 Posts 5 Posters 8.1k Views
  • 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.
  • A Offline
    A Offline
    Ajith_P_V
    wrote on last edited by
    #1

    Hi all,

    Currently I'm working on a embedded platform which have Qt 5.2.1 and x11 support. The task involves getting a 300x400 8-bit grayscale single channel raw image from one of the module and display it on a QLabel.

    I read that, there is no Grayscale support available in the Qt5 and hence I have tried with "QImage::Format_Indexed8". I can manage to get the correct data but the colour or look and feel is not at all matching with the actual image content.
    How I can display a 300x400 8-bit grayscale single channel raw image to a QLabel correctly?

    PS: I can share what I have done for this conversion, if anybody interested while giving help.

    Thank you in adavance,
    Ajith P V

    raven-worxR 1 Reply Last reply
    0
    • A Ajith_P_V

      Hi all,

      Currently I'm working on a embedded platform which have Qt 5.2.1 and x11 support. The task involves getting a 300x400 8-bit grayscale single channel raw image from one of the module and display it on a QLabel.

      I read that, there is no Grayscale support available in the Qt5 and hence I have tried with "QImage::Format_Indexed8". I can manage to get the correct data but the colour or look and feel is not at all matching with the actual image content.
      How I can display a 300x400 8-bit grayscale single channel raw image to a QLabel correctly?

      PS: I can share what I have done for this conversion, if anybody interested while giving help.

      Thank you in adavance,
      Ajith P V

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Ajith_P_V
      QImage::Format_Indexed8 requires a corresponding color table. If you haven't set it the result won't be what you expect.
      So you can init the color table like this:

      QImage img(imgData, imgWidth, imgHeight, QImage::Format_Indexed8);
      for( int i = 0; i < 256; ++i )
          img.setColor(i, qRgb(i,i,i));
      

      Where does your image data come from?

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      A L 2 Replies Last reply
      1
      • raven-worxR raven-worx

        @Ajith_P_V
        QImage::Format_Indexed8 requires a corresponding color table. If you haven't set it the result won't be what you expect.
        So you can init the color table like this:

        QImage img(imgData, imgWidth, imgHeight, QImage::Format_Indexed8);
        for( int i = 0; i < 256; ++i )
            img.setColor(i, qRgb(i,i,i));
        

        Where does your image data come from?

        A Offline
        A Offline
        Ajith_P_V
        wrote on last edited by
        #3

        @raven-worx Thank you very much for your help. I'm actually using the color table but I think I'm missing something in the conversion. I think it is better to show what I have done for the conversion (please see the below code snippet).

        Step1: Taking the 8-bit single channel grayscale raw image which is stored in the predefined path.

        QFile file8bitImage("<path of the image folder>/8bitImage.raw");
        if (!file8bitImage.open(QFile::ReadOnly))
                return;
        QByteArray array8bitImage = file8bitImage.readAll();
            char* buffer8bitImage = array8bitImage.data();
        
            file8bitImage.close(); // close the file after copying the data
        QImage *image = new  QImage(reinterpret_cast<unsigned char*>(buffer8bitImage), 300, 400, QImage::Format_Indexed8);
         QImage resultimage = convertToGray(image); //calling the convert function
        

        Step 2: Convert logic for display

        QImage MainWindow::convertToGray(QImage *pInputImage)
        {
            if ( pInputImage && !pInputImage->isNull() )
            {
                QImage retImg(pInputImage->width(),pInputImage->height(),QImage::Format_Indexed8);
                QVector<QRgb> table( 256 );
                for( int i = 0; i < 256; ++i )
                {
                    table[i] = qRgb(i,i,i);
                }
        
                retImg.setColorTable(table);
        
                for(int i =0; i < pInputImage->width();i++)
                {
                    for(int j=0; j < pInputImage->height();j++)
                    {
                        QRgb value = pInputImage->pixel(i,j);
                        retImg.setPixel(i,j,qGray(value));
                    }
                }
                //retImg.invertPixels();  // can used to invert black to white and vice versa
                return retImg;
            }
        }
        

        Step 3: Displaying the image on QLabel

            int labelWidth = ui->label8bitImage->width();
            int labelHeight = ui->label8bitImage->height();
            QImage scaled_img = resultimage.scaled(labelWidth, labelHeight, Qt::KeepAspectRatio);
        
            ui->label8bitImage->setPixmap(QPixmap::fromImage(scaled_img,Qt::AutoColor));
        

        I'm getting the content almost similar to the original but colour and look and feel is entirely different. Please let me know what I'm missing here. If any better conversion is available, I warmly welcome that too.

        D 1 Reply Last reply
        0
        • M Offline
          M Offline
          mvuori
          wrote on last edited by
          #4

          First, look and feel is a very vague thing. You should try to find something concrete, such as that the image is too light, too contrasty or something.

          I think you have not assigned a color table to the input image, which it should need just like the output image. But wouldn't the conversion have been done with that already...

          1 Reply Last reply
          0
          • raven-worxR raven-worx

            @Ajith_P_V
            QImage::Format_Indexed8 requires a corresponding color table. If you haven't set it the result won't be what you expect.
            So you can init the color table like this:

            QImage img(imgData, imgWidth, imgHeight, QImage::Format_Indexed8);
            for( int i = 0; i < 256; ++i )
                img.setColor(i, qRgb(i,i,i));
            

            Where does your image data come from?

            L Offline
            L Offline
            LeThalois
            wrote on last edited by
            #5

            @raven-worx
            thank you for your reply ^^ helped me a lot :)

            1 Reply Last reply
            0
            • A Ajith_P_V

              @raven-worx Thank you very much for your help. I'm actually using the color table but I think I'm missing something in the conversion. I think it is better to show what I have done for the conversion (please see the below code snippet).

              Step1: Taking the 8-bit single channel grayscale raw image which is stored in the predefined path.

              QFile file8bitImage("<path of the image folder>/8bitImage.raw");
              if (!file8bitImage.open(QFile::ReadOnly))
                      return;
              QByteArray array8bitImage = file8bitImage.readAll();
                  char* buffer8bitImage = array8bitImage.data();
              
                  file8bitImage.close(); // close the file after copying the data
              QImage *image = new  QImage(reinterpret_cast<unsigned char*>(buffer8bitImage), 300, 400, QImage::Format_Indexed8);
               QImage resultimage = convertToGray(image); //calling the convert function
              

              Step 2: Convert logic for display

              QImage MainWindow::convertToGray(QImage *pInputImage)
              {
                  if ( pInputImage && !pInputImage->isNull() )
                  {
                      QImage retImg(pInputImage->width(),pInputImage->height(),QImage::Format_Indexed8);
                      QVector<QRgb> table( 256 );
                      for( int i = 0; i < 256; ++i )
                      {
                          table[i] = qRgb(i,i,i);
                      }
              
                      retImg.setColorTable(table);
              
                      for(int i =0; i < pInputImage->width();i++)
                      {
                          for(int j=0; j < pInputImage->height();j++)
                          {
                              QRgb value = pInputImage->pixel(i,j);
                              retImg.setPixel(i,j,qGray(value));
                          }
                      }
                      //retImg.invertPixels();  // can used to invert black to white and vice versa
                      return retImg;
                  }
              }
              

              Step 3: Displaying the image on QLabel

                  int labelWidth = ui->label8bitImage->width();
                  int labelHeight = ui->label8bitImage->height();
                  QImage scaled_img = resultimage.scaled(labelWidth, labelHeight, Qt::KeepAspectRatio);
              
                  ui->label8bitImage->setPixmap(QPixmap::fromImage(scaled_img,Qt::AutoColor));
              

              I'm getting the content almost similar to the original but colour and look and feel is entirely different. Please let me know what I'm missing here. If any better conversion is available, I warmly welcome that too.

              D Offline
              D Offline
              DevAmorph
              wrote on last edited by
              #6

              @Ajith_P_V , maybe, you need not RGB, but BGR model. I didn't work with graphics in qt, but in OpenCV all work was gone in BGR.

              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