Using ColorMap to Change Yellow Object in Image to Green

11 views (last 30 days)
Hello
Im fairly new to MatLab and am taking an elective course which required MatLab while doing my MBA (yes, totally different field). As part of the assignment, we have to use MatLab colormapping to convert the yellow object in the image below into green:
I've used the "imread" function and imported it into Matlab. I also have some experience with colormap with Matlab, but quite basic. In the hints provided, it is said that I should use the IMTOOL function to determine the range of r,g,b values for the yellow pixels and the background pixels. I'm supposed to design a color mapping algorithm that transforms the yellow object to green, but not touch any of the background at all.
In my previous exercise, we converted the 255 values to be just 1s and 0s by dividing by 255. We then created a new colormap and applied it which converted the colors. But I still have no idea how to bring this all together. I've tried some code I found from the Matlab help, but I'm not getting anywhere. My approach has been to import image into Matlab, read image matrix. For all pixels that are close to the pixels of yellow, I replace them with a generic green pixel. Is there a better way to do this? If not, how would I do this with simple MatLab code?
Also, I've checked the code in this function, and I'm 10000000% sure what they are asking me doesn't have to be so complicated: https://www.mathworks.com/matlabcentral/fileexchange/26420-simplecolordetection
Thanks Steve.

Accepted Answer

Guillaume
Guillaume on 27 Sep 2018
I don't know what your assignment is exactly trying to get you to do. They seem to be strangely formulated.
I think that by now you should know how to avoid for loops. You can operate at once on the whole image:
%don't use image as a variable name, it's already a matlab function
%img: input image loaded in matlab:
red = img(:, :, 1);
green = img(:, :, 2);
blue = img(:, :, 3);
%create a mask same size as image that indicates 'yellow' pixels
isyellow = red > 200 & red < 250 & green > 200 & green < 250 & blue > 200 & blue < 250
%set red and blue channel to 0 when image is yellow. set green channel to max
%no idea if that's what asked by the assignment
red(isyellow) = 0;
green(isyellow) = 255;
blue(isyellow) = 0;
%recombine all channels
newimg = cat(3, red, green, blue);
imshow(newimg);
  2 Comments
whatchamacallit
whatchamacallit on 30 Sep 2018
Thanks. This was exactly what I was trying to do. I'm still used to the C ways of for loops, but slowly learning :)
Image Analyst
Image Analyst on 30 Sep 2018
This doesn't work:
img = imread('avocado.png');
subplot(2, 2, 1);
imshow(img);
title('Original Image', 'FontSize', 20);
impixelinfo;
drawnow;
red = img(:, :, 1);
green = img(:, :, 2);
blue = img(:, :, 3);
%create a mask same size as image that indicates 'yellow' pixels
isyellow = red > 200 & red < 250 & green > 200 & green < 250 & blue > 200 & blue < 250;
%set red and blue channel to 0 when image is yellow. set green channel to max
%no idea if that's what asked by the assignment
subplot(2, 2, 2);
imshow(isyellow);
title('Yellow Mask', 'FontSize', 20);
drawnow;
red(isyellow) = 0;
green(isyellow) = 255;
blue(isyellow) = 0;
%recombine all channels
newimg = cat(3, red, green, blue);
subplot(2, 2, 3);
imshow(newimg);
title('Changed Image', 'FontSize', 20);
drawnow;
The isyellow mask is not correct.
Fixed code is below:
img = imread('avocado.png');
subplot(2, 2, 1);
imshow(img);
title('Original Image', 'FontSize', 20);
impixelinfo;
drawnow;
red = img(:, :, 1);
green = img(:, :, 2);
blue = img(:, :, 3);
%create a mask same size as image that indicates 'yellow' pixels
isyellow = red > 173;
% Extract the largest blob only
isyellow = bwareafilt(isyellow, 1);
%set red and blue channel to 0 when image is yellow. set green channel to max
%no idea if that's what asked by the assignment
subplot(2, 2, 2);
imshow(isyellow);
title('Yellow Mask', 'FontSize', 20);
drawnow;
red(isyellow) = 0;
green(isyellow) = 255;
blue(isyellow) = 0;
%recombine all channels
newimg = cat(3, red, green, blue);
subplot(2, 2, 3);
imshow(newimg);
title('Changed Image', 'FontSize', 20);
drawnow;

Sign in to comment.

More Answers (3)

whatchamacallit
whatchamacallit on 27 Sep 2018
Edited: whatchamacallit on 27 Sep 2018
In the mean time this is some of the code I have, and its based on C mixed with MatLab (I know more of C than I do of MatLab). I am trying to select a color range of yellows and then replace those specific pixels with set green pixels. This is the for loop I have so far:
for i = 1: image.columnlength
for j = 1: image.rowlenght
rgb = image(i, j);
red = rgb(:,1); %rgb(1,:);
green = rgb(:,2);%rgb(2,:);
blue = rgb(:,3);%rgb(3,:);
if( red > 200 && red < 250 && green > 200 && green < 250 && blue > 200 && blue < 250
image(i, j ) = green
end
end
end

Image Analyst
Image Analyst on 28 Sep 2018
See attached demos where I change colors. Adapt as needed.

Image Analyst
Image Analyst on 30 Sep 2018

Try this. It will find the yellow part of the avocado, and set it's color to the mean greenish color of the background.

rgbImage = imread('avocado.png');
subplot(2, 2, 1);
imshow(rgbImage);
title('Original Image', 'FontSize', 20);
impixelinfo;
drawnow;
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
%create a mask same size as image that indicates 'yellow' pixels
yellowMask = redChannel > 173;
% Extract the largest blob only
yellowMask = bwareafilt(yellowMask, 1);
%set red and blue channel to 0 when image is yellow. set green channel to max
%no idea if that's what asked by the assignment
subplot(2, 2, 2);
imshow(yellowMask);
title('Yellow Mask', 'FontSize', 20);
drawnow;
% Get the mean green color outside the mask
meanR = mean(redChannel(~yellowMask))
meanG = mean(greenChannel(~yellowMask))
meanB = mean(blueChannel(~yellowMask))
% Set the yellow portion to this particular mean green color.
redChannel(yellowMask) = meanR;
greenChannel(yellowMask) = meanG;
blueChannel(yellowMask) = meanB;
% Recombine all channels
rgbImageChanged = cat(3, redChannel, greenChannel, blueChannel);
subplot(2, 2, 3);
imshow(rgbImageChanged);
title('Changed Image', 'FontSize', 20);
drawnow;

Community Treasure Hunt

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

Start Hunting!