How do I change image background to black?

36 vues (au cours des 30 derniers jours)
Alberto Acri
Alberto Acri le 23 Mar 2022
Modifié(e) : DGM le 23 Mar 2022
Hello! Is there a code that substitutes the image background with a black background?
I brought a picture as an example!

Réponse acceptée

DGM
DGM le 23 Mar 2022
Modifié(e) : DGM le 23 Mar 2022
For a crude binary masking:
inpict = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/938679/image.jpeg');
hsvpict = rgb2hsv(inpict); % convert to hsv
mask = hsvpict(:,:,2) <= 0.25; % look for low-saturation regions
mask = bwareafilt(mask,1); % pick the largest object (the background)
outpict = inpict;
outpict(mask(:,:,[1 1 1])) = 0; % fill background with black
imshow(outpict)
It can be more complicated, but that's a basic approach. If you want to figure out what thresholds to use, you can try to open your image in the Color Thresholder app and explore the options.

Plus de réponses (0)

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by