MATLAB: Using ColorMap to Change Yellow Object in Image to Green

colorcolormapdigital image proc...digital image processinghomeworkimageImage Acquisition Toolboximage analysisimage processingImage Processing ToolboxMATLAB

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.

Best Answer

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);