필터 지우기
필터 지우기

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

조회 수: 5 (최근 30일)
logo cuit
logo cuit 2023년 6월 29일
댓글: logo cuit 2023년 6월 30일
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);

채택된 답변

Angelo Yeo
Angelo Yeo 2023년 6월 29일
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
DGM
DGM 2023년 6월 29일
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 2023년 6월 30일
Thank you very much for your reminder. It has indeed greatly improved the efficiency of the code.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Blue에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by