Azzera filtri
Azzera filtri

How to prevent the saved images become uint8

12 visualizzazioni (ultimi 30 giorni)
Niko
Niko il 15 Set 2023
Commentato: Niko il 20 Set 2023
Dear Admin,
This is my code:
filename = 'D:\files.txt';
grayImage = transpose(importdata(filename));
a = readmatrix(filename);
imwrite(a', 'files.png');
I want to read a 2D array (512x512 with double type) from a matrix file named ‘files.txt’. However, when I try to save this matrix as an image, the data type of the image becomes uint8.
My problem is how to preserve the original data type (double) from ‘files.txt’ when saving it as an image?
Regards,
  2 Commenti
Dyuman Joshi
Dyuman Joshi il 15 Set 2023
That's not possible.
The default behaviour when using imwrite is to convert the bit depth to uint8. Exceptions include some image types that can be saved as uint16.
As your file is a 2D array of double type, I assume it is a grayscale image.
From the documentation of imwrite - "If A is a grayscale or RGB color image of data type double or single, then imwrite assumes that the dynamic range is [0, 1] and automatically scales the data by 255 before writing it to the file as 8-bit values."
Niko
Niko il 16 Set 2023
Dear Dyuman,
Thank you for your explanation. I appreciate your insight into the default behavior of imwrite and its conversion to 8-bit values. I had previously read the documentation and was aware of this behavior.
However, I was wondering if there might be a way to save the image as a double type in JPEG or PNG format. I understand from your explanation that this might not be possible due to the limitations of these formats, but I thought I’d ask just in case there might be a workaround or alternative method. Thank you again for your assistance.
Regards,

Accedi per commentare.

Risposte (1)

Walter Roberson
Walter Roberson il 15 Set 2023
PNG has no support for floating point pixels.
TIFF has support for floating point, but the imwrite() interface to TIFF does not support that.
See https://www.mathworks.com/matlabcentral/fileexchange/30519-export-image-to-tif-or-tiff-file-of-selected-data-type for a File Exchange contribution that knows how to deal with the various different TIFF output types. (Except possibly COMPLEX)
  5 Commenti
DGM
DGM il 17 Set 2023
I think it's worth emphasizing that the solution depends less on MATLAB and more on whatever other application you expect to read the file. Most image viewers and editors (at least that I've tried) won't open a floating-point TIFF (or at least not correctly).
Similarly, you might be able to typecast the data and pack it into a PNG for transport, but most other applications will have no way of reversing the process, and any modifications performed on the packed image will destroy it.
If the target application supports no formats that are capable of floating-point, then that's that. If there is no target application, and all you are concerned with is using it in MATLAB, then why not use a MAT file or something? A MAT file will be reasonably compact and convenient to use -- at least within MATLAB.
Niko
Niko il 20 Set 2023
Image Analyst: Thank you for your response. I have tried the save function and also looked into the JPEG file format on Wikipedia. The reason I want to use floating point pixel values is because I believe it will provide more precise data than the uint8 pixel values. I plan to import these images into Lumerical Ansys, which only accepts .jpg or .png image types. Unfortunately, .tiff files cannot be imported into Lumerical Ansys. I understand from your response that JPEG may not support floating point pixel values, but I’m wondering if there’s a workaround or an alternative format that might be compatible with Lumerical Ansys.
Walter Roberson: I appreciate your detailed explanation and the code you provided. I was able to successfully generate a .tiff image with float double data. Your information regarding which image file formats support floating point was very helpful. Your suggestion about typecasting the 64-bit double data to ‘uint8’ for PNG files is interesting, and I’m considering giving it a try. However, I’m concerned about the potential loss of precision during the conversion process. Do you think this could be a significant issue?
DGM: Thank you for your insights. Like you, I have tried opening images with double point pixel values in various applications without success. My goal is to use these float files in Lumerical Ansys. Your suggestion about using a MAT file within MATLAB is a good one, but unfortunately, it doesn’t solve my problem with Lumerical Ansys. Do you have any other suggestions for formats or methods that might be compatible with this software?

Accedi per commentare.

Categorie

Scopri di più su Convert Image Type in Help Center e File Exchange

Prodotti


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by