Create transparent overlay imagesc for certain values

60 次查看(过去 30 天)
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