How can I convert the red color in the image to white?
Show older comments

I need to remove the noise (red color) from this image. I need help in performing two operations: 1. Convert the red color in the image to white. 2. Considering the image is array of pixels in 2D. What I need to do is ask the program to check which pixel values are white, and then give it a value of a non-white adjacent pixel.
Can this be done in MATLAB?
This is how the final product should look like

Answers (1)
Image Analyst
on 22 Feb 2018
Edited: Image Analyst
on 23 Feb 2018
Try this:
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
redMask = redChannel >= 200 & greenChannel <= 50 & blueChannel <= 50;
greenChannel(redMask) = 255;
blueChannel(redMask) = 255;
% Recombine separate color channels into a single, true color RGB image.
rgbImage = cat(3, redChannel, greenChannel, blueChannel);
8 Comments
Image Analyst
on 23 Feb 2018
Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = 'NEWREF01.jpg';
folder = pwd
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
%=======================================================================================
% Read in demo image.
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Display image.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis on;
caption = sprintf('Original Color Image\n%s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
redMask = redChannel >= 200 & greenChannel <= 50 & blueChannel <= 50;
redChannel(redMask) = 255;
greenChannel(redMask) = 255;
blueChannel(redMask) = 255;
% Display the image.
subplot(2, 2, 2);
imshow(redMask);
title('Red Mask', 'FontSize', fontSize, 'Interpreter', 'None');
% Recombine separate color channels into a single, true color RGB image.
rgbImage = cat(3, redChannel, greenChannel, blueChannel);
% Display the image.
subplot(2, 2, 3);
imshow(rgbImage);
title('Red Mask Now White', 'FontSize', fontSize, 'Interpreter', 'None');

Usman Mahmood
on 23 Feb 2018
Usman Mahmood
on 23 Feb 2018
Edited: Usman Mahmood
on 23 Feb 2018
Usman Mahmood
on 23 Feb 2018
Image Analyst
on 24 Feb 2018
So either change the thresholds (probably best) or dilate the red mask
redMask = imdilate(redMask, ones(11));
But since the red seems like it's computer graphics, not actually part of the original scene, don't you KNOW where the red is, I mean since you probably actually drew it in the first place???
Usman Mahmood
on 24 Feb 2018
Usman Mahmood
on 24 Feb 2018
Image Analyst
on 24 Feb 2018
OK good luck. When you paint over it, it would be good if you used pure red (255,0,0) instead of whatever similar color you used, or at least know exactly what color you used so you can put those values in to the algorithm.
Next, never use JPG images for image analysis because this will change the values. That's probably why there was still a little bit of red around the perimeter. The jpeg process blurred out the image.
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!