Finding area from the bounding box created by vision.blobAnalysis

Greetings fellow programmers,
As stated in the title, is there a possible way to extract information from the boundingbox created by the function vision.blobanalysis?
blob = vision.BlobAnalysis('BoundingBoxOutputPort', true,'MinimumBlobAreaSource', 'Property','MinimumBlobArea', 200);
The reason for this is to find the area from the [x y width height] boundary box and then multiply the 3rd and 4th parameter (width x height) to obtain the area of the bounding box.
These are similar methods but used in the image processing instead,
bb = stats(K).BoundingBox;
barea = bb(3) * bb(4);
fprintf('Area #%d x = %d y = %d, K, bb(1), bb(2)...);
Is there a way to do the same thing like this in computer vision toolbox using the blob Analysis? Thank you very much.
Regards, Akira

 Accepted Answer

Hi Akira,
Yes, of course.
blob = vision.BlobAnalysis('BoundingBoxOutputPort', true,'MinimumBlobAreaSource', 'Property','MinimumBlobArea', 200);
Creates an object called blob. To get the connected components from an image, you should now use the step() method:
[areas, centroids, boundingBoxes] = step(blob, I);
where I is a binary image. areas will contain the areas of the connected components. You can control what outputs you get using the object's properties. AreaOutputPort, CentroidOutputPort, and BoundingBoxOutputPort are true by default.

4 Comments

Hi Dima, Thank you for your fast reply, I have followed your advice but I have encountered a problem. I wanted area, boundingbox and centroids from my blob analysis so I wrote this
blob = vision.BlobAnalysis('CentroidOutputPort', true, 'AreaOutputPort',...
true,'BoundingBoxOutputPort', true,'MinimumBlobAreaSource', 'Property',...
'MinimumBlobArea', 15000);
I don't know If I get this right or not because when I tried to apply that using step() like this:
[areas, centroids, boundingBoxes] = step(blob, Final);
My "Final" indicate the pre-processed binary object. The problem here is that I don't know how to type in such a way that it can playback the video for me to see the result. I tried this,
while start
frame = step(videoReader);
[areas, centroids, boundingBoxes] = step(blob, Final);
result = step(Final, frame, [areas, centroids, boundingBoxes]);
step(videoPlayer, result);
end
release(videoPlayer);
release(videoReader);
However, I get this error instead, it said I used step() function the wrong way:
Error using tf (line 279)
The values of the "num" and "den" properties must be row
vectors or cell arrays of row vectors, where each vector
is nonempty and containing numeric data. Type "help
tf.num" or "help tf.den" for more information.
Error in step (line 109)
sys = tf(a,b);
Error in ComputerVision2 (line 74)
result = step(Final, frame, [areas, centroids,
boundingBoxes]);
Sorry for my inexperience but, I have no idea what is the problem. Please help, thank you.
You have the area, but what do you want to do with it? For example do you want the area displayed as a text string, with the label going at the centroid of the blob?
The error you are seeing occurs because you are applying step() to a numeric array ("Final") instead of to a Computer Vision object (such as "blob"). That results in you getting the step() response plot routine from the Control Systems Toolbox.
Hi Akira,
Just to clarify: vision.BlobAnalysis is a class, like in Java or C++. step() is a method of this class. The same method exists in many other classes, and it always means "do your thing". In this particular case, "do your thing" means do blob analysis on a binary image, and return the properties of the blobs.
This is why you cannot say step(Final, ...). Final is a numeric array, not a MATLAB class, so there is no step method for it.
If you want to display the blob information on the image, you can use the following functions: insertObjectAnnotation, insertShape, insertText. Please see their documentation for details.
Thank you Mr Walter and Mr Dima for clarifying! I will get to the code when I have the time. So in short, in order for me to post or to obtain values from the vision.BlobAnalysis, I will have to use insertShape, insertText, got it. Alright, just to clarify I needed the area because I want to put it into a if function. If Area > 500 then counter = counter +1, something like that. If possible, is there any way to contact the both of you if I have more questions? Thanks.
Regards, Akira

Sign in to comment.

More Answers (0)

Categories

Asked:

on 14 Jul 2015

Commented:

on 15 Jul 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!