Converting 32 bit to uint16

13 vues (au cours des 30 derniers jours)
Tevin
Tevin le 11 Avr 2024
Commenté : DGM le 2 Mai 2024
I want to convert 32bit image to uint16 image but preserve the grey values. I do not want to lose the grey value, rather scale them to the uint16. How can I do this. My code below makes the images fully grey.
% Normalize the pixel values to the range [0, 1]
img_normalized = double(img_original) / double(max(img_original(:)));
% Find the minimum and maximum values in the normalized image
min_val = min(img_normalized(:));
max_val = max(img_normalized(:));
% Define the minimum and maximum values for uint16
min_uint16 = double(intmin('uint16'));
max_uint16 = double(intmax('uint16'));
% Calculate the scaling factors
scale_factor = (max_uint16 - min_uint16) / (max_val - min_val);
% Scale the pixel values to uint16 range
img_scaled = uint16((img_normalized - min_val) * scale_factor + min_uint16);

Réponses (2)

Abhishek Chakram
Abhishek Chakram le 2 Mai 2024
Hi Tevin,
To convert a 32-bit image to a uint16 image while preserving the grayscale values, you need to scale the pixel values appropriately. The goal is to map these values to the 0-65535 range, since uint16 can represent values in this range. Here is a sample code to do that:
% Read a 32-bit image. Replace 'your_image_path.png' with the actual path to your image.
% This assumes the image is stored in a format that supports 32-bit data (e.g., PNG, TIFF).
img32 = imread('your_image_path.png');
% Convert the image to double precision for processing, assuming it's not already
img32Double = double(img32);
% If your image is in a floating-point format with values ranging from 0 to 1,
% you can skip this step. If it's a 32-bit integer format, you should scale it to [0, 1]:
% img32Double = img32Double / double(intmax('uint32'));
% Scale the values from the [0, 1] range to [0, 65535] range for uint16
% Adjust this line if your original image uses a different scale
img16 = uint16(img32Double * 65535);
% Save the 16-bit image. Choose an appropriate format that supports 16-bit data.
imwrite(img16, 'your_output_image_path.tif');
You can refer to the following documentation to know more about functions used:
Best Regards,
Abhishek Chakram
  1 commentaire
DGM
DGM le 2 Mai 2024
I don't know why you're commenting out the scaling operation, since it's the actual part that's required. Otherwise, this just results in a grossly truncated (i.e. completely destroyed) image.

Connectez-vous pour commenter.


DGM
DGM le 2 Mai 2024
MIMT imcast() supports int32 and uint32 images, and will perform the appropriate scaling and casting in one go.
% uint32 RGB image
inpict = imread('peppers_uint32.tiff');
% convert to uint16
outpict = imcast(inpict,'uint16');
This behaves as does im2uint8(), im2uint16(), etc. Black and white are the reference points used for the scaling, not the image extrema, so the relative gray levels don't change.

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by