Draw a rectangular bounding box around the person in the image
I want to create a bounding box around the person in the image. I've tried different methods, but I couldn't get the solution I want.
Here's the image I'm using:
Here's the code I've written so far:
bw = im2bw(test, graythresh(test)); bw2 = imfill(bw,'holes'); imshow(bw2); figure; L = bwlabel(bw2); imshow(label2rgb(L, @jet, [.7 .7 .7])) figure; imshow(I1); R = regionprops(L, 'BoundingBox'); rectangle('Position', R(1).BoundingBox);
source to share
The problem isn't really drawing the bounding box - it is placing the person within the image, which you didn't quite do right. If you don't do it right, you won't be able to place the correct bounding box around the person. This is what I did to find the person in the image and then draw a bounding box around that person. Your image is supposed to be stored in im
:
- Note that the person's intensity distribution is darker than most of the scene. So I'm going to create a threshold image by choosing any pixels less than intensity 65 to be white and the rest to be black.
- I clear any white pixels that surround the border of the image.
- I am making a call
regionprops
retrieving propertiesBoundingBox
andArea
. - I go through all areas and find
BoundingBox
with the largestArea
. - I use this one
BoundingBox
and draw it on our image.
Thus:
%// Step #1
im_thresh = im < 65;
%// Step #2
im_thresh2 = imclearborder(im_thresh);
%// Step #3
rp = regionprops(im_thresh2, 'BoundingBox', 'Area');
%// Step #4
area = [rp.Area].';
[~,ind] = max(area);
bb = rp(ind).BoundingBox;
%// Step #5
imshow(im);
rectangle('Position', bb, 'EdgeColor', 'red');
This is what we get:
Keep in mind that this is not ideal. You may have to play with the threshold to get a more accurate bounding box, but that should be enough to get you started.
Good luck!
source to share
You can also use vision.PeopleDetector
in the video surveillance system toolbar:
im = imread('bnJzI.png'); detector = vision.PeopleDetector('ClassificationModel', 'UprightPeople_96x48', 'ClassificationThreshold', 2.5); bbox = step(detector, im); im2 = insertObjectAnnotation(im, 'rectangle', bbox, 'person'); imshow(im2);
source to share