Coloring a river on a grayscale image
Show older comments
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
David Goodmanson
on 22 Nov 2017
Hi Gabriel,
I killed off my answer because I had not checked it sufficiently
Gabriel Vilela
on 22 Nov 2017
What is
size(originalIM_River)
size(imRGB_River)
Does the problem still occur if you use
[nLine, nColumn, ~] = size(originalIM_River);
Gabriel Vilela
on 22 Nov 2017
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
More Answers (0)
Categories
Find more on Display Image 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!