function varargout = matlab(varargin)
% MATLAB MATLAB code for matlab.fig
% MATLAB, by itself, creates a new MATLAB or raises the existing
% singleton*.
%
% H = MATLAB returns the handle to a new MATLAB or the handle to
% the existing singleton*.
%
% MATLAB('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in MATLAB.M with the given input arguments.
%
% MATLAB('Property','Value',...) creates a new MATLAB or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before matlab_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to matlab_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help matlab
% Last Modified by GUIDE v2.5 08-Jun-2019 08:46:42
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @matlab_OpeningFcn, ...
'gui_OutputFcn', @matlab_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before matlab is made visible.
function matlab_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to matlab (see VARARGIN)
% Choose default command line output for matlab
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes matlab wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = matlab_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%创建系统对象,用于读入待处理视频
[filename0,permission]=uigetfile({'*.mp4';'*.avi';});
% Inimg=imread(strcat(permission,filename));
% filename = 'viptraffic02.mp4';
filename = filename0;
% 创建系统对象,用于读入待处理视频
hvfr = vision.VideoFileReader(filename, 'ImageColorSpace', 'RGB');
% 创建系统对象,用于色彩空间转换
hcsc = vision.ColorSpaceConverter('Conversion', 'RGB to intensity');
% 创建系统对象,用于用高斯混合模型检测背景
hfdet = vision.ForegroundDetector(...
'NumTrainingFrames', 5, ... % 取连续五帧进行检测背景
'InitialVariance', (30/255)^2); % 初始标准差为 30/255
% 创建系统对象,用于检测出包含汽车运动的图像块
hblob = vision.BlobAnalysis( ...
'CentroidOutputPort', false, ...
'AreaOutputPort', true, ...
'BoundingBoxOutputPort', true, ...
'OutputDataType', 'single', ...
'MinimumBlobArea', 250, ...
'MaximumBlobArea', 3600, ...
'MaximumCount', 80);
% 创建系统对象,用于对检测出的运动车辆进行框画
hshapeins = vision.ShapeInserter( ...
'BorderColor', 'Custom', ...
'CustomBorderColor', [0 255 0]);
% 创建系统对象,用于标注检测到车辆的个数
htextins = vision.TextInserter( ...
'Text', '%4d', ...
'Location', [1 1], ...
'Color', [255 255 0], ...
'FontSize', 12);
% 创建系统对象,用于显示结果
sz = get(0,'ScreenSize');
pos = [20 sz(4)-300 200 200];
hVideoOrig = vision.VideoPlayer('Name', 'Original', 'Position', pos);
pos(1) = pos(1)+320; %在右侧建立下一个视窗
hVideoFg = vision.VideoPlayer('Name', 'Foreground', 'Position', pos);
pos(1) = pos(1)+320;
hVideoRes = vision.VideoPlayer('Name', 'Results', 'Position', pos);
line_row = 23; % 定义感兴趣区域(ROI)
% 以下的程序段为对输入的视频图像进行处理
i=1;
t=0;0.1;8;
% count(i)=[];
while ~isDone(hvfr)
image = step(hvfr); % 读入视频的每一帧
y = step(hcsc, image); % 将彩色图像转换成灰度图像
% 采用自动白平衡算法去除灰度突变
y = y-mean(y(:));
fg_image = step(hfdet, y); % 检验背景
% 采用数学形态学,在前景图像中检测变化的联通图像块区域的面积
[area, bbox] = step(hblob, fg_image);
image_out = image;
image_out(22:23,:,:) = 255; % 仅对经过白线后的车辆进行计数
image_out(1:15,1:30,:) = 0; % 将背景置为黑色
Idx = bbox(:,2) > line_row; %选择感兴趣区域.
% 当在感兴趣区域中,联通图像块的比例占整个变化区域的40%以上时,便认为是车辆
ratio = zeros(length(Idx),1);
ratio(Idx) = single(area(Idx,1))./single(bbox(Idx,3).*bbox(Idx,4));
ratiob = ratio > 0.4;
count(i) = int32(sum(ratiob)); % 车辆的数量
bbox(~ratiob,:) = int32(-1);
% 将检测出的车辆圈画出来
image_out = step(hshapeins, image_out, bbox);
% 显示检测汽车的数量
image_out = step(htextins, image_out, count(i));
i=i+1;
step(hVideoOrig, image); % 原始视频
step(hVideoFg, fg_image); % 前景
step(hVideoRes, image_out); % 用方框勾画检测出的车辆
pause(0.25);
end
j=1;
len=size(count);
n=floor(len(2)/15);
icount=zeros(n,1);
for i=1:1:n
num=0;
n=j+(i-1)*15;
% for m=n:n+13
% if (count(m)~=count(m+1))
% num=num+1;
% end
% end
icount(i)=max(count(n:n+14));
% icount(i)=num;
% icount(i)=count(n)+count(n+1)+count(n+2)+count(n+3)+count(n+4)+count(n+5)+count(n+6)+count(n+7)+count(n+8)+count(n+9)+count(n+10)+count(n+11)+count(n+12)+count(n+13)+count(n+14);
end
%关闭视频文件
release(hvfr);
figure;
plot(icount);
xlabel('秒');
ylabel('车辆数');