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. Extracting RGB from image
Forum Updated to NodeBB v4.3 + New Features

Extracting RGB from image

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 5 Posters 814 Views 3 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.
  • K Offline
    K Offline
    Kris Revi
    wrote on 15 Nov 2021, 14:32 last edited by
    #1

    Is it possible with Qt to take an image of a (palette) and analyze the image and extract the color information from left to right (so total width of image and only first scanline)

    RainbowForSlider.png

    Position, Red, Green, Blue
    

    like this (example)

    {
        0, 120,  0,  0,
       22, 179, 22,  0,
       51, 255,104,  0,
       85, 167, 22, 18,
      135, 100,  0, 103,
      198,  16,  0, 130,
      255,   0,  0, 160
    };
    

    if yes, how?

    R 1 Reply Last reply 15 Nov 2021, 15:18
    0
    • K Kris Revi
      15 Nov 2021, 14:32

      Is it possible with Qt to take an image of a (palette) and analyze the image and extract the color information from left to right (so total width of image and only first scanline)

      RainbowForSlider.png

      Position, Red, Green, Blue
      

      like this (example)

      {
          0, 120,  0,  0,
         22, 179, 22,  0,
         51, 255,104,  0,
         85, 167, 22, 18,
        135, 100,  0, 103,
        198,  16,  0, 130,
        255,   0,  0, 160
      };
      

      if yes, how?

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 15 Nov 2021, 15:18 last edited by
      #2

      @Kris-Revi
      https://doc.qt.io/qt-5/qimage.html#scanLine

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

      K 1 Reply Last reply 15 Nov 2021, 16:33
      1
      • R raven-worx
        15 Nov 2021, 15:18

        @Kris-Revi
        https://doc.qt.io/qt-5/qimage.html#scanLine

        K Offline
        K Offline
        Kris Revi
        wrote on 15 Nov 2021, 16:33 last edited by
        #3

        @raven-worx
        RainbowForSlider.png

            QImage img( ":/image/palettes/Rainbow.png" );
            img.convertTo(QImage::Format_ARGB32);
        
            struct newRGB
            {
                int Pos;
                int Red;
                int Green;
                int Blue;
            };
        
            newRGB testRGB[img.width()];
        
            for(uint32_t Y = 0; Y < 1; ++Y)
            {
                uint8_t* pPixel = img.scanLine(Y);
        
                for(int X = 0; X < img.width(); ++X)
                {
                  const int Blue = *pPixel++;
                  const int Green = *pPixel++;
                  const int Red = *pPixel++;
        
                  testRGB[X].Pos = X;
                  testRGB[X].Red = Red;
                  testRGB[X].Green = Green;
                  testRGB[X].Blue = Blue;
        
                }
            }
        
            for(int i = 0; i < img.width(); ++i)
            {
                qDebug() << testRGB[i].Pos << "," << testRGB[i].Red << "," << testRGB[i].Green << "," << testRGB[i].Blue;
            }
        

        produces

        0 , 255 , 0 , 0
        1 , 4 , 0 , 255
        2 , 0 , 255 , 255
        3 , 255 , 255 , 9
        4 , 255 , 13 , 0
        5 , 18 , 0 , 255
        6 , 0 , 255 , 255
        7 , 255 , 255 , 22
        

        Pos 0 is 255 Red, that is correct! but then Pos 1 is suddenly 255 Blue with 4 on the red :S

        A R 2 Replies Last reply 15 Nov 2021, 16:39
        0
        • K Kris Revi
          15 Nov 2021, 16:33

          @raven-worx
          RainbowForSlider.png

              QImage img( ":/image/palettes/Rainbow.png" );
              img.convertTo(QImage::Format_ARGB32);
          
              struct newRGB
              {
                  int Pos;
                  int Red;
                  int Green;
                  int Blue;
              };
          
              newRGB testRGB[img.width()];
          
              for(uint32_t Y = 0; Y < 1; ++Y)
              {
                  uint8_t* pPixel = img.scanLine(Y);
          
                  for(int X = 0; X < img.width(); ++X)
                  {
                    const int Blue = *pPixel++;
                    const int Green = *pPixel++;
                    const int Red = *pPixel++;
          
                    testRGB[X].Pos = X;
                    testRGB[X].Red = Red;
                    testRGB[X].Green = Green;
                    testRGB[X].Blue = Blue;
          
                  }
              }
          
              for(int i = 0; i < img.width(); ++i)
              {
                  qDebug() << testRGB[i].Pos << "," << testRGB[i].Red << "," << testRGB[i].Green << "," << testRGB[i].Blue;
              }
          

          produces

          0 , 255 , 0 , 0
          1 , 4 , 0 , 255
          2 , 0 , 255 , 255
          3 , 255 , 255 , 9
          4 , 255 , 13 , 0
          5 , 18 , 0 , 255
          6 , 0 , 255 , 255
          7 , 255 , 255 , 22
          

          Pos 0 is 255 Red, that is correct! but then Pos 1 is suddenly 255 Blue with 4 on the red :S

          A Offline
          A Offline
          artwaw
          wrote on 15 Nov 2021, 16:39 last edited by
          #4

          @Kris-Revi

          uchar *QImage::scanLine(int i)
          
          Returns a pointer to the pixel data at the scanline with index i. The first scanline is at index 0.
          
          The scanline data is as minimum 32-bit aligned. For 64-bit formats it follows the native alignment of 64-bit integers (64-bit for most platforms, but notably 32-bit on i386).
          
          Warning: If you are accessing 32-bpp image data, cast the returned pointer to QRgb* (QRgb has a 32-bit size) and use it to read/write the pixel value. You cannot use the uchar* pointer directly, because the pixel format depends on the byte order on the underlying platform. Use qRed(), qGreen(), qBlue(), and qAlpha() to access the pixels.
          
          See also bytesPerLine(), bits(), Pixel Manipulation, and constScanLine().
          

          So I would suggest following the documentation and sticking with qRGB approach? For alpha aware images there is also separate type.

          For more information please re-read.

          Kind Regards,
          Artur

          1 Reply Last reply
          0
          • O Offline
            O Offline
            ollarch
            wrote on 15 Nov 2021, 16:51 last edited by
            #5

            Instead of using scanLine you can use
            https://doc.qt.io/qt-5/qimage.html#pixel
            or also
            https://doc.qt.io/qt-5/qimage.html#pixelColor

            1 Reply Last reply
            0
            • K Kris Revi
              15 Nov 2021, 16:33

              @raven-worx
              RainbowForSlider.png

                  QImage img( ":/image/palettes/Rainbow.png" );
                  img.convertTo(QImage::Format_ARGB32);
              
                  struct newRGB
                  {
                      int Pos;
                      int Red;
                      int Green;
                      int Blue;
                  };
              
                  newRGB testRGB[img.width()];
              
                  for(uint32_t Y = 0; Y < 1; ++Y)
                  {
                      uint8_t* pPixel = img.scanLine(Y);
              
                      for(int X = 0; X < img.width(); ++X)
                      {
                        const int Blue = *pPixel++;
                        const int Green = *pPixel++;
                        const int Red = *pPixel++;
              
                        testRGB[X].Pos = X;
                        testRGB[X].Red = Red;
                        testRGB[X].Green = Green;
                        testRGB[X].Blue = Blue;
              
                      }
                  }
              
                  for(int i = 0; i < img.width(); ++i)
                  {
                      qDebug() << testRGB[i].Pos << "," << testRGB[i].Red << "," << testRGB[i].Green << "," << testRGB[i].Blue;
                  }
              

              produces

              0 , 255 , 0 , 0
              1 , 4 , 0 , 255
              2 , 0 , 255 , 255
              3 , 255 , 255 , 9
              4 , 255 , 13 , 0
              5 , 18 , 0 , 255
              6 , 0 , 255 , 255
              7 , 255 , 255 , 22
              

              Pos 0 is 255 Red, that is correct! but then Pos 1 is suddenly 255 Blue with 4 on the red :S

              R Offline
              R Offline
              raven-worx
              Moderators
              wrote on 15 Nov 2021, 16:51 last edited by
              #6

              @Kris-Revi said in Extracting RGB from image:

              Pos 0 is 255 Red, that is correct! but then Pos 1 is suddenly 255 Blue with 4 on the red :S

              dont increment the pointer for each color, but only for each position

              const int Blue = *pPixel++;
                      const int Green = *pPixel++;
                        const int Red = *pPixel++;
              

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

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on 15 Nov 2021, 18:17 last edited by
                #7

                Hi,

                @Kris-Revi said in Extracting RGB from image:

                QImage::Format_ARGB32

                Just in case: this is a 4 channels image. You completely ignore the A in your loop.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                K 1 Reply Last reply 15 Nov 2021, 18:54
                1
                • SGaistS SGaist
                  15 Nov 2021, 18:17

                  Hi,

                  @Kris-Revi said in Extracting RGB from image:

                  QImage::Format_ARGB32

                  Just in case: this is a 4 channels image. You completely ignore the A in your loop.

                  K Offline
                  K Offline
                  Kris Revi
                  wrote on 15 Nov 2021, 18:54 last edited by
                  #8

                  @SGaist oh yea! it should be Format_RGB32

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 15 Nov 2021, 19:23 last edited by
                    #9

                    Even so, if you read the description of the format, the A channel is locked at 0xFF.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    1

                    1/9

                    15 Nov 2021, 14:32

                    • Login

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