Why isn't matlab drawing the bounding box on my picture?
4 views (last 30 days)
I followed these commands in tutorial exactly how he did it to draw a bounding box around the main object of a pic but nothing happened for me. The picture looks the same. Will these commands work or not depending on the picture you are using?
>> walk = imread('walkstraight/frame0062.tif');
>> gray_walk = rgb2gray(walk)
>> bw_walk = im2bw(gray_walk, 0.8);
>> bw = ~bw_walk;
>> bw = bwareaopen(bw, 1000);
>> [bwLabel, num] = bwlabel(bw, 8);
>> s = regionprops(bwLabel, 'BoundingBox');
>> hold on;
>> rectangle('Position', s.BoundingBox)
>> hold off;
TADA on 5 Sep 2021
Without your image its hard to tell, but I tried your code on a couple matlab builtin images and it seems to work.
if you get an error, you may have more than one bounding box returned from regionprops, then you can use a loop to plot all rectangles
for i = 1:numel(s)
if you don't get an error, but just can't see the rectangle, it may be actually drawn around the entire image. It might happen if the contrast isn't high enough.
in that case, you can try lowering the cutoff intensity you use for im2bw.
Image Analyst on 5 Sep 2021
Is the main object the largest one? If so, instead of
bw = bwareaopen(bw, 1000);
bw = bwareafilt(bw, 1); % Take largest blob only.
Attach your image if you reply to either one of us. And tell us where to find the tutorial you're following.