MATLAB: How to convert specific pixel value to pseudocolor

pixel color

I have a uint8 image. If i want to convert all pixel with value smaller than 40 to red color. How can i do? Whould you please give me some hints.

Best Answer

Is one of these what you are looking for? Walter's right about the ambiguity so I picked a couple of possibilities (grayscale changing the image and not using a colormap, and RGB thresholding each channel individually) and programmed up a demo for you. If neither case is what you want, then you must clarify. Don't worry about the length, just copy, paste, and run, then study the code later.
clc; % Clear command window.
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = 16;
% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'football.jpg';
% Get the full filename, with path prepended.

fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.

if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.

errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 2, 1);
imshow(rgbImage, []);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Threshold each color channel to get pixels darker than 40.

redMask = redChannel < 40;
greenMask = greenChannel < 40;
blueMask = blueChannel < 40;
% Apply the mask

redChannel(redMask) = 255;
greenChannel(greenMask) = 0;
blueChannel(blueMask) = 0;
% Recombine into RGB image.

rgbImage = cat(3, redChannel, greenChannel, blueChannel);
% Display

subplot(2, 2, 2);
imshow(rgbImage, []);
title('Masked Color Image', 'FontSize', fontSize);
promptMessage = sprintf('Now we will do it for a gray scale image?');
titleBarCaption = 'Continue with gray scale example?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
if strcmp(button, 'Cancel')
return;
end
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(2, 2, 3);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Get red, green, and blue channels.
redChannel = grayImage;
greenChannel = grayImage;
blueChannel = grayImage;
% Threshold each color channel to get pixels darker than 40.
mask = grayImage < 40;
% Apply the mask
redChannel(mask) = 255;
greenChannel(mask) = 0;
blueChannel(mask) = 0;
% Recombine into RGB image.
rgbImage = cat(3, redChannel, greenChannel, blueChannel);
% Display
subplot(2, 2, 4);
imshow(rgbImage, []);
title('Masked Color Image', 'FontSize', fontSize);