function varargout = main(varargin)
% MAIN MATLAB code for main.fig
% MAIN, by itself, creates a new MAIN or raises the existing
% singleton*.
%
% H = MAIN returns the handle to a new MAIN or the handle to
% the existing singleton*.
%
% MAIN('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in MAIN.M with the given input arguments.
%
% MAIN('Property','Value',...) creates a new MAIN or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before main_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to main_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 main
% Last Modified by GUIDE v2.5 26-Mar-2018 08:59:30
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @main_OpeningFcn, ...
'gui_OutputFcn', @main_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 main is made visible.
function main_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 main (see VARARGIN)
% Choose default command line output for main
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes main wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = main_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;
function edit2_Callback(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double
% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
[filename, pathname] = uigetfile( '*.avi', '开始');
implay([pathname,filename]);
mov=VideoReader(filename);
frames=read(mov);
%计数车流量
car_count=0;
set(handles.edit2,'string',num2str(0));
for i=1:2:size(frames,4)-1
%读取视频文件
img0=frames(:,:,:,i);%i表示要读取的第几帧
%显示当前处理的帧数
set(handles.edit1,'string',num2str(i));
%显示原视频文件
axes(handles.axes3);
imshow(img0);
axes(handles.axes4);
[us,vs]=HSoptflow(frames,i);
i3=im2bw(vs);
SE1=strel('square',1);
%腐蚀操作
a_erode=imerode(i3,SE1,'same'); %erode
SE2 = strel('square',7);
%膨胀操作
erode_dilate_img=imdilate(a_erode,SE2,'same'); %dilate
Ibw=im2bw(erode_dilate_img);
status=regionprops(Ibw,'BoundingBox');
%axes(handles.axes3);
%画出计数界线
[m,n]=size(Ibw);
Ibw(60-2:60+2,:)=1;
imshow(Ibw);hold on;
%画出轮廓并计数
for i=1:size(status,1)
if status(i).BoundingBox(3)<n/2&&status(i).BoundingBox(3)>n/10&&status(i).BoundingBox(4)<m/1.5&&status(i).BoundingBox(4)>m/5
rectangle('position',status(i).BoundingBox,'edgecolor','r');
if abs(status(i).BoundingBox(2)+status(i).BoundingBox(4)-60)<3
car_count=car_count+1;
set(handles.edit2,'string',num2str(floor((car_count+1)/2)));
end
end
end
end
% --- 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)
[filename, pathname] = uigetfile( '*.avi', '开始');
% readerobj = mmreader([pathname,filename]);
rgb = 3;
gray = 1;
nop = 0;
% get(readerobj )
implay([pathname,filename]);
mov=VideoReader(filename);
frames=read(mov);
%读取第一帧图片
img0 = read( mov, 1);
i1=rgb2gray(img0);
[m,n]=size(i1);
im1=double(i1);
%建立保存图片
i3=zeros(size(i1));
%计数车流量
car_count=0;
set(handles.edit2,'string',num2str(0));
for i=1:2:size(frames,4)
%读取视频文件
img=frames(:,:,:,i);%i表示要读取的第几帧
%显示当前处理的帧数
set(handles.edit1,'string',num2str(i));
%显示原视频文件
axes(handles.axes1);
imshow(img);
%灰度化
i2=rgb2gray(img);
im2=double(i2);
im1=medfilt2(im1,[3,3]);
im2=medfilt2(im2,[3,3]);
%直接做差
% for i=1:m
% for j=1:n
% i3(i,j)=im2(i,j)-im1(i,j);
% end;
% end;
%差别超过阈值
for i=1:m
for j=1:n
if abs(im2(i,j)-im1(i,j))>40%&&im2(i,j)>80
i3(i,j)=255;
else
i3(i,j)=0;
end
end;
end;
SE1=strel('square',4);
%腐蚀操作
a_erode=imerode(i3,SE1,'same'); %erode
SE2 = strel('square',20);
%膨胀操作
erode_dilate_img=imdilate(a_erode,SE2,'same'); %dilate
%去掉上面的干扰部分
%erode_dilate_img(1:100,:)=0;
%size(erode_dilate_img)
axes(handles.axes2);
%imshow(erode_dilate_img);
Ibw=im2bw(erode_dilate_img);
status=regionprops(Ibw,'BoundingBox');
%axes(handles.axes3);
%画出计数界线
Ibw(60-2:60+2,:)=1;
imshow(Ibw)
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
基于MATLAB编程,有GUI界面,分别用侦差法和光流法进行车流量统计,代码完整,包含数据,有注释,方便扩展应用 1,如有疑问,不会运行,可以私信, 2,需要创新,或者修改可以扫描二维码联系博主, 3,本科及本科以上可以下载应用或者扩展, 4,内容不完全匹配要求或需求,可以联系博主扩展。
资源推荐
资源详情
资源评论
收起资源包目录
code.rar (7个子文件)
viptraffic.avi 204KB
main.m 8KB
1.avi 1.57MB
main.fig 12KB
HSoptflow.m 2KB
6.avi 3.39MB
viptraffic_Transcode.avi 299KB
共 7 条
- 1
资源评论
神经网络机器学习智能算法画图绘图
- 粉丝: 2398
- 资源: 577
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功