How do I automatically remove white spots in an image?
Afficher commentaires plus anciens
I'm trying to write some code to automatically remove white spots from the image below:

I've figured out a way to manually do it (see code at bottom of post, Approach 1) where the user has to draw a rectangle around each spot, but this is slow & non-ideal. The right image below has one spot removed from left of center.

Ideally, I would like to use some sort of thresholding to identify the locations of the bright spots (see code at bottom of post, Approach 2), and then use region fill to smooth out those spots. My current application of this (see below) doesn't produce a smooth result. It seems to only darken the spots a little bit. Maybe I need to draw rectangles around the location of each bright spot? Any help is greatly appreciated.

The code:
clc, clear all, close all
% load the image:
folder = pwd;
fileName = uigetfile('*.png','Multiselect','off');
fullName = fullfile(folder,fileName);
imgCropped = imread(fullName);
figure; imshow(imgCropped,'InitialMagnification',300);
% Approach 1 - Hand-picked:
figure
imshow(imgCropped,'InitialMagnification',300);
title('Select a spot:','FontSize',fontSize);
spotROI = drawrectangle('Color',[1 1 0]);
rectPosition = spotROI.Position;
r_w = rectPosition(3);
r_h = rectPosition(4);
x = ones(1,4);
y = ones(1,4);
x(1) = rectPosition(1);
x(2) = rectPosition(1)+r_w;
x(3) = rectPosition(1)+r_w;
x(4) = rectPosition(1);
y(1) = rectPosition(2);
y(2) = rectPosition(2);
y(3) = rectPosition(2)+r_h;
y(4) = rectPosition(2)+r_h;
L = regionfill(imgCropped,x,y);
figure; imshowpair(imgCropped,L,'montage');
title('Original vs. Hand-picked ''Region-fill''')
% Approach 2 - Automated:
figure
level = 0.8;
BW = imbinarize(imgCropped,level);
mask = bwareaopen(BW, 40);
J = regionfill(imgCropped,mask); % use BW as a mask
figure; imshowpair(imgCropped,J,'montage');
title('Original vs. ''Region-filled'' Image')
Réponse acceptée
Plus de réponses (1)
clc; clear all; close all;
im = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/786560/image.png');
figure; imshow(im);
bw = imbinarize(im,'adaptive','ForegroundPolarity','dark','Sensitivity',0.7);
jm = regionfill(im, bw);;
figure; imshow(bw);
figure; imshow(jm);
Catégories
En savoir plus sur Blocked Images dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



