Suppose a binary image is considered, which consists of two colors. Is it possible to assign red color to the white portions in the image?
MATLAB: How to color a binary image
assign colorImage Processing Toolbox
Related Solutions
See this demo:
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
clearvars;format longg;format compact;fontSize = 20;% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');baseFileName = 'peppers.png';% 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; endendrgbImage = 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]);% Find where the average is more than 250.
grayImage = rgb2gray(rgbImage);% Display the image.
subplot(2, 2, 2);imshow(grayImage);title('Gray Image', 'FontSize', fontSize);% Binarize (threshold) the image to find
% where the average is brighter than some threshold value.
thresholdValue = 200; % Change to whatever value you want.
binaryImage = grayImage > thresholdValue;% Display the image.subplot(2, 2, 3);imshow(binaryImage);caption = sprintf('Binary Image = gray image > %d', thresholdValue);title(caption, 'FontSize', fontSize);% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);greenChannel = rgbImage(:, :, 2);blueChannel = rgbImage(:, :, 3);% Make binary pixels red.
redChannel(binaryImage) = 255;greenChannel(binaryImage) = 0;blueChannel(binaryImage) = 0;% Get RGB image again.
newRGB = cat(3, redChannel, greenChannel, blueChannel);% Display the image.subplot(2, 2, 4);imshow(newRGB);title('New RGB Image', 'FontSize', fontSize);
Set up a colormap and apply it. For example (untested)
grayImage = imread('cameraman.tif');imshow(grayImage);cmap = gray(256);% Make gray levels 150 - 170 red
cmap(151:191, 1) = 1;cmap(151:191, 2) = 0;cmap(151:191, 3) = 0;colormap(gca, cmap);colorbar;hp = impixelinfo();
Best Answer