Coloring a river on a grayscale image
4 views (last 30 days)
Show older comments
Gabriel Vilela
on 22 Nov 2017
Commented: Image Analyst
on 14 Mar 2021
I'm currently trying to paint part of my image (a river) yellow but the end result is not only the river being yellow but a large yellow square right next to the image. Orginally the image is in grayscale so I've used cat() to concatenate my grayscale image (and apparently "turn" it into an RGB image?) but I'm still not sure what is actually going wrong.
originalIM_River = imread('fig_lista4_2.bmp');
figure,title('Original image'),imshow(originalIM_River)
imRGB_River = cat(3, originalIM_River, originalIM_River, originalIM_River);
[nLine, nColum] = size(originalIM_River); % Correct
%[nLine, nColumn] = size(imRGB_River); %Incorrect
for i = 1 : nLine
for j = 1 : nColumn
if imRGB_River(i,j) <= 48
imRGB_River(i,j,:) = [255,255,0]; % (255,255,0) is yellow
end
end
end
figure, title('New imagem - River painted with yellow'),imshow(imRGB_River)


I've tried to get each of the channels, and tried to change the colors of it to concatenate but still didn't work.
5 Comments
Guillaume
on 22 Nov 2017
Yes, I suspected that nColumn was 3 times as big as you somehow were using a 2d syntax on a 3d matrix.
Note that using
[height, width, ~] = size(rgb_or_gray_image)
is guaranteed to work whether or not the image is grayscale or rgb.
Accepted Answer
Image Analyst
on 22 Nov 2017
Are you interested in a vectorized solution instead of the double for loop? Just threshold to get the mask, then set each color channel using that mask to the proper values (255 or 0 depending on what color channel), then combine into an rgb image and display it. See below.
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 short g;
format compact;
fontSize = 25;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = 'MUGgbnH.png';
% Get the full filename, with path prepended.
folder = pwd
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, '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
%===============================================================================
% Read in demo image.
grayImage = imread(fullFileName);
% 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 original image.
subplot(2, 2, 1);
imshow(grayImage, []);
axis on;
caption = sprintf('Original Color Image, %s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Find the mask.
mask = grayImage <= 48;
% Display the image.
subplot(2, 2, 2);
imshow(mask, []);
axis on;
caption = sprintf('Binary Mask Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
drawnow;
% Create an RGB image making it yellow where the mask is. Yellow is (255,255,0)
redChannel = grayImage; % Initialize
redChannel(mask) = 255;
greenChannel = grayImage; % Initialize
greenChannel(mask) = 255;
blueChannel = grayImage; % Initialize
blueChannel(mask) = 0;
rgbImage = cat(3, redChannel, greenChannel, blueChannel);
% Display the image.
subplot(2, 2, 3);
imshow(rgbImage, []);
axis on;
caption = sprintf('Masked Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
drawnow;

2 Comments
Image Analyst
on 14 Mar 2021
Simply make the obvious changes:
% Create an RGB image making it red where the mask is. Red is (255,0,0)
redChannel = grayImage; % Initialize
redChannel(mask) = 255;
greenChannel = grayImage; % Initialize
greenChannel(mask) = 0;
blueChannel = grayImage; % Initialize
blueChannel(mask) = 0;
rgbImage = cat(3, redChannel, greenChannel, blueChannel);
More Answers (0)
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!