Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Display 2D array of type double* as image
I have a pointer to my image pixel values represented as double data type. I want to display it as a image in Qt. Is the QImage::QImage suitable for this task? If yes, how can I convert double * to uchar * ? Moreover, how to show image? I tried all the provided maps but no one displays it well.
@hei6775 What image format is that?
You would use https://doc.qt.io/qt-5/qimage.html#fromData to load binary data as image, but you have to know what format that is. Here is a list of supported formats: https://doc.qt.io/qt-5/qimagereader.html#supportedImageFormats
If your format is not supported you will have to convert the data to a supported format first.
@jsulm hi, the data's format is double in C++. I am doing something about simulation. I will create 2D matrix of double. The double type is 8-byte, right?
I don't know how to deal with the convertion.
I am new to Qt and C++ in general.
JonB last edited by JonB
I don't know of an image format (supported by Qt, at least) which takes any
doubles as an image format. Indeed, we don't know what that format actually is.
If you need to convert an array of
doubles to, say, an array of
ints, declare the
intarray with the same dimensions as the double array and iterate through every
doublevalue converting it to
intin the new array.
I don't know whether a library like boost might contain a function to do this any faster than this. Be aware that if you have "large" images/you need to do this repeatedly I would think this could be a "slow" operation.
the data's format is double in C++
How is this data interpreted? One double per pixel? One double per base color (RGB), so 3 doubles per pixel? You have to know this if you want to use such images.
@jsulm one double per pixel. It seems that QT can't handle data of type double. Even if double is converted to a supported type, it will cause a lack of precision.
@hei6775 And how exactly does such a double represent a pixel? I mean, how is the color encoded? Do you actually have an exact format specification?
@jsulm The data is like a grayscale image, and the value of each pixel is represented by a double type. For example, if the first pixel value is 198.88, then the double value is 198.88
@hei6775 Do you really need such a precision? I mean displays have anyway limited capabilities, so I don't see why loosing some precition when converting to another (supported) format for displaying the picture would be a problem.
@jsulm Hahaha, I want to know if there is a better way to show it. If there has to be a loss of accuracy, there is no problem.
Then take a look at QImage::Format_Grayscale8/16 QImage format and convert your data to this.