フィルターのクリア

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 Center および File ExchangeBlue についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by