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. how to knoe the pixel value of a QImage or QPixmap
Qt 6.11 is out! See what's new in the release blog

how to knoe the pixel value of a QImage or QPixmap

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 4.6k Views 1 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.
  • AlvaroSA Offline
    AlvaroSA Offline
    AlvaroS
    wrote on last edited by
    #1

    hello everyone.
    First of all thanks a lot for reading this post and being able to help.

    I would like to sve an image with pixel values from a QImage.

    My code looks like this:

                QImage img1(path_name_image); //Image creation //Ver como hacer con variable global.
    
                //Create matrix where each element will be a pixel value.
                matrix = new int *[img.width()];
    
                for(int i=0;i<img.width();++i)
                    matrix[i] = new int [img.height()];
    
                for(int i=0;i<img.width();++i){
                    for(int j=0;j<img.height();++j){
                        matrix[i][j] = QColor(img.pixel(i,j)).black() > 127 ? 0:1;
                        std::cout<<img1.pixel(i,j)<<std::endl;
                        std::cout<<matrix[i][j]<<std::endl;
                    }
                }
    

    But std::cout gives me values out of 0-256

    I think the problem is that .pixel(i,j) returns the color of the pixel at coordinates (x, y), but coordinates of scene I think....

    How can I get a matrix with pixel values right?

    raven-worxR VRoninV 2 Replies Last reply
    0
    • AlvaroSA AlvaroS

      hello everyone.
      First of all thanks a lot for reading this post and being able to help.

      I would like to sve an image with pixel values from a QImage.

      My code looks like this:

                  QImage img1(path_name_image); //Image creation //Ver como hacer con variable global.
      
                  //Create matrix where each element will be a pixel value.
                  matrix = new int *[img.width()];
      
                  for(int i=0;i<img.width();++i)
                      matrix[i] = new int [img.height()];
      
                  for(int i=0;i<img.width();++i){
                      for(int j=0;j<img.height();++j){
                          matrix[i][j] = QColor(img.pixel(i,j)).black() > 127 ? 0:1;
                          std::cout<<img1.pixel(i,j)<<std::endl;
                          std::cout<<matrix[i][j]<<std::endl;
                      }
                  }
      

      But std::cout gives me values out of 0-256

      I think the problem is that .pixel(i,j) returns the color of the pixel at coordinates (x, y), but coordinates of scene I think....

      How can I get a matrix with pixel values right?

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

      @AlvaroS
      you receive values from 0-255 (QRgb) because it returns a 8-bit RGB values (quad-truplets).
      If you want the specific color pass the returned value to QColor constructor or use qAlpha()/qRed()/qBlue()/... etc. on the value.

      --- 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
      • M Offline
        M Offline
        michelson
        wrote on last edited by
        #3

        You can try using QImage::pixelColor()
        http://doc.qt.io/qt-5/qimage.html#pixelColor

        1 Reply Last reply
        0
        • AlvaroSA AlvaroS

          hello everyone.
          First of all thanks a lot for reading this post and being able to help.

          I would like to sve an image with pixel values from a QImage.

          My code looks like this:

                      QImage img1(path_name_image); //Image creation //Ver como hacer con variable global.
          
                      //Create matrix where each element will be a pixel value.
                      matrix = new int *[img.width()];
          
                      for(int i=0;i<img.width();++i)
                          matrix[i] = new int [img.height()];
          
                      for(int i=0;i<img.width();++i){
                          for(int j=0;j<img.height();++j){
                              matrix[i][j] = QColor(img.pixel(i,j)).black() > 127 ? 0:1;
                              std::cout<<img1.pixel(i,j)<<std::endl;
                              std::cout<<matrix[i][j]<<std::endl;
                          }
                      }
          

          But std::cout gives me values out of 0-256

          I think the problem is that .pixel(i,j) returns the color of the pixel at coordinates (x, y), but coordinates of scene I think....

          How can I get a matrix with pixel values right?

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4

          @AlvaroS said:

          My code looks like this:

                      QImage img1(path_name_image); //Image creation //Ver como hacer con variable global.
                              matrix[i][j] = QColor(img.pixel(i,j)).black() > 127 ? 0:1;
                              std::cout<<img1.pixel(i,j)<<std::endl;
          

          What is img? you declare only img1

          currently cout will print the QRgb Value of the colour and matrix will contain 1 if the pixel "blackness" (black component of cmyk encoding) is less than 50% or 0 otherwise

          @AlvaroS said:

          I think the problem is that .pixel(i,j) returns the color of the pixel at coordinates (x, y), but coordinates of scene I think....

          x and y are coordinates of the image (assuming img is a QImage)

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          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