clear all;
close all;
vehicle_num = 0; tempones = 0;
block_info = zeros(4,4); % 用于存储虚拟检测框信息([列,行,宽,高])
point_X = zeros(4,5); % 存储检测框点横坐标信息
point_Y = zeros(4,5); % 存储检测框点纵坐标信息
block_area = zeros(1,4);
% 读入视频
videoObj = VideoReader('D:\matlabCode\cars\test 1.avi');
numFrames =videoObj.NumberOfFrames;
FPS = videoObj.FrameRate; %获取帧率(fps)
% 读入视频序列的第一帧作为初始值
newframe = read(videoObj, 1);
fmed = double(newframe);
[height, width, numColor]=size(newframe);
figure(1);
imshow(newframe);
for i=1:4
k = waitforbuttonpress; % 等待鼠标按下
point1 = get(gca,'CurrentPoint'); % 鼠标按下了
finalRect = rbbox; %
point2 = get(gca,'CurrentPoint'); % 鼠标松开了
point1 = point1(1,1:2); % 提取出两个点
point2 = point2(1,1:2);
p1 = min(floor(point1),floor(point2)); % 计算位置
p2 = max(floor(point1),floor(point2));
offset = abs(floor(point1)-floor(point2)); % offset(1)表示宽,offset(2)表示高
x = [p1(1) p1(1)+offset(1) p1(1)+offset(1) p1(1) p1(1)];
y = [p1(2) p1(2) p1(2)+offset(2) p1(2)+offset(2) p1(2)];
hold on %防止plot时闪烁
plot(x,y,'r');
block_info(i,1:2) = round(point1(1,1:2));
block_info(i,3:4) = offset(1,1:2);
block_area(i) = offset(1) * offset(2);
point_X(i,1:5) = x(1,1:5);
point_Y(i,1:5) = y(1,1:5);
end
% 近似中值背景模型参数阈值
Threh = 20;
beta = 1.0;
fg = zeros(height, width);
%设置检测带参数
blockthresh = 0.5; % 阈值(面积大于该阈值则表示有车)!!!!!!最重要的参数
pre_stream_data = zeros(1,4);
% 为了避免消耗过多的内存,每次只读取一个帧
for n = 1:numFrames %%%%!!!!!!numFrames可以调整
newframe = read(videoObj, n);
%近似中值背景模型
Idiff = double(newframe) - fmed;
pixInc = find(Idiff > 0);
fmed(pixInc) = fmed(pixInc) + beta;
pixDec = find(Idiff < 0);
fmed(pixDec) = fmed(pixDec) - beta;
fg = abs(Idiff) >Threh;
if ( numColor == 3)
fg = fg(:, :, 1) | fg(:, :, 2) | fg(:, :, 3);
end
% 图像显示
figure(1);
% 原图
subplot(2,2,1), imshow(newframe);
title(strcat('当前帧为No. ', int2str(n)));
% 近似中值滤波分割结果
subplot(2,2,2), imshow(fg);
title('近似中值滤波分割结果');
%%%%!!!!!!加入去阴影的代码
% 形态学滤波,消除噪点、填充车辆分割区域
% se=strel('square',3'); % 方型结构元素 % 可调整!!!!!!
se=strel('disk',3'); % 圆盘型结构元素
fg =imopen(fg,se);
fg =imdilate(fg,se);
subplot(2,2,3), imshow(fg);
%%%%!!!!!!加入去阴影的代码
title('形态学滤波结果');
% 加入检测框
subplot(2,2,4), imshow(newframe);
hold on;
for i = 1:4
x = point_X(i,1:5);
y = point_Y(i,1:5);
plot(x,y,'r');
end
% 对检测框提取数据信息,用来判断检测框内是否有车辆
cur_stream_data = zeros(1,4);
for driveway = 1:4 % 总共4个车道 %%%%!!!!!!可随车道数目更改
% fg(起始行:终止行,起始列:终止列)
start_row = block_info(driveway,2); % 起始行
end_row = block_info(driveway,2) + block_info(driveway,4) - 1; %终止行
start_column = block_info(driveway,1); % 起始列
end_column = block_info(driveway,1) + block_info(driveway,3) - 1; %终止列
fgstrip = fg(start_row:end_row,start_column:end_column);
% 计算每个检测框中图像块中“1”的个数
block_sum = sum(sum(fgstrip));
% 该检测框像素总数(即面积)
%block_area = block_info(driveway,3)*block_info(driveway,4);
% 确定是否有车,如果该块中1的个数占该块像素总数的比例>= blockthresh.
if block_sum/block_area(driveway) >= blockthresh
% 表明检测框内有车,将cur_stream_data对应元素值置1
cur_stream_data(driveway) = 1;
end
end
% 检测带状态变化及其设置
% 检测带状态变化
stream_change = cur_stream_data - pre_stream_data;
%保存检测带车流状态,以便下一帧使用
pre_stream_data = cur_stream_data;
% 将检测带车流状态变化量stream_change中的1置为0,(1对应车进入)
stream_change(stream_change == 1) = 0;
% 将检测带车流状态变化向量stream_change中的-1置为1,以便统计和显示
% 统计stream_change中连续1(即原-1)的个数超过stream_changewidth_thresh的段数,每段即表明通过一辆车
stream_change(stream_change == -1) = 1;
% 统计车辆数
tempones = 0;
for i = 1: size(stream_change, 2)
if stream_change(i)
vehicle_num = vehicle_num + 1;
end
end
title(strcat('加入检测框,当前车辆数为', int2str(vehicle_num)));
end
car.zip_matlab 车流量_手动画框检测_流量检测matlab_车流量_车流量检测matlab代码
版权申诉
132 浏览量
2022-07-15
19:04:30
上传
评论 1
收藏 2KB ZIP 举报
四散
- 粉丝: 50
- 资源: 1万+
最新资源
- python-leetcode面试题解之第157题用Read4读取N个字符-题解.zip
- python-leetcode面试题解之第156题上下翻转二叉树-题解.zip
- python-leetcode面试题解之第155题最小栈-题解.zip
- python-leetcode面试题解之第153题寻找旋转排序数组中的最小值-题解.zip
- python-leetcode面试题解之第152题乘积最大子数组-题解.zip
- python-leetcode面试题解之第151题反转字符串中的单词-题解.zip
- python-leetcode面试题解之第150题逆波兰表达式求值-题解.zip
- python-leetcode面试题解之第149题直线上最多的点数-题解.zip
- python-leetcode面试题解之第148题排序链表-题解.zip
- python-leetcode面试题解之第147题对链表进行插入排序-题解.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈