how to extract images from pure white background

1 回表示 (過去 30 日間)
Fateme Jalali
Fateme Jalali 2016 年 7 月 29 日
回答済み: DGM 2023 年 5 月 27 日
Hello, I want to know how to extract images from white background to process each image individually.This is my image:

回答 (1 件)

DGM
DGM 2023 年 5 月 27 日
Well if it's a pure white background, you can just create a mask that selects everything that's not white and then ...
inpict = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/155240/image.jpeg');
mask = all(inpict(:,:,1) ~= permute([255 255 255],[1 3 2]),3);
imshow(mask,'border','tight')
... oh well I guess it's not actually white. It's been interpolated and compressed, so the edges are ambiguous. You can gamble on using HSV information to separate the two, but it would be easy to find a case that would fail.
inpict = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/155240/image.jpeg');
% generate a mask in HSV
hsvpict = rgb2hsv(inpict);
mask = hsvpict(:,:,2)>0.2 | hsvpict(:,:,3)<0.95;
S = regionprops(mask,'boundingbox'); % get box locations
cropmargin = 4; % amount to trim off of region
numblobs = numel(S);
imstack = cell(numblobs,1); % preallocate
for k = 1:numblobs % crop each image and store in a cell array
thisrect = S(k).BoundingBox + [1 1 -2 -2]*cropmargin;
imstack{k} = imcrop(inpict,thisrect);
end
% show the results
imshow(imstack{1},'border','tight')
imshow(imstack{2},'border','tight')
In this case, I opted to simply crop off the periphery of each image, since the boundary is a soft transition filled with artifacts. It's easy to assume that there's no point in keeping it.

Community Treasure Hunt

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

Start Hunting!

Translated by