Create transparent overlay imagesc for certain values

조회 수: 60 (최근 30일)
Siegmund
Siegmund 2023년 6월 1일
답변: Image Analyst 2023년 6월 2일
I have an elevation tif (4669x6692 single) (First image) and I performed a wavelet transform on it (Second image).
Now I want to overlay the wavelet transform data over the original elevation tif but only showing data within a certain threshold, so basically making all the blue data in the wavelet transparent/NaN/.... I tried different methods but overall, the colormap keeps plotting the full extent of the data. How can I create an overlay so that it only plots the data within a threshold (currently between 2 - 4)? The intented outcome is the third image.
for j = 1:numel(thresholdPercentiles)
threshold = thresholdValues(j);
% Set coefficients below the threshold to zero
waveletCoefficients(waveletCoefficients < threshold) = 0;
% Create a mask for values between 2 and 4
mask = (waveletCoefficients >= 2) & (waveletCoefficients <= 4);
% Create a masked version of the data with NaN for non-masked values
maskedData = data;
maskedData(~mask) = NaN;
% Display the masked values with transparency
subplot(3, 2, j+1);
imagesc(maskedData);
colormap('jet');
colorbar;
title(sprintf('Threshold: %dth percentile', thresholdPercentiles(j)));
% Apply transparency to the unwanted data (values other than 2-4)
alphaMask = double(~mask);
alphaMask(alphaMask == 1) = 0; % Set non-masked values to transparent
% Update the colormap with transparency (alpha channel)
cmap = colormap('jet');
cmap = [cmap(:, 1:3), alphaMask]; % Update the colormap
% Apply the updated colormap with transparency
colormap(cmap);
colorbar;
end

채택된 답변

LeoAiE
LeoAiE 2023년 6월 2일
Your approach seems mostly correct, however there are some minor adjustments that you can make to achieve the desired result.
Please note that 'alpha' channel cannot be used in conjunction with a colormap. Instead, we should use 'AlphaData' attribute to handle transparency.
for j = 1:numel(thresholdPercentiles)
threshold = thresholdValues(j);
% Set coefficients below the threshold to zero
waveletCoefficients(waveletCoefficients < threshold) = 0;
% Create a mask for values between 2 and 4
mask = (waveletCoefficients >= 2) & (waveletCoefficients <= 4);
% Create a masked version of the data with NaN for non-masked values
maskedData = data;
maskedData(~mask) = NaN;
% Display the masked values with transparency
subplot(3, 2, j+1);
h = imagesc(maskedData);
colormap('jet');
colorbar;
title(sprintf('Threshold: %dth percentile', thresholdPercentiles(j)));
% Apply transparency to the unwanted data (values other than 2-4)
alphaMask = double(mask); % Set masked values to opaque
set(h, 'AlphaData', alphaMask); % Use AlphaData attribute for transparency
end
'AlphaData' attribute to apply transparency to the image handle h returned by imagesc. The value of alphaMask is 1 (opaque) for the desired values (where mask is true) and 0 (transparent) for the unwanted values (where mask is false). This should result in an image with the desired values highlighted and the rest of the values made transparent.

추가 답변 (1개)

Image Analyst
Image Analyst 2023년 6월 2일
You can use labeloverlay

카테고리

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

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by