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. constructing image using img.setpixel(x,y,qRgb())
Forum Updated to NodeBB v4.3 + New Features

constructing image using img.setpixel(x,y,qRgb())

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 3 Posters 846 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.
  • P Offline
    P Offline
    Prakash08
    wrote on last edited by
    #1

    I'm constructing the image from the pixel values stored in a integer vector. i have three vectors for storing r,g,b values of each pixel and later i will retreive the same and construct the image. while retrieving from the vectors the values of 1st pixel are 36,155,92. but after constructing the image the values of the same are 187,202,39. why both are not the same ?

                ```
    
    for (int i = 0; i < 1; ++i) {
                        for(int j=0;j<2;j++){
                            int x = i % 2;
                            int y = i / 2;
                            int rVal = currentFrameRed[i];
                            int gVal = currentFrameGreen[i];
                            int bVal = currentFrameBlue[i];
                            qDebug()<<rVal<< gVal<< bVal;
                            img.setPixel(x, y, qRgb(rVal, gVal, bVal));
                            QRgb n=qRgb(rVal, gVal, bVal);
                            QString hex=QString::number(n,16);
                            qDebug()<<hex;
                            qDebug()<<qRgb(rVal, gVal, bVal);
                        }
                    }
    
    in the above code i am constructing a 2x2 image. currentFrameRed, currentFrameGreen, currentFrameBlue are the integer vectors that store color values of each pixel.  and i'm using qDebug() to verify the pixel values before and after constructing the image.
    Pl45m4P 1 Reply Last reply
    0
    • jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Do you mean qDebug()<<rVal<< gVal<< bVal prints 36,155,92 and qDebug()<<qRgb(rVal, gVal, bVal) prints 187,202,39.
      Your loops and vector access are wrong: first of all the outer loop will be iterated only once, but you will also overwrite the 0,0 pixel in the second inner loop iteration (0/2=0 and 1/2=0).

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • P Offline
        P Offline
        Prakash08
        wrote on last edited by
        #3

        the second debug statement prints a 10 digit decimal representation of hexa value, which is the returned by qRgb(rVal,gVal,bVal).
        And the loop and vector access is wrong i agree, but the problem is image pixel value is changing after constructing the image , how to do that ?

        1 Reply Last reply
        0
        • P Prakash08

          I'm constructing the image from the pixel values stored in a integer vector. i have three vectors for storing r,g,b values of each pixel and later i will retreive the same and construct the image. while retrieving from the vectors the values of 1st pixel are 36,155,92. but after constructing the image the values of the same are 187,202,39. why both are not the same ?

                      ```
          
          for (int i = 0; i < 1; ++i) {
                              for(int j=0;j<2;j++){
                                  int x = i % 2;
                                  int y = i / 2;
                                  int rVal = currentFrameRed[i];
                                  int gVal = currentFrameGreen[i];
                                  int bVal = currentFrameBlue[i];
                                  qDebug()<<rVal<< gVal<< bVal;
                                  img.setPixel(x, y, qRgb(rVal, gVal, bVal));
                                  QRgb n=qRgb(rVal, gVal, bVal);
                                  QString hex=QString::number(n,16);
                                  qDebug()<<hex;
                                  qDebug()<<qRgb(rVal, gVal, bVal);
                              }
                          }
          
          in the above code i am constructing a 2x2 image. currentFrameRed, currentFrameGreen, currentFrameBlue are the integer vectors that store color values of each pixel.  and i'm using qDebug() to verify the pixel values before and after constructing the image.
          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by
          #4

          @Prakash08 said in constructing image using img.setpixel(x,y,qRgb()):'

          for (int i = 0; i < 1; ++i) {
                              for(int j=0;j<2;j++){
                                  int x = i % 2;
                                  int y = i / 2;
          

          As @jsulm wrote, are you aware that this loop and these x and y values might not do what you think they do?!
          Your X is always 0. (i is always 0, and so is x = i mod 2)

          In addition:
          See what qRgb documentation states:

          QRgb qRgb(int r, int g, int b)

          Returns the ARGB quadruplet (255, r, g, b).

          So in your hex conversion you have a 255


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          1
          • P Offline
            P Offline
            Prakash08
            wrote on last edited by
            #5

            yeah i have255 in it

            Pl45m4P 1 Reply Last reply
            0
            • P Prakash08

              yeah i have255 in it

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by
              #6

              @Prakash08 said in constructing image using img.setpixel(x,y,qRgb()):

              yeah i have255 in it

              Can you say what's wrong and give an example what you actually get?


              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              1 Reply Last reply
              0
              • P Offline
                P Offline
                Prakash08
                wrote on last edited by
                #7

                my problem is i'm storing red, green and blue value of each pixel in respective color vectors in the form of integer. when i start constructing image with img.setpixel(x,y,qRgb(rVal,gVal,bVal) the image is being constructed with some width and height. Later after construction of image, the RGB values of each pixel is not matching with the values stored inside the vectors .. how is that ?

                jsulmJ Pl45m4P 2 Replies Last reply
                0
                • P Prakash08

                  my problem is i'm storing red, green and blue value of each pixel in respective color vectors in the form of integer. when i start constructing image with img.setpixel(x,y,qRgb(rVal,gVal,bVal) the image is being constructed with some width and height. Later after construction of image, the RGB values of each pixel is not matching with the values stored inside the vectors .. how is that ?

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Prakash08 said in constructing image using img.setpixel(x,y,qRgb()):

                  my problem

                  Your problem is that your code is wrong as already stated - are you going to ignore that? Fix the code...

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  Pl45m4P 1 Reply Last reply
                  2
                  • P Prakash08

                    my problem is i'm storing red, green and blue value of each pixel in respective color vectors in the form of integer. when i start constructing image with img.setpixel(x,y,qRgb(rVal,gVal,bVal) the image is being constructed with some width and height. Later after construction of image, the RGB values of each pixel is not matching with the values stored inside the vectors .. how is that ?

                    Pl45m4P Offline
                    Pl45m4P Offline
                    Pl45m4
                    wrote on last edited by Pl45m4
                    #9

                    @Prakash08 said in constructing image using img.setpixel(x,y,qRgb()):

                    my problem is i'm storing red, green and blue value of each pixel in respective color vectors in the form of integer. when i start constructing image with img.setpixel(x,y,qRgb(rVal,gVal,bVal) the image is being constructed with some width and height. Later after construction of image, the RGB values of each pixel is not matching with the values stored inside the vectors .. how is that ?

                    You are misunderstanding what qRgb(r, g, b) does. Read that part of the documentation I've linked above.

                    Besides that your loop and indices are wrong, your code is correct, but you are just interpreting it wrong.

                    Change the last line of debug from

                    qDebug() << qRgb(rVal, gVal, bVal);
                    

                    to

                    qDebug() << qRed(n) << qGreen(n) << qBlue(n);
                    

                    where n is your constructed QRgb triplet.

                    Then you will get the same color values as in your three vectors.
                    Setting qRgb to your image won't change anything as you thought at first. Everything works as expected.

                    The issue with your current qDebug() output is that qRgb(r,g,b) will always add the alpha value a (= 255) as the code shows:

                    inline Q_DECL_CONSTEXPR QRgb qRgb(int r, int g, int b)// set RGB value
                    { return (0xffu << 24) | ((r & 0xffu) << 16) | ((g & 0xffu) << 8) | (b & 0xffu); }
                    

                    That's also why your hex string will start with ff


                    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                    ~E. W. Dijkstra

                    1 Reply Last reply
                    2
                    • P Offline
                      P Offline
                      Prakash08
                      wrote on last edited by
                      #10
                                  int i=0;
                                  for (int j = 0; j < 2; ++j) {
                                      for(int k=0;k<2;k++){
                                          int x = j;
                                          int y = k;
                                          int rVal = currentFrameRed[i];
                                          int gVal = currentFrameGreen[i];
                                          int bVal = currentFrameBlue[i];
                                          ++i;
                      
                                          qDebug()<<rVal<< gVal<< bVal;
                                          img.setPixel(x, y, qRgb(rVal, gVal, bVal));
                                          QRgb n=qRgb(rVal, gVal, bVal);
                                          QString hex=QString::number(n,16);
                                          qDebug()<<hex;
                                          qDebug() << qRed(n) << qGreen(n) << qBlue(n);
                                      }
                                  }
                      

                      i have changed the above code to set pixel at correct position. but the problem is :
                      actual pixel values:
                      pixel-1: 207,209,1131
                      pixel-2: 2,185,169
                      pixel-3: 165,133, 205
                      pixel-4: 18,180,121

                      constructed image:
                      pixel-1: 134,211,135
                      pixel-2: 111,188,172
                      pixel-3: 85,162,146
                      pixel-4: 72, 149, 133

                      how should i modify the the code in order to have my actual pixel values and constructed pixel values to be same ?

                      1 Reply Last reply
                      0
                      • jsulmJ jsulm

                        @Prakash08 said in constructing image using img.setpixel(x,y,qRgb()):

                        my problem

                        Your problem is that your code is wrong as already stated - are you going to ignore that? Fix the code...

                        Pl45m4P Offline
                        Pl45m4P Offline
                        Pl45m4
                        wrote on last edited by Pl45m4
                        #11

                        @jsulm said in constructing image using img.setpixel(x,y,qRgb()):

                        Your problem is that your code is wrong as already stated - are you going to ignore that? Fix the code...

                        @Prakash08

                        Can't believe that I've done this, but enjoy

                            QImage img(QSize(2,2), QImage::Format_RGB32);
                            QVector<int> vRed   { 20,  40,  80, 160 };
                            QVector<int> vGreen { 10,  30,  60, 120 };
                            QVector<int> vBlue  {  5,  10,  20,  40 };
                            //                   0/0, 0/1, 1/0, 1/1
                        
                            int x = 0;
                            int y = 0;
                            Q_ASSERT( (vRed.size() == vGreen.size()) && (vRed.size() == vBlue.size()) );
                            for (int i = 0; i < 4; i++){
                        
                           //     if (i >= 2)
                           //        x = 1;
                           //     else
                           //        x = 0;
                           //
                           //     int y = i % 2;
                        
                          // Even better and shorter:
                                if (i > 0)
                                    x = i % (y+i);
                                y = i % 2;
                          // for 2x2 and all colors same size
                        
                                int rVal = vRed.at(i);
                                int gVal = vGreen.at(i);
                                int bVal = vBlue.at(i);
                        
                                img.setPixel(x, y, qRgb(rVal, gVal, bVal));
                        
                                qDebug() << "\n";
                                qDebug() << "( " << x << " | " << y << " )";
                                qDebug() << "Vector RGB: " << rVal << gVal << bVal;
                                QRgb n = qRgb(rVal, gVal, bVal);
                                QString hex = QString::number( n,16 );
                                qDebug() << hex;
                                qDebug() << "Pixel RGB: " << qRed(n) << qGreen(n) << qBlue(n);
                            }
                        

                        Impossible to solve, right? :)
                        Try to understand why your loop was still wrong. You corrected the output now after my comment, but as @jsulm said two times, you had to fix your loop also (i, j, x and y).
                        My code above should work for you.

                        Output

                        (  0  |  0  )
                        Vector RGB:  20 10 5
                        "ff140a05"
                        Pixel RGB:  20 10 5
                        
                        
                        (  0  |  1  )
                        Vector RGB:  40 30 10
                        "ff281e0a"
                        Pixel RGB:  40 30 10
                        
                        
                        (  1  |  0  )
                        Vector RGB:  80 60 20
                        "ff503c14"
                        Pixel RGB:  80 60 20
                        
                        
                        (  1  |  1  )
                        Vector RGB:  160 120 40
                        "ffa07828"
                        Pixel RGB:  160 120 40
                        

                        Edit:
                        Improved loop and x/y condition


                        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                        ~E. W. Dijkstra

                        P Pl45m4P 2 Replies Last reply
                        1
                        • Pl45m4P Pl45m4

                          @jsulm said in constructing image using img.setpixel(x,y,qRgb()):

                          Your problem is that your code is wrong as already stated - are you going to ignore that? Fix the code...

                          @Prakash08

                          Can't believe that I've done this, but enjoy

                              QImage img(QSize(2,2), QImage::Format_RGB32);
                              QVector<int> vRed   { 20,  40,  80, 160 };
                              QVector<int> vGreen { 10,  30,  60, 120 };
                              QVector<int> vBlue  {  5,  10,  20,  40 };
                              //                   0/0, 0/1, 1/0, 1/1
                          
                              int x = 0;
                              int y = 0;
                              Q_ASSERT( (vRed.size() == vGreen.size()) && (vRed.size() == vBlue.size()) );
                              for (int i = 0; i < 4; i++){
                          
                             //     if (i >= 2)
                             //        x = 1;
                             //     else
                             //        x = 0;
                             //
                             //     int y = i % 2;
                          
                            // Even better and shorter:
                                  if (i > 0)
                                      x = i % (y+i);
                                  y = i % 2;
                            // for 2x2 and all colors same size
                          
                                  int rVal = vRed.at(i);
                                  int gVal = vGreen.at(i);
                                  int bVal = vBlue.at(i);
                          
                                  img.setPixel(x, y, qRgb(rVal, gVal, bVal));
                          
                                  qDebug() << "\n";
                                  qDebug() << "( " << x << " | " << y << " )";
                                  qDebug() << "Vector RGB: " << rVal << gVal << bVal;
                                  QRgb n = qRgb(rVal, gVal, bVal);
                                  QString hex = QString::number( n,16 );
                                  qDebug() << hex;
                                  qDebug() << "Pixel RGB: " << qRed(n) << qGreen(n) << qBlue(n);
                              }
                          

                          Impossible to solve, right? :)
                          Try to understand why your loop was still wrong. You corrected the output now after my comment, but as @jsulm said two times, you had to fix your loop also (i, j, x and y).
                          My code above should work for you.

                          Output

                          (  0  |  0  )
                          Vector RGB:  20 10 5
                          "ff140a05"
                          Pixel RGB:  20 10 5
                          
                          
                          (  0  |  1  )
                          Vector RGB:  40 30 10
                          "ff281e0a"
                          Pixel RGB:  40 30 10
                          
                          
                          (  1  |  0  )
                          Vector RGB:  80 60 20
                          "ff503c14"
                          Pixel RGB:  80 60 20
                          
                          
                          (  1  |  1  )
                          Vector RGB:  160 120 40
                          "ffa07828"
                          Pixel RGB:  160 120 40
                          

                          Edit:
                          Improved loop and x/y condition

                          P Offline
                          P Offline
                          Prakash08
                          wrote on last edited by
                          #12

                          @Pl45m4 said in constructing image using img.setpixel(x,y,qRgb()):

                          Q_ASSERT( (vRed.size() == vGreen.size()) && (vRed.size() == vBlue.size()) );

                          may I know why this line is used ?

                          1 Reply Last reply
                          0
                          • Pl45m4P Pl45m4

                            @jsulm said in constructing image using img.setpixel(x,y,qRgb()):

                            Your problem is that your code is wrong as already stated - are you going to ignore that? Fix the code...

                            @Prakash08

                            Can't believe that I've done this, but enjoy

                                QImage img(QSize(2,2), QImage::Format_RGB32);
                                QVector<int> vRed   { 20,  40,  80, 160 };
                                QVector<int> vGreen { 10,  30,  60, 120 };
                                QVector<int> vBlue  {  5,  10,  20,  40 };
                                //                   0/0, 0/1, 1/0, 1/1
                            
                                int x = 0;
                                int y = 0;
                                Q_ASSERT( (vRed.size() == vGreen.size()) && (vRed.size() == vBlue.size()) );
                                for (int i = 0; i < 4; i++){
                            
                               //     if (i >= 2)
                               //        x = 1;
                               //     else
                               //        x = 0;
                               //
                               //     int y = i % 2;
                            
                              // Even better and shorter:
                                    if (i > 0)
                                        x = i % (y+i);
                                    y = i % 2;
                              // for 2x2 and all colors same size
                            
                                    int rVal = vRed.at(i);
                                    int gVal = vGreen.at(i);
                                    int bVal = vBlue.at(i);
                            
                                    img.setPixel(x, y, qRgb(rVal, gVal, bVal));
                            
                                    qDebug() << "\n";
                                    qDebug() << "( " << x << " | " << y << " )";
                                    qDebug() << "Vector RGB: " << rVal << gVal << bVal;
                                    QRgb n = qRgb(rVal, gVal, bVal);
                                    QString hex = QString::number( n,16 );
                                    qDebug() << hex;
                                    qDebug() << "Pixel RGB: " << qRed(n) << qGreen(n) << qBlue(n);
                                }
                            

                            Impossible to solve, right? :)
                            Try to understand why your loop was still wrong. You corrected the output now after my comment, but as @jsulm said two times, you had to fix your loop also (i, j, x and y).
                            My code above should work for you.

                            Output

                            (  0  |  0  )
                            Vector RGB:  20 10 5
                            "ff140a05"
                            Pixel RGB:  20 10 5
                            
                            
                            (  0  |  1  )
                            Vector RGB:  40 30 10
                            "ff281e0a"
                            Pixel RGB:  40 30 10
                            
                            
                            (  1  |  0  )
                            Vector RGB:  80 60 20
                            "ff503c14"
                            Pixel RGB:  80 60 20
                            
                            
                            (  1  |  1  )
                            Vector RGB:  160 120 40
                            "ffa07828"
                            Pixel RGB:  160 120 40
                            

                            Edit:
                            Improved loop and x/y condition

                            Pl45m4P Offline
                            Pl45m4P Offline
                            Pl45m4
                            wrote on last edited by Pl45m4
                            #13

                            @Prakash08 said in constructing image using img.setpixel(x,y,qRgb()):

                            Q_ASSERT( (vRed.size() == vGreen.size()) && (vRed.size() == vBlue.size()) );

                            may I know why this line is used ?

                            In my updated code I used

                            for (int i = 0; i < vRed.size(); i++){
                            }
                            

                            instead of

                            
                            for (int i = 0; i < 4; i++){
                            }
                            

                            Therefore all three color vectors have to have the same length. Tried to make the code as variable as possible, to make it work for 3x3, 4x4 or other image dimensions. But then you would have to re-think the X, Y coordinate handling anyway.

                            So this code

                            Q_ASSERT( (vRed.size() == vGreen.size()) && (vRed.size() == vBlue.size()) );
                            

                            will stop/interrupt the execution of the program if the condition inside is false. In this case you gonna know directly where and why it stopped instead of crashing somewhere else randomly because the vectors are not the same size.
                            Not really needed here, but might help in other places and when you work on bigger projects.
                            Q_ASSERT is just the Qt macro to call assert() from cassert.h


                            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                            ~E. W. Dijkstra

                            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