How to convert specific pixel value to pseudocolor?

2 views (last 30 days)
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.
  2 Comments
Walter Roberson
Walter Roberson on 25 Nov 2012
Is the uint8 image RGB or pseudocolor?
Is the desired output RGB or pseudocolor?
When you say "with value smaller than 40", are you referring to intensity in a particular colorplane, or are you referring to brightness, or are you referring to a pseudocolor map index (which is arbitrary and has no inherent meaning) ?
wang1031 yang
wang1031 yang on 26 Nov 2012
Sorry, I did not represent my question clearly. I have an uint8 grayscal image. I want to replace all pixels with pixel intensity smaller than 40 by red color. How can i do?

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 25 Nov 2012
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);

More Answers (1)

DGM
DGM on 30 Dec 2023
Edited: DGM on 30 Dec 2023
IPT imoverlay is convenient here, and it will work fine for either I/RGB inputs. While imoverlay() wasn't available at the time, alternatives are not complicated either.
% read an image
inpict = imread('cameraman.tif');
% create a mask
mask = inpict<40;
% apply the mask
outpict = imoverlay(inpict,mask,[1 0 0]);
% that's it.
imshow(outpict,'border','tight')

Community Treasure Hunt

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

Start Hunting!