constructing image using img.setpixel(x,y,qRgb())
-
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.
-
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). -
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 ? -
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.
@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
andy
values might not do what you think they do?!
YourX
is always 0. (i
is always 0, and so isx = i mod 2
)In addition:
See whatqRgb
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
-
@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?
-
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 ?
-
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 ?
@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...
-
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 ?
@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 constructedQRgb
triplet.Then you will get the same color values as in your three vectors.
SettingqRgb
to your image won't change anything as you thought at first. Everything works as expected.The issue with your current
qDebug()
output is thatqRgb(r,g,b)
will always add the alpha valuea
(= 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
-
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,121constructed image:
pixel-1: 134,211,135
pixel-2: 111,188,172
pixel-3: 85,162,146
pixel-4: 72, 149, 133how should i modify the the code in order to have my actual pixel values and constructed pixel values to be same ?
-
@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...
@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...
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 -
@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...
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 -
@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...
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@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 callassert()
fromcassert.h