I am trying to crop an object/product from a relatively solid background but have been struggling to get a perfect implementation or at least something that works most of the time.
My previous questions were asked on Stack Overflow and I've been able to get decent results and cropping a rectangle works almost perfectly:
My current approach is to find a mask of the image and then change every unwanted pixel to white, but this hasn't given me consistent results:
original = imread('1.jpg');level = graythresh(original);img = rgb2gray(original);mask = im2bw(img,level+0.1);se = strel('line',10,90);mask = imdilate(~mask,se);mask = imfill(mask,'holes');bw = activecontour(img,mask);rows = numel(original(:,1,1));columns = numel(original(1,:,1));for i = 1:rows for j = 1:columns if ( bw(i,j,1) == 0 ) original(i,j,:) = 255; end endendimshow(original);
A preview image:
Best Answer