Borrar filtros
Borrar filtros

Is it possible to set the Alpha of the colour in geotiffwrite?

5 visualizaciones (últimos 30 días)
I am saving a TIFF file, as shown in the example below, and I want to set color Alpha. It seems that the geotiffwrite function in MATLAB cannot achieve this. Is there any other good solution?
%example
A1 = load('A1.mat');
cmp = jet;
R = georefcells([30 55],[100 110],size(A1));
geotiffwrite('test.tif',A1,cmp,R);

Respuesta aceptada

Angelo Yeo
Angelo Yeo el 29 de Jun. de 2023
I believe you need to convert the grey image to RGB. Then you can add the alpha channel to tiff.
load('A1.mat');
cmp = jet;
R = georefcells([30 55],[100 110],size(A1));
geotiffwrite('test.tif',A1,cmp,R);
C = uint8(254 * colormap(jet));
img = imread('test.tif');
[rimg, cimg] = size(img);
for idx = 1:rimg
for jdx = 1:cimg
pixel = double(img(idx, jdx)) + 1;
new_red_pixel = C(pixel, 1);
new_green_pixel = C(pixel, 2);
new_blue_pixel = C(pixel, 3);
new_img(idx, jdx, 1) = new_red_pixel;
new_img(idx, jdx, 2) = new_green_pixel;
new_img(idx, jdx, 3) = new_blue_pixel;
end
end
% imshow(new_img)
% I referred to here to add alpha channel for tiff images
% https://kr.mathworks.com/help/matlab/ref/tiff.html#mw_a2efd938-557e-41ad-925b-64f84a51f07b
alphaval = 0.1; % change your alpha value
alpha = 255 * ones([rimg, cimg], 'uint8') * alphaval;
data = cat(3,new_img,alpha);
t = Tiff('myfile.tif','w');
setTag(t,'Photometric',Tiff.Photometric.RGB);
setTag(t,'Compression',Tiff.Compression.None);
setTag(t,'BitsPerSample',8);
setTag(t,'SamplesPerPixel',4);
setTag(t,'SampleFormat',Tiff.SampleFormat.UInt);
setTag(t,'ExtraSamples',Tiff.ExtraSamples.Unspecified);
setTag(t,'ImageLength',numrows);
setTag(t,'ImageWidth',numcols);
setTag(t,'TileLength',32);
setTag(t,'TileWidth',32);
setTag(t,'PlanarConfiguration',Tiff.PlanarConfiguration.Chunky);
write(t,data);
close(t);
  3 comentarios
DGM
DGM el 29 de Jun. de 2023
Two things:
If the image is indexed color, then read the entire image and map, then just use ind2rgb() instead of a slow loop with a slightly different map.
load('A1.mat');
cmp = jet(256); % SPECIFY THE LENGTH
R = georefcells([30 55],[100 110],size(A1));
geotiffwrite('test.tif',A1,cmp,R); % this writes an indexed color image
[img map] = imread('test.tif'); % so actually read it as an indexed color image
new_img = ind2rgb(img,cmp); % and convert it
The second problem is the length of the colormap. Note that I explicitly specified that in the call to jet(), since the mapping won't work unless it matches the dynamic range of uint8 (or otherwise the range of indices in the indexed image). Calling jet() without any arguments can get you a map of any unpredictable length depending on whether a prior figure is open -- regardless of whether it's being currently used, and regardless of whether it's been cleared.
logo cuit
logo cuit el 30 de Jun. de 2023
Thank you very much for your reminder. It has indeed greatly improved the efficiency of the code.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Blue en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by