Blob Detection in LIVE Video
Blob Detection in LIVE Video

Hello again. In this project I have illustrated how you can detect a blob i.e. the connected component in a LIVE video and count the total number of it. Here also I used the Computer Vision Toolbox and Image Acquisition Toolbox which is included in my MATLAB version R2012a. So you are gonna need these, although live video can be acquired using other hand on MATLAB commands.

In this sample code I used very simple method for detecting the blobs, first convert the whole image into its binary equivalent and eliminate all the unwanted things by using suitable thresholds and filters. Then mark all the remaining areas and count the number. Simple isn’t it?

Here I am giving the whole code. All the code lines are self explanatory. So I didn’t thought of including any comment after each of the line. Now if you still have any further problem of understating the code and need any kind of help, do not hesitate to write me. Will be uploading more MATLAB codes. All the best.


% Program Name : Blob Detection in LIVE Video
% Author : Arindam Bose
% Version : 1.0
% Description : This program detects the blob in LIVE Video
% and put a box around it and count the total number
% of blobs in one frame.
% Copyright : © Arindam Bose, All rights reserved.
% License : Open Source, Freeware
vidDevice = imaq.VideoDevice('winvideo', 1, 'YUY2_640x480', ...
'ROI', [1 1 640 480], ...
'ReturnedColorSpace', 'rgb');
hMean1 = vision.Mean;
hMean2 = vision.Mean('RunningMean', true);
hMedianFilt = vision.MedianFilter;
hHisteq = vision.HistogramEqualizer('Histogram', 'Uniform', 'BinCount', 2);
hblob = vision.BlobAnalysis(...
'CentroidOutputPort', false, 'AreaOutputPort', true, ...
'BoundingBoxOutputPort', true, 'OutputDataType', 'double', ...
'MinimumBlobArea', 250, 'MaximumBlobArea', 3600, 'MaximumCount', 80);
hshapeins1 = vision.ShapeInserter('BorderColor', 'Custom', ...
'CustomBorderColor', [0 1 0]);
htextins = vision.TextInserter('Text', '%4d', 'Location', [1 1], ...
'Color', [1 1 1], 'FontSize', 12);
hVideo1 = vision.VideoPlayer('Name','Thresholded Video','Position',[10 100 670 510]);
hVideo2 = vision.VideoPlayer('Name','Result Video','Position',[690 100 670 510]);
lineRow = 22;
nFrames = 0;
while (nFrames < 200)
frame = step(vidDevice);
grayFrame = rgb2gray(frame);
segmentedObjects = logical(step(hHisteq, grayFrame));
step(hVideo1, segmentedObjects);
[area, bbox] = step(hblob, segmentedObjects);
Idx = bbox(:,1) > lineRow;
ratio = zeros(length(Idx), 1);
ratio(Idx) = single(area(Idx,1))./single(bbox(Idx,3).*bbox(Idx,4));
count = int32(length(ratio));
y2 = step(hshapeins1, frame, bbox);
y2(lineRow:lineRow+1,:,:) = 1;
y2(1:15,1:30,:) = 0;
result = step(htextins, y2, count);
step(hVideo2, result);
nFrames = nFrames + 1;

The code has to optimized more.

Hope this will help. Thanks… More Programs are coming soon…

About the author

Scientific History Blog Writer • Art enthusiast and Illustrator • Amateur Photographer • Biker and Hiker • Beer Enthusiast • Electrical Engineer • Chicago

Leave a Reply

Twitter Feed
%d bloggers like this: