foregroundDetector = vision.ForegroundDetector('NumGaussians', 3, ...
'NumTrainingFrames', 50);
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);
blobAnalysis1 = vision.BlobAnalysis('BoundingBoxOutputPort', false, ...
'AreaOutputPort', false, 'CentroidOutputPort', true, ...
'MinimumBlobArea', 150);
bbox = step(blobAnalysis, filteredForeground);
bbox1 = step(blobAnalysis1, filteredForeground);
result = insertShape(frame, 'Rectangle', bbox, 'Color', 'green');
result = insertMarker(result, bbox1, 'x', 'Color', 'red');
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]
videoB = vision.VideoPlayer('Name', 'Detected Cars');
videoCB = vision.VideoPlayer('Name', 'Detected Cars');
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);
bbox1 = step(blobAnalysis1, filteredForeground);
% Draw bounding boxes around the detected cars
result = insertShape(frame, 'Rectangle', bbox, 'Color', 'green');
posline = [0 250 1000 250];
result = insertShape(result, 'line', posline );
% c = insertMarker(result, bbox1, 'o', 'Color', 'red');
% result = c;
result = bwlabel(result);
stats = regionprops(result, 'BoundingBox', 'Centroid');
a=text(AllCentroids(:,1)+15,AllCentroids(:,2), strcat('X: ', num2str(round(AllCentroids(:,1))), ' Y: ', num2str(round(AllCentroids(:,2)))));
set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'red');
for object = 1:length(stats)
rectangle('Position',AllBB(k,:),'EdgeColor','r','LineWidth',2)
plot(AllCentroids(object,1),AllCentroids(object,2), '-m+')
end
% Display the number of cars found in the video frame
numCars = size(bbox1, 1);
% numCars = 0;
% if (position(b) == position(c));
% numCars =+ 1;
% end
result1 = insertText(result, [10 10], numCars, 'BoxOpacity', 1, ...
'FontSize', 14);
step(videoPlayer, result1); % display the results
step(videoB, foreground); % display the results
step(videoCB, filteredForeground); % display the results
end
release(videoReader); % close the video file
AB.zip_insertShape
版权申诉
39 浏览量
2022-09-24
21:25:43
上传
评论
收藏 1KB ZIP 举报
小贝德罗
- 粉丝: 71
- 资源: 1万+
最新资源
- 基于宏晶STC单片机开发ISP程序Demo-使用Y-Modem协议软件源码+文档说明资料.zip
- 基于Vue+Echarts开发的风力发电机中传感器的数据展示监控
- 新普_软包电池_三是调试工程05301.zip
- 基于python遥感毕业设计TOA深度学习反演PM2.5源码+文档说明(高分项目)
- 基于stc8A系列单片机和ov7670模块制作的人脸识别系统
- 基于python的系统设计与实现博客系统.zip
- SSH客户机Xshell资源包
- 未命名1.cpp
- AD7177-2 AD7175-2, AD7172-2 AD717X-系列芯片多路复用模数转换器驱动程序C源码.zip
- 后端框架入门掌握SpringBoot和Django等后端框架电子资源pdf
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈