MATLAB: Draw pixels within threshold interval

opacitythreshold interval

I set threshold to [min max] interval. How to plot only certain pixels within that range?
Later on, I would use this as one of the layers in the image. The idea is to interpolate colormap within [min max] interval and obtain/transmit those pixels. Every other pixel should be set to zero opacity.
Thanks in advance.

Best Answer

Try this demo. It uses the thresholds to get a mask than erases outside that mask. Then it makes a colormap and applies it only within the threshold range. Adapt as needed.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 22;
%--------------------------------------------------------------------------------------------------------





% READ IN IMAGE
folder = pwd;
baseFileName = 'cameraman.tif';
grayImage = imread(baseFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = rgb2gray(grayImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
% grayImage = grayImage(:, :, 2); % Take green channel.
end
%--------------------------------------------------------------------------------------------------------
% Display the image with the original, linear colormap.
subplot(2, 2, 1);
cmap = jet(256);
imshow(grayImage, 'Colormap', cmap);
axis('on', 'image');
title('Original Color Map', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
colorbar;
hFig = gcf;
hFig.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.
drawnow;
set(gca,'ColorScale','log')
%--------------------------------------------------------------------------------------------------------
% Show the histogram:
subplot(2, 2, 2);
imhist(grayImage);
grid on;
title('Histogram', 'FontSize', fontSize, 'Interpreter', 'None');
minGL = 150;
maxGL = 190;
xline(minGL, 'Color', 'r', 'LineWidth', 2);
xline(maxGL, 'Color', 'r', 'LineWidth', 2);
%--------------------------------------------------------------------------------------------------------
% BINARIZE THE IMAGE
mask = grayImage >= minGL & grayImage <= maxGL;
%--------------------------------------------------------------------------------------------------------
% Display the mask.
subplot(2, 2, 3);
imshow(mask);
axis('on', 'image');
title('Mask', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
colorbar;
drawnow;
%--------------------------------------------------------------------------------------------------------
% Apply the mask
maskedImage = grayImage; % Initialize
maskedImage(~mask) = 0; % Erase outside of mask
subplot(2, 2, 4);
imshow(maskedImage, 'Colormap', cmap);
axis('on', 'image');
caption = sprintf('Masked Image with color map applied between %d and %d', minGL, maxGL);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
colorbar;
caxis([minGL, maxGL]);
drawnow;