PDA

View Full Version : Convert image to grayscale and get pixel values



Evo
9th May 2020, 16:13
Hello,

I have an image as a QImage object in format ARGB32. I need to convert this image to grayscale and then get access to all pixels values. I found convertToFormat(QImage::Format_Grayscale8), but if I use it I won't be able to use bits() or scanline(), right? So how do I do this?

ChristianEhrlicher
9th May 2020, 16:19
Hello,
but if I use it I won't be able to use bits() or scanline(), right?

Why should you? And if you won't do the math use QImage::pixel() (https://doc.qt.io/qt-5/qimage.html#pixel-1)

Evo
9th May 2020, 17:49
Just used these functions with the image in a different format, it was convenient.

I need to calculate the average value and this will be the end of the work with the image. But I need to do this with many images, so I would like to choose the best speed option available.
Given this fact, is there anything else I should consider?

d_stranz
9th May 2020, 18:05
Given this fact, is there anything else I should consider?

Why not use QImage::constBits()? If you have converted to 8-bit grey scale, then each character in that array will be a single pixel. It doesn't make a copy of the underlying data, so no overhead there.

If you write your own grey scale converter, you can operate on the original ARGB bits without creating a new QImage. Convert to grey scale and average in the same loop. I am sure you can find code for grey scale conversion online or in the QImage source code.

Evo
10th May 2020, 21:52
If you write your own grey scale converter, you can operate on the original ARGB bits without creating a new QImage. Convert to grey scale and average in the same loop. I am sure you can find code for grey scale conversion online or in the QImage source code.

Yes, I will do so. Thank you!

ChrisW67
15th May 2020, 02:29
There is definitely more than one way to go from RGB to grey, and which you use will depend on the application.
For examples, http://cadik.posvete.cz/color_to_gray_evaluation/