PDA

View Full Version : How to change the values of the pixels in an Qimage?



kid17
19th November 2008, 13:23
I read an image with:


QString fileName = QFileDialog::getOpenFileName(this,
tr("Open File"), QDir::currentPath());
if (!fileName.isEmpty()) {
QImage image(fileName);

imageLabel->setPixmap(QPixmap::fromImage(image));
scaleFactor = 1.0;

printAct->setEnabled(true);
fitToWindowAct->setEnabled(true);
updateActions();

if (!fitToWindowAct->isChecked())
imageLabel->adjustSize();
}

Now I want to change the values of the pixels of this image with new values. They are contained in an matrix[][] of int representing the same image equalized with my function.
The range of values in this matrix is 0-255.
I tried with:

for( int i = 0; i < r; i++ )
for(int j = 0; j < c; j++)
image.setPixel(j,i,(uint)matrix[i][j]);

but don't work, infact if I try to view it, I get a white image
Hepl me!!Thanks.

wysota
19th November 2008, 16:44
What is the format of the image?

kid17
19th November 2008, 17:08
the format is TIF.It is grayscale

wysota
19th November 2008, 17:17
So it's probably not 24b. In that case the image uses a colour table - setPixel only determines which index of the colour table describes the colour of the image. The actual colour needs to be placed into the colour table. See QImage docs for details.

kid17
19th November 2008, 17:35
I show docs but i don't understand how solved my problem

wysota
19th November 2008, 17:59
See QImage::colorTable(), QImage::setColorTable and QImage "Image Formats" and "Pixel Manipulation" sections.

kid17
19th November 2008, 20:29
I don't understand how I can change each pixel with the new. If I change the color table at position 1from x to y,each pixel 1 will be represented by y. Mistake?

wysota
19th November 2008, 21:10
Please read the docs where there is a table and code showing how to modify an 8bit image.

kid17
23rd November 2008, 20:52
Please read the docs where there is a table and code showing how to modify an 8bit image.

Thanks.I solved my problem.