Reputation: 45
I am using this code in the below.
function videoAnalysis()
foregroundDetector = vision.ForegroundDetector('NumGaussians', 3, 'NumTrainingFrames', 50);
[filename, pathname] = uigetfile( ...
{'*.*', 'All Files (*.*)'}, ...
'Select a video file');
videoReader = vision.VideoFileReader(fullfile(pathname,filename));
% fRate = videoReader.info.VideoFrameRate;
% disp(fRate);
for i = 1:150
frame = step(videoReader);
foreground = step(foregroundDetector, frame);
end
se = strel('square', 3);
filteredForeground = imopen(foreground, se);
blobAnalysis = vision.BlobAnalysis('BoundingBoxOutputPort', true, ...
'AreaOutputPort', false, 'CentroidOutputPort', false, ...
'MinimumBlobArea', 350 , 'MaximumBlobArea', 15000, ...
'MajorAxisLengthOutputPort' , true, 'MaximumCount', 5);
bbox = step(blobAnalysis, filteredForeground);
result = insertShape(frame, 'Rectangle', bbox, 'Color', 'green');
numCars = size(bbox, 1);
result = insertText(result, [10 10], numCars, 'BoxOpacity', 1, 'FontSize', 14);
videoPlayer = vision.VideoPlayer('Name', 'Detected Cars');
videoPlayer.Position(3:4) = [650,400]; % window size: [width, height]
% se = strel('square', 3); % morphological filter for noise removal
i = 0;
while ~isDone(videoReader)
% if ( mode(i,10) == 0 )
% disp(i);
% end
% i = i + 1;
frame = step(videoReader); % read the next video frame
% Detect the foreground in the current video frame
foreground = step(foregroundDetector, frame);
% Use morphological opening to remove noise in the foreground
filteredForeground = imopen(foreground, se);
% Detect the connected components with the specified minimum area, and
% compute their bounding boxes
bbox = step(blobAnalysis, filteredForeground);
% Draw bounding boxes around the detected cars
result = insertShape(frame, 'Rectangle', bbox, 'Color', 'blue');
% imshow(result);
%
% Display the number of cars found in the video frame
numCars = size(bbox, 1);
result = insertText(result, [10 10], numCars, 'BoxOpacity', 1, 'FontSize', 14);
%if there is moving object
if numCars == 0
result = insertText(result, [100 20], 'You can pass now', 'BoxOpacity', 1, 'FontSize', 14, 'BoxColor', 'green');
else
result = insertText(result, [100 20], 'Please wait', 'BoxOpacity', 1, 'FontSize', 14, 'BoxColor', 'red');
end
step(videoPlayer, result); % display
end
release(videoReader); % close
end
Basically code looks for a video and detects the moving object between video frames and draw bounding boxes around changed pixels. I need these bounding boxes' center and area informations. To do that I assume I have to change AreaOutputPort and CentroidOutputPort parameters in blobAnalysis to TRUE. But if I do that Matlab gives error: The POSITION matrix must have four columns for shape Rectangle. How can I obtain these values?
Thank you.
Upvotes: 1
Views: 2771
Reputation: 45
I have found a new way to calculate area and centroid:
for i=1:size(bbox, 1)
centroid(i,:) = [ bbox(i,1)+bbox(i,3)/2 ; bbox(i,2)+bbox(i,4)/2 ];
area(i,1) = bbox(i,3)*bbox(i,4);
end
Upvotes: 0
Reputation: 39389
If you set AreaOutputPort
and CentroidOutputPort
to true
, you will get three outputs instead of one.
Instead of
bbox = step(blobAnalysis, filteredForeground);
use
[areas, centroids, bbox] = step(blobAnalysis, filteredForeground);
The way you currently have it bbox
ends up being a 1-D array containing the areas, which is why insertShape
throws the error.
Upvotes: 1