how to extract images from pure white background
2 views (last 30 days)
Show older comments
Hello, I want to know how to extract images from white background to process each image individually.This is my image:
0 Comments
Answers (1)
DGM
on 27 May 2023
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.
0 Comments
See Also
Categories
Find more on Image Segmentation and Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!