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. Get RGB value of pixel
Forum Updated to NodeBB v4.3 + New Features

Get RGB value of pixel

Scheduled Pinned Locked Moved Solved General and Desktop
22 Posts 4 Posters 13.8k 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.
  • O Offline
    O Offline
    ollarch
    wrote on last edited by
    #13

    @MHermann said in Get RGB value of pixel:

    YUV420P

    Well, if you want to get the pixel value on some position in the image you could get the pointer to the image bits "uchar* imageBits()" to get the bits of the internal data and then do the YUV to RGB conversion. This will be faster instead of converting the entire image to RGB, so it depends on what you want to achieve.

    M 1 Reply Last reply
    1
    • O ollarch

      @MHermann said in Get RGB value of pixel:

      YUV420P

      Well, if you want to get the pixel value on some position in the image you could get the pointer to the image bits "uchar* imageBits()" to get the bits of the internal data and then do the YUV to RGB conversion. This will be faster instead of converting the entire image to RGB, so it depends on what you want to achieve.

      M Offline
      M Offline
      MHermann
      wrote on last edited by MHermann
      #14

      @ollarch : Thanks.
      But at the moment I have the problem that I don't have a valid image.
      I get a QVideoFrame in format QVideoFrame::Format_YUV420P.
      I think I first have to convert this QVideoFrame to a QImage. And after that I want to search for red pixels in this image.
      What is your advice? Can you tell me how I can get a RGB image? Or is it possible to get a YUV image and then search there for the red pixels?

      O 1 Reply Last reply
      0
      • M MHermann

        @ollarch : Thanks.
        But at the moment I have the problem that I don't have a valid image.
        I get a QVideoFrame in format QVideoFrame::Format_YUV420P.
        I think I first have to convert this QVideoFrame to a QImage. And after that I want to search for red pixels in this image.
        What is your advice? Can you tell me how I can get a RGB image? Or is it possible to get a YUV image and then search there for the red pixels?

        O Offline
        O Offline
        ollarch
        wrote on last edited by
        #15

        @MHermann The easiest way to search for red pixels would be to convert the image to RGB first. I don't know how to do the conversion but you can search for this conversion on your search engine. It will not be so difficult to get the RGB pixels from a YUV pixels. You also can look how OpenCV does the conversion.

        Finally, for searching the red pixels, do you want only the pixels with R=255,G=0,B=0 ?

        M 1 Reply Last reply
        0
        • O ollarch

          @MHermann The easiest way to search for red pixels would be to convert the image to RGB first. I don't know how to do the conversion but you can search for this conversion on your search engine. It will not be so difficult to get the RGB pixels from a YUV pixels. You also can look how OpenCV does the conversion.

          Finally, for searching the red pixels, do you want only the pixels with R=255,G=0,B=0 ?

          M Offline
          M Offline
          MHermann
          wrote on last edited by
          #16

          @ollarch : Ok. At the moment I try to do the conversion correctly.
          I am not sure if I want to search only for 255,0,0. Does this matter? Or do you mean that I can use V component of YUV then?

          O 1 Reply Last reply
          0
          • M MHermann

            @ollarch : Ok. At the moment I try to do the conversion correctly.
            I am not sure if I want to search only for 255,0,0. Does this matter? Or do you mean that I can use V component of YUV then?

            O Offline
            O Offline
            ollarch
            wrote on last edited by
            #17

            @MHermann I was only asking. I don't know how to work directly with the YUV components. I was thinking that getting only the (255,0,0) values on a real image will get you 0 pixels as you will not get a perfect red pixels on an image(could be but it will be strange).

            M 1 Reply Last reply
            0
            • O ollarch

              @MHermann I was only asking. I don't know how to work directly with the YUV components. I was thinking that getting only the (255,0,0) values on a real image will get you 0 pixels as you will not get a perfect red pixels on an image(could be but it will be strange).

              M Offline
              M Offline
              MHermann
              wrote on last edited by
              #18

              @ollarch : Ok. Now it works. I am doing the conversion from YUV to RGB. And then I am able to get the RGB values of each pixel.

              O 1 Reply Last reply
              0
              • M MHermann

                @ollarch : Ok. Now it works. I am doing the conversion from YUV to RGB. And then I am able to get the RGB values of each pixel.

                O Offline
                O Offline
                ollarch
                wrote on last edited by
                #19

                @MHermann now that you have a solution would be great to share your solution with other people to help them if they need to implement the same.

                M 1 Reply Last reply
                1
                • O ollarch

                  @MHermann now that you have a solution would be great to share your solution with other people to help them if they need to implement the same.

                  M Offline
                  M Offline
                  MHermann
                  wrote on last edited by MHermann
                  #20

                  @ollarch : Of course. Maybe someone is interested in this:

                  QImage convertFrameYUV420ToImageRGB888(QVideoFrame vidFrame)
                  {
                      vidFrame.map(QAbstractVideoBuffer::ReadOnly);
                  
                      unsigned char* pData = (unsigned char *) vidFrame.bits();
                      unsigned char pDataRgb[vidFrame.width()*vidFrame.height()*3];
                  
                      int bytesPerLine = vidFrame.bytesPerLine();
                      int numYBytes = vidFrame.width()*vidFrame.height();
                      int numUBytes = numYBytes / 4;
                      int idxY = 0;
                      int idxU = numYBytes;
                      int idxV = numYBytes+numUBytes;
                      int dstIdx=0;
                      for (int h=0; h<vidFrame.height(); h++) {
                          for (int w=0; w<vidFrame.width(); w++) {
                  
                              idxY = h*bytesPerLine+w;
                              int y = pData[idxY];
                  
                              idxU = (int)(numYBytes+(h/2)*(bytesPerLine/2)+w/2);
                              int u = pData[idxU];
                  
                              idxV = (int)(numYBytes*1.25 + (h/2)*(bytesPerLine/2)+w/2);
                              int v = pData[idxV];
                  
                              pDataRgb[dstIdx++] = y + 1.402 * (v-128);                   // r
                              pDataRgb[dstIdx++] = y - 0.344 * (u-128) - 0.714 * (v-128); // g
                              pDataRgb[dstIdx++] = y + 1.772 * (u-128);                   // b
                          }
                      }
                  
                      vidFrame.unmap();
                  
                      QImage imgRgb (pDataRgb, vidFrame.width(), vidFrame.height(), QImage::Format_RGB888);
                      return imgRgb;
                  }
                  

                  And after that conversion go over all pixels in the image and access them via QImage::PixelColor().

                  O 1 Reply Last reply
                  0
                  • M MHermann

                    @ollarch : Of course. Maybe someone is interested in this:

                    QImage convertFrameYUV420ToImageRGB888(QVideoFrame vidFrame)
                    {
                        vidFrame.map(QAbstractVideoBuffer::ReadOnly);
                    
                        unsigned char* pData = (unsigned char *) vidFrame.bits();
                        unsigned char pDataRgb[vidFrame.width()*vidFrame.height()*3];
                    
                        int bytesPerLine = vidFrame.bytesPerLine();
                        int numYBytes = vidFrame.width()*vidFrame.height();
                        int numUBytes = numYBytes / 4;
                        int idxY = 0;
                        int idxU = numYBytes;
                        int idxV = numYBytes+numUBytes;
                        int dstIdx=0;
                        for (int h=0; h<vidFrame.height(); h++) {
                            for (int w=0; w<vidFrame.width(); w++) {
                    
                                idxY = h*bytesPerLine+w;
                                int y = pData[idxY];
                    
                                idxU = (int)(numYBytes+(h/2)*(bytesPerLine/2)+w/2);
                                int u = pData[idxU];
                    
                                idxV = (int)(numYBytes*1.25 + (h/2)*(bytesPerLine/2)+w/2);
                                int v = pData[idxV];
                    
                                pDataRgb[dstIdx++] = y + 1.402 * (v-128);                   // r
                                pDataRgb[dstIdx++] = y - 0.344 * (u-128) - 0.714 * (v-128); // g
                                pDataRgb[dstIdx++] = y + 1.772 * (u-128);                   // b
                            }
                        }
                    
                        vidFrame.unmap();
                    
                        QImage imgRgb (pDataRgb, vidFrame.width(), vidFrame.height(), QImage::Format_RGB888);
                        return imgRgb;
                    }
                    

                    And after that conversion go over all pixels in the image and access them via QImage::PixelColor().

                    O Offline
                    O Offline
                    ollarch
                    wrote on last edited by
                    #21

                    @MHermann Thank you for sharing. Now, I can suggest you some modifications to make it faster.
                    First of all, the "vidFrame" could be changed to a pointer or a const reference to avoid the copy of the QVideoFrame object(time consuming).
                    Finally, reading the Qt Docs, the "pixelColor" method "is expensive when used for massive pixel manipulations. Use constBits() or constScanLine() when many pixels needs to be read". I suggest you to access directly to the internal data bits and check the values there using data pointer offsets.

                    M 1 Reply Last reply
                    0
                    • O ollarch

                      @MHermann Thank you for sharing. Now, I can suggest you some modifications to make it faster.
                      First of all, the "vidFrame" could be changed to a pointer or a const reference to avoid the copy of the QVideoFrame object(time consuming).
                      Finally, reading the Qt Docs, the "pixelColor" method "is expensive when used for massive pixel manipulations. Use constBits() or constScanLine() when many pixels needs to be read". I suggest you to access directly to the internal data bits and check the values there using data pointer offsets.

                      M Offline
                      M Offline
                      MHermann
                      wrote on last edited by
                      #22

                      @ollarch : Thanks for your suggestions.
                      But I think I have to copy the QVideoFrame. Because I can only access the bytes of the frame while they are mapped. I am doing this conversion and further code in a separate thread. So if I map and then unmap the frame the bytes are no longer accessable.

                      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