![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/471662/image.png)
Draw pixels within threshold interval
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Janez Novak
il 27 Dic 2020
Risposto: Image Analyst
il 27 Dic 2020
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.
0 Commenti
Risposta accettata
Image Analyst
il 27 Dic 2020
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;
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/471662/image.png)
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Blue in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!