To remove a specific color (e.g. red) from an RGB image in MATLAB, you can use the following steps:
Read the image using the imread() function.
Convert the RGB image to the HSV color space using the rgb2hsv() function.
Extract the hue, saturation, and value channels using the rgb2hsv() function.
Create a binary mask of the pixels that have the hue value within a certain range corresponding to the color you want to remove. For example, if you want to remove red, you can set the hue range to [0, 0.1] and [0.9, 1]. You can use the inRange() function from the Computer Vision Toolbox to create the binary mask.
Use the binary mask to set the pixels in the saturation and value channels to 0 (black).
Convert the modified HSV image back to RGB using the hsv2rgb() function.
Display the original and modified images using the subplot() and imshow() functions.
Here's an example code snippet that removes red from an image:
main.m
% Read the imageimg = imread('image.jpg');
% Convert the RGB image to the HSV color spacehsvImg = rgb2hsv(img);
% Extract the hue, saturation, and value channelshue = hsvImg(:,:,1);
saturation = hsvImg(:,:,2);
value = hsvImg(:,:,3);
% Create a binary mask of the pixels with red hue valuesredMask = inRange(hue, [0, 0.1]) | inRange(hue, [0.9, 1]);
% Set the saturation and value channels of masked pixels to 0saturation(redMask) = 0;
value(redMask) = 0;
% Combine the modified channels back into an HSV imagemodifiedHsvImg = cat(3, hue, saturation, value);
% Convert the modified HSV image to RGBmodifiedRgbImg = hsv2rgb(modifiedHsvImg);
% Display the original and modified images side by sidesubplot(1, 2, 1), imshow(img), title('Original image')
subplot(1, 2, 2), imshow(modifiedRgbImg), title('Modified image (red removed)')