How to remove red from the image

12 views (last 30 days)
Divya Kumar
Divya Kumar on 20 Mar 2024
Commented: Divya Kumar on 21 Mar 2024
Hello,
I want to remove the red spots in the image not from the background. Please see the attached image. Please help!
  1 Comment
DGM
DGM on 21 Mar 2024
What exactly do you mean "remove the red spots in the image not from the background"?
Specifically, what do you mean "remove"? What should the red areas be replaced by?
By "in the image not from the background" do you mean to remove only disconnected red regions?
If you created this image, then you should be working with the mask that was used to create the red parts of the image to begin with. We shouldn't be wasting our time with a damaged JPG screenshot of random size. JPG should never be part of the image processing routine, and neither should screenshots.

Sign in to comment.

Accepted Answer

DGM
DGM on 21 Mar 2024
If you created the original image, then you shouldn't even be trying to do this. Processing damaged mis-sized screenshots is only adding pointless complexity to the workflow and it adds heaps of error into any sort of quantitative analysis. Save images using imwrite(). Don't save screenshots. If you don't understand the problems with using JPG, then don't use it.
Here's a thing. I still think it's a bad idea to even use this image, but if there's no other option, well at least this is more useful than an AI-generated list of bullet points.
% save the actual image
% not a screenshot of the image
% don't save as JPG
inpict = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1647221/image.jpeg');
% crop off all the padding left from taking a screenshot
inpict = imcrop(inpict,[348.51 35.51 980.98 819.98]);
% get a mask
[H S V] = rgb2hsv(inpict);
mk = S>0.5; % pick some threshold
mk = imclearborder(mk); % get rid of the part connected to the border
% do something with the mask?
fillcolor = [0 1 0]; % pick some fill color
outpict = permute(fillcolor,[1 3 2]).*mk + im2double(inpict).*(1-mk);
% show whatever is left
imshow(outpict,'border','tight')
Red regions which are not connected to the background are filled with green for sake of demonstration. The mask coverage will be guaranteed to be inaccurate due to the JPG compression. Some red regions will be overselected. Some will be underselected.
If that's not what you wanted, you'll have to be clear about what you want.
  1 Comment
Divya Kumar
Divya Kumar on 21 Mar 2024
Thank you! Your code helped me to get what I wanted.

Sign in to comment.

More Answers (1)

Pratyush
Pratyush on 21 Mar 2024
Hi Divya,
To remove red spots from an image in MATLAB without affecting a red background, follow these steps:
  • Load your image with "imread" and display it using "imshow".
  • Convert the image from RGB to HSV color space using "rgb2hsv" for easier color segmentation.
  • Define thresholds for hue, saturation, and value to isolate red areas. This step requires experimentation to differentiate spots from the background. Create a binary mask based on these thresholds.
  • Use morphological operations like "bwareaopen" to remove noise and small objects from the mask. Further refine the mask as needed to accurately capture the red spots.
  • Use the refined mask to alter or remove the red spots from the original image. This could involve setting the spots to a different color or blending them with their surroundings.
Adjust the parameters (e.g., thresholds, size for "bwareaopen") based on your specific image to accurately target and remove the red spots without affecting the red background.

Community Treasure Hunt

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

Start Hunting!