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

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)

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

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');
Perfect, the first part of the problem has been addressed but I can still see some red color on the border. Let me tell you what I am hoping to do.I am trying to find the diameter of the flowable liquid underneath. Now I have a 1000 such images so I cannot use imtools to do that. Now the white part in the image is my noise, I need to remove that. I am hoping to have my final image to be somewhat like :-
I am not sure if I've explained it to you correctly, but here's what my final image should look like:
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???
yes the red is drawn manually using paintbrush just to highlight the actual noise, here's the actual image, you might wanna take a look.
I intend to repaint my apparatus to red or any other color, so I can try separating the noise later.
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.

Sign in to comment.

Categories

Products

Asked:

on 22 Feb 2018

Commented:

on 24 Feb 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!