Using ColorMap to Change Yellow Object in Image to Green
Show older comments
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
More Answers (3)
whatchamacallit
on 27 Sep 2018
Edited: whatchamacallit
on 27 Sep 2018
1 Comment
Guillaume
on 27 Sep 2018
This would have been better posted as a comment to the question.
Image Analyst
on 28 Sep 2018
0 votes
See attached demos where I change colors. Adapt as needed.

Image Analyst
on 30 Sep 2018
0 votes
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;

Categories
Find more on Green 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!
