Remove background from image

17 ビュー (過去 30 日間)
Valnyr Lira
Valnyr Lira 2019 年 5 月 3 日
回答済み: DGM 2023 年 4 月 30 日
Hi, i'd like to remove the background from RGB images. Since this have to be done in a lot of images i'd like to automate this task.
Original image with background:
mx01.jpg
The image should be transformed as this one below, without background.
m001.jpg
I have cropped the images as the first one, however, i'm getting in trouble to remove the background.

回答 (1 件)

DGM
DGM 2023 年 4 月 30 日
% read the image
inpict = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/217934/mx01.jpeg');
% you could use color-based thresholding
% but with the low contrast and chroma, JPG damage is an obstacle
% in this case, there's plenty of object contrast in luma alone
Ymask = rgb2gray(inpict)<128;
Ymask = ~imfill(Ymask,'holes');
% adjust white level
inlevels = stretchlim(inpict).*[0; 1];
outpict = imadjust(inpict,inlevels);
% apply mask
outpict(repmat(Ymask,[1 1 3])) = 255;
imshow(outpict)

Community Treasure Hunt

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

Start Hunting!

Translated by