Display and calibrate grayscale from QImage to Qt

I managed to display the image in the label in Qt using something like the following:

transformPixels(0,0,1,imheight,imwidth,1);//sets unsigned char** imageData unsigned char* fullCharArray = new unsigned char[imheight * imwidth]; for (int i = 0 ; i < imheight ; i++) for (int j = 0 ; j < imwidth ; j++) fullCharArray[(i*imwidth)+j] = imageData[i][j]; QImage *qi = new QImage(fullCharArray, imwidth, imheight, QImage::Format_RGB32); ui->viewLabel->setPixmap(QPixmap::fromImage(*qi,Qt::AutoColor)); 

So, fullCharArray is an array of unsigned characters that were mapped from an imageData 2D array, in other words, it is imheight * imwidth bytes.

The problem is that only part of my image is displayed on the label. The image is very large. I would like to display the full image, reduced to fit the label, while maintaining the aspect ratio.

Also, this QImage format was the only one I could find that seemed to give me a close idea of ​​the image I want to display - is that what I should expect? I use only one byte per pixel (unsigned char - values ​​from 0 to 255), and it seems that liek RGB32 does not make much sense for this data type, but none of the others displays anything remotely correct

edit: Following dan gallaghers advice, I implemented this code:

 QImage *qi = new QImage(fullCharArray, imwidth, imheight, QImage::Format_RGB32); int labelWidth = ui->viewLabel->width(); int labelHeight = ui->viewLabel->height(); QImage small = qi->scaled(labelWidth, labelHeight,Qt::KeepAspectRatio); ui->viewLabel->setPixmap(QPixmap::fromImage(small,Qt::AutoColor)); 

But this leads to the fact that my program "unexpectedly ends" code 0

+2
source share
3 answers

QImage has a scaled member. So you want to change your setPixmap call to something like:

 QImage small = qi->scaled(labelWidth, labelHeight, Qt::KeepAspectRatio); ui->viewLabel->setPixmap(QPixmap::fromImage(small, Qt::AutoColor); 

Note that scaled does not alter the original qi image; it returns a new QImage , which is a scaled copy of the original.

Re-Edit:

To convert from 1-byte grayscale to 4-byte grayscale RGB:

  QImage qi = new QImage(imwidth, imheight, QImage::Format_RGB32); for (int i = 0; i < imheight; i++) { for (int j = 0; j < imwidth; j++) { qi->setPixel(i, j, QRgb(imageData[i][j], imageData[i][j], imageData[i][j])); } } 

Then scale qi and use a scaled copy like pixmap for viewLabel .

+5
source

Qt does not support building grayscale images directly. You need to use an 8-bit indexed color image:

 QImage * qi = new QImage(imageData, imwidth, imheight, QImage::Format_Indexed8); for(int i=0;i<256;++i) { qi->setColor(i, qRgb(i,i,i)); } 
+6
source

I also had a similar problem - QImage :: scaled returned black images. The quick work that worked in my case was to convert QImage to QPixmap, scale and convert at that time. Like this:

 QImage resultImg = QPixmap::fromImage(image) .scaled( 400, 400, Qt::KeepAspectRatio ) .toImage(); 

where the "image" is the original image. Before reading this stream, I did not know the problem with the format, but really, my 1-bit images are black and white.

Regards, Valentine Heinitz

+3
source

All Articles