How to identify and crop a rectangle inside an image in MATLAB -
i have image has rectangle drawn in it. rectangle can of design background isn't single color. it's photo taken phone camera one. want crop inner picture (scenary) in image.
how can in matlab?
i tried code
img = im2double(imread('https://i.stack.imgur.com/is2ht.jpg')); bw = im2bw(img); dim = size(bw) col = round(dim(2)/2)-90; row = min(find(bw(:,col))) boundary = bwtraceboundary(bw,[row, col],'n'); r = [min(boundary) , max(boundary)]; img_cropped = img(r(1) : r(3) , r(2) : r(4) , :); imshow(img_cropped);
and not 1 above or one
i need find code works image specific rectangle design.any aprreciated.thank you
the processing below considers following:
- backgroung monochrome, possible gradient
- a border monochrome(gradient), distinguishable background, not barocco/rococco style
- a picture kind of real world picture lots of details, not malevich's black square
so first search picture , flatten background entropy filtering
img=imread('http://i.stack.imgur.com/kmbrg.jpg'); %dimensions neighbourhood guess cross_nhood = false(11,11); cross_nhood(:,6)=1;cross_nhood(6,:)=1; img_ent = entropyfilt(img./255,repmat(cross_nhood,[1 1 3])); img_ent_gray = rgb2gray(img_ent);
then find corners using harris detector , choose 4 point: 2 leftmost , 2 rightmost, , crop image removing background (with precision inclination). use r2011a, may have bit different functions, refer matlab help
harris_pts = corner(img_ent_gray); corn_pts = sortrows(harris_pts,1); corn_pts = [corn_pts(1:2,:);... corn_pts(size(corn_pts,1)-1:size(corn_pts,1),:)]; crop_img=img(min(corn_pts(:,2)):max(corn_pts(:,2)),... min(corn_pts(:,1)):max(corn_pts(:,1)),:); corn_pts(:,1)=corn_pts(:,1) - min(corn_pts(:,1)); corn_pts(:,2)=corn_pts(:,2) - min(corn_pts(:,2)); corn_pts = corn_pts + 1;
an here problem: lines between corner points inclined @ bit different angle. can both problem of corner detection , image capture (objective distortion and/or bit wrong acquisition angle). there no straightforward, right solution. i'd better choose biggest inclination (it crop picture little) , start processing image line line (to split image use bresenham algorithm) till or most, choose, pixels belongs picture, not inner border. distinguishable feature can local entropy, std of colour values, specific colour threshold, different methods of statistics, whatever.
another approach colour segmentation, gram-shmidt orthogonalization or a*-b* color segmentation. however, you'll same problems if image skewed , part of picture matches colour of border (see last picture, bottom left corner).
Comments
Post a Comment