c++ - Crop image by detecting a specific large object or blob in image? -
please help me resolve issue. working on image processing based project , stuck @ point. got image after processing , farther processing need crop or observe deer , remove other portion of image.
this initial image:
and result should this:
it more improve if single biggest blob in image , save image.
it looks deer in image pretty much connected , closed. can utilize regionprops
find of bounding boxes in image. 1 time this, can find bounding box gives largest area, presumably deer. 1 time find bounding box, can crop image , focus on deer entirely. such, assuming image stored in im
, this:
im = im2bw(im); %// in case... bound = regionprops(im, 'boundingbox', 'area'); %// obtaining bounding box co-ordinates bboxes = reshape([bound.boundingbox], 4, []).'; %// obtain areas within each bounding box areas = [bound.area].'; %// figure out bounding box has maximum area [~,maxind] = max(areas); %// obtain bounding box %// ensure floating point removed finalbb = floor(bboxes(maxind,:)); %// crop image out = im(finalbb(2):finalbb(2)+finalbb(4), finalbb(1):finalbb(1)+finalbb(3)); %// show images figure; subplot(1,2,1); imshow(im); subplot(1,2,2); imshow(out);
let's go through code slowly. first convert image binary in case. image may rgb image intensities of 0 or 255... can't sure, let's binary conversion in case. phone call regionprops
boundingbox
property find every bounding box of every unique object in image. bounding box minimum spanning bounding box ensure object contained within it. each bounding box 4 element array structured so:
[x y w h]
each bounding box delineated origin @ top left corner of box, denoted x
, y
, x
horizontal co-ordinate while y
vertical co-ordinate. x
increases positively left right, while y
increases positively top bottom. w,h
width , height of bounding box. because these points in structure, extract them , place them single 1d vector, reshape becomes m x 4
matrix. bear in mind way know of can extract values in arrays each structuring element efficiently without for
loops. facilitate our searching quicker. have done same area
property. each bounding box have in our image, have attribute of total area encapsulated within bounding box.
thanks @shai spot, can't utilize bounding box co-ordinates determine whether or not has biggest area within have lean diagonal line drive bounding box co-ordinates higher. such, need rely on total area object takes within bounding box well. put, it's sum of of pixels contained within object.
therefore, search entire area vector have created see has maximum area. corresponds deer. 1 time find location, extract bounding box locations, utilize crop image. bear in mind bounding box values may have floating point numbers. image co-ordinates in integer based, need remove these floating point values before decide crop. decided utilize floor
. write code displays original image, cropped result.
bear in mind only work if there 1 object in image. if want find multiple objects, check bwboundaries
in matlab. otherwise, believe should started.
just completeness, next result:
c++ matlab opencv image-processing
No comments:
Post a Comment