Clear Filters
Clear Filters

geotiffwrite changes ColorType from truecolor to grayscale

3 views (last 30 days)
I am importing a geotiff to Matlab (2022b) using readgeoraster, applying a small XY shift to R.XWorldLimits and R.YWorldLimits, then exporting it using geotiffwrite. I do not change any other values.
Using geotiffinfo on the input and output geotiffs, the only variable that is different is that ColorType has been changed from truecolor to grayscale. Is this a bug and/or is there a way to preserve the original variable value?

Answers (1)

Himanshu
Himanshu on 1 Sep 2023
Hello Rob,
I understand that you are facing an issue while importing a "geotiff" to MATLAB (2022b) and making a slight XY shift to "R.XWorldLimits" and "R.YWorldLimits" and then exporting it using "geotiffwrite".
The change in "ColorType" from "truecolor" to "grayscale" occurs because "geotiffwrite" function is optimizing the output geotiff based on the data it receives.
To preserve the "ColorType" change from "truecolor" to "grayscale", you can ensure that the image data is in the correct format (uint8 or uint16). To address this issue, you can use the following code:
% generate a sample truecolor image (3 channels)
image_data = cat(3, rand(100, 100), rand(100, 100), rand(100, 100));
% create a referencing object (geographic coordinates)
R = georasterref( ...
'RasterSize', size(image_data), ...
'RasterInterpretation', 'cells', ...
'LatitudeLimits', [0, 1], ...
'LongitudeLimits', [0, 1]);
% display the original truecolor image
figure;
imshow(image_data);
title('Original Truecolor Image');
% export the truecolor image with a small XY shift
shift_x = 0.01;
shift_y = 0.02;
% apply the shift to latitude and longitude limits
R.LatitudeLimits = R.LatitudeLimits + shift_y;
R.LongitudeLimits = R.LongitudeLimits + shift_x;
% ensure that image_data is in the correct format (uint8 or uint16)
image_data = uint8(image_data * 255);
% export the shifted truecolor image
geotiffwrite('output_truecolor.tif', image_data, R);
% check "ColorType" before and after export
info_before = geotiffinfo('output_truecolor.tif');
fprintf('ColorType before export (truecolor): %s\n', info_before.ColorType);
% load the exported image
image_data_exported = imread('output_truecolor.tif');
info_after = geotiffinfo('output_truecolor.tif');
fprintf('ColorType after export (truecolor): %s\n', info_after.ColorType);
% display the exported truecolor image
figure;
imshow(image_data_exported);
title('Exported Truecolor Image');
You can refer to the below documentation to learn more about the "geotiffwrite", "geotiffinfo", and "readgeoraster" functions in MATLAB.

Categories

Find more on Ultrasound Imaging in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!