Using ColorMap to Change Yellow Object in Image to Green
12 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
whatchamacallit
el 27 de Sept. de 2018
Respondida: Image Analyst
el 30 de Sept. de 2018
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.
0 comentarios
Respuesta aceptada
Guillaume
el 27 de Sept. de 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 comentarios
Image Analyst
el 30 de Sept. de 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;
Más respuestas (3)
Image Analyst
el 28 de Sept. de 2018
See attached demos where I change colors. Adapt as needed.
0 comentarios
Image Analyst
el 30 de Sept. de 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;
0 comentarios
Ver también
Categorías
Más información sobre Green en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!