Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QT QImage pixel manipulation

I am building a QT GUI application and use QImage for opening images. My problem is that I can't figure out how to use QImage's bit() and scanline() methods to get access at per pixel level.

I've seen this post Qt QImage pixel manipulation problems but this is only for the first pixel of each row. Is this correct or I got it all wrong?

thanks in advance

like image 204
theosem Avatar asked Jan 19 '10 16:01

theosem


People also ask

What is the difference between QImage and QPixmap?

The QPixmap class is an off-screen image representation that can be used as a paint device. The QImage class provides a hardware-independent image representation that allows direct access to the pixel data, and can be used as a paint device.

How do you convert QImage to QPixmap?

A QPixmap object can be converted into a QImage using the toImage() function. Likewise, a QImage can be converted into a QPixmap using the fromImage(). If this is too expensive an operation, you can use QBitmap::fromImage() instead.

How do I display an image in Qt widget?

There isn't a widget specifically made for displaying images, but this can be done with the label widget. We do this with the pixmap property. QPixmap pic("/path/to/your/image"); ui->label->setPixmap(pic);

How do I crop an image in QT?

Since you use QPixmap, you can use its copy method and supply it with a QRect to perform the actual crop. Show activity on this post. Just use of the QPixmap's copy() functions.


1 Answers

The scanlines correspond to the the height of image, the columns correspond to the width of the image.

According to the docs, the prototype looks like uchar* QImage::scanline(int i), or a similar const version.

But, as a commenter pointed out, because the data is dependent on the machine architecture and image, you should NOT use the uchar * directly. Instead, use something like the following:

QRgb *rowData = (QRgb*)img.scanLine(row);
QRgb pixelData = rowData[col];
int red = qRed(pixelData);
like image 64
Kaleb Pederson Avatar answered Sep 21 '22 05:09

Kaleb Pederson