MATLAB Answers


change color in image

Asked by Elysi Cochin on 18 Feb 2013

please refer the image below.....

is it possible to remove those boxes from that image.....

and also, is it possible to change the color of those boxes......

for example i want red box to appear yellow....

and the blue circle to appear red.... is it possible....

please do reply


No products are associated with this question.

1 Answer

Answer by Image Analyst
on 18 Feb 2013
 Accepted answer

Why don't you have access to the original image? If you don't, for some strange reason, then you can find the pixels by first extracting the individual color planes, then median filtering them and replacing the colored pixels with the median filtered pixels. It will work a lot better if you have the original uncompressed image, rather than the blurry one you uploaded, which does not have sharp graphics.

rgbImage = imread(fullFileName);
% Get the dimensions of the image.  numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 2, 1);
axis on;
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Then do a median filter on each one.  
medianRed = medfilt2(redChannel, [7 7]);
medianGreen = medfilt2(greenChannel, [7 7]);
medianBlue = medfilt2(blueChannel, [7 7]);
% Display the image.
subplot(2, 2, 2);
title('Red Median Image', 'FontSize', fontSize);
% Then find the pure color pixels.
thresholdValue = 40;
pureRed = redChannel >= thresholdValue & greenChannel < thresholdValue & blueChannel < thresholdValue;
pureGreen = redChannel < thresholdValue & greenChannel >= thresholdValue & blueChannel < thresholdValue;
pureBlue = redChannel < thresholdValue & greenChannel < thresholdValue & blueChannel >= thresholdValue;
% Display the image.
subplot(2, 2, 3);
title('Red Mask Image', 'FontSize', fontSize);
% Then replace each pure color with the median from that channel.
redChannel(pureRed) = medianRed(pureRed);
greenChannel(pureGreen ) = medianGreen(pureGreen );
blueChannel (pureBlue ) = medianBlue(pureBlue );
% Construct the new color image
rgbImageFixed = cat(3, redChannel, greenChannel, blueChannel);
% Display the image.
subplot(2, 2, 4);
title('Fixed RGB Image', 'FontSize', fontSize);


Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!