can someone tell me why cant i use any other video as an example in this code : https://in.mathworks.com/help/vision/examples/detecting-cars-using-gaussian-mixture-models.html
Show older comments
foregroundDetector = vision.ForegroundDetector('NumGaussians', 3, ...
'NumTrainingFrames',80 );
videoReader = vision.VideoFileReader('visiontraffic.avi');
for i = 1:150
frame = step(videoReader); % read the next video frame
foreground = step(foregroundDetector, frame);
end
figure; imshow(frame); title('Video Frame');
figure; imshow(foreground); title('Foreground');
se = strel('square', 3);
filteredForeground = imopen(foreground, se);
figure; imshow(filteredForeground); title('Clean Foreground');
blobAnalysis = vision.BlobAnalysis('BoundingBoxOutputPort', true, ...
'AreaOutputPort', false, 'CentroidOutputPort', false, ...
'MinimumBlobArea', 150);
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);
figure; imshow(result); title('Detected Cars');
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
while ~isDone(videoReader)
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', 'green');
% Display the number of cars found in the video frame
numCars = size(bbox, 1);
result = insertText(result, [10 10], numCars, 'BoxOpacity', 1, ...
'FontSize', 14);
step(videoPlayer, result); % display the results
end
release(videoReader); % close the video file
Answers (1)
Steven Lord
on 26 Nov 2018
0 votes
You didn't say what happens when you try using a different video in the example.
- Do you have Computer Vision System Toolbox installed? That example uses many functions from that toolbox, so if you don't have it installed you won't be able to run the example. Check the ver function output to determine if you have it installed.
- Which release of MATLAB are you using? The online documentation is for the most recent release (currently release R2018b.) I'm not sure how much (if any) that example has changed in recent releases, but if you're using a sufficiently old release you may need to use the version of that example included in the documentation for the release you're using. If you're using a really old version (one that predates the introduction of some of the functions/objects used in the example) that example may not exist or may be significantly different.
- Do you receive an error? If so, what is the full text of the error message (everything displayed in red text) that you receive when you try running the code?
- Do you receive a different result than you expected? If so, what result do you receive and what result did you expect to receive? Be specific about the differences between the actual results and your expected results.
- Did something else happen? if so, describe in detail what happened.
3 Comments
Y.Rahul Kumar
on 26 Nov 2018
Steven Lord
on 26 Nov 2018
What exactly happens when you change this line of the example to use a different video file?
videoReader = vision.VideoFileReader('visiontraffic.avi');
Y.Rahul Kumar
on 26 Nov 2018
Categories
Find more on Get Started with Computer Vision Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!