function varargout = EasyTrack(varargin)
% EASYTRACK MATLAB code for EasyTrack.fig
% EASYTRACK, by itself, creates a new EASYTRACK or raises the existing
% singleton*.
%
% H = EASYTRACK returns the handle to a new EASYTRACK or the handle to
% the existing singleton*.
%
% EASYTRACK('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in EASYTRACK.M with the given input arguments.
%
% EASYTRACK('Property','Value',...) creates a new EASYTRACK or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before EasyTrack_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to EasyTrack_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 EasyTrack
% Last Modified by GUIDE v2.5 12-Jun-2016 23:54:34
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @EasyTrack_OpeningFcn, ...
'gui_OutputFcn', @EasyTrack_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 EasyTrack is made visible.
function EasyTrack_OpeningFcn(hObject, ~, 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 EasyTrack (see VARARGIN)
% Choose default command line output for EasyTrack
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes EasyTrack wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = EasyTrack_OutputFcn(~, ~, 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 mainB_Callback(~, ~, handles)
%数据的前期准备
set(handles.numText,'string',0);%计数框清零
set(handles.endTime,'string',0);%结束时间清零
set(handles.startTime,'string',datestr(datetime('now')));
set(handles.infoP,'Visible','on');
%视频获取途径的选择
camSwitch=get(handles.radioCam,'value');%获取值
position=handles.position;
areaSwitch=handles.areaSwitch;
if areaSwitch>0;
BW = handles.BW;%传递二进制图
end
%保存设置;
saveSwitch=handles.saveSwitch;
if saveSwitch>0.5
saveName=get(handles.saveName,'String');
saveName=[saveName,'.avi'];
videoFWriter = vision.VideoFileWriter(saveName);%设定记录器及名称
videoFWriter.VideoCompressor='ffdshow Video Codec';%设定水平压缩的方式
end
timeLimit=str2double(get(handles.timeLimit,'String'));
nextId = 1; %track的初始值ID值
uniqueReliableIds=[];%收集可信的track的唯一id
startIdsCount=0;%用于统计变量的不同值
idsCount=0;
obj.Dplayer=vision.DeployableVideoPlayer('Name','Detecting Video');%设定播放器
%obj.MaskDplayer=vision.DeployableVideoPlayer('Name','Detecting Mask');%设定播放器
%定义检测器
obj.detector = vision.ForegroundDetector('NumGaussians', 5, ...
'NumTrainingFrames', 40, 'MinimumBackgroundRatio', 0.7);
%定义分析器
obj.blobAnalyser = vision.BlobAnalysis('BoundingBoxOutputPort', true, ...
'AreaOutputPort', true, 'CentroidOutputPort', true, ...
'MinimumBlobArea', 400,'MaximumBlobArea',20000);
%初始化跟踪路径的数据
tracks = struct(...
'id', {}, ...
'label',{},...
'time',{},...
'bbox', {}, ...
'kalmanFilter', {}, ...
'age', {}, ...
'totalVisibleCount', {}, ...
'consecutiveInvisibleCount', {});
%定义一个收集器
reliableTracksCollection=struct(...
'id', {}, ...
'label',{},...
'time',{},...
'bbox', {}, ...
'kalmanFilter', {}, ...
'age', {}, ...
'totalVisibleCount', {}, ...
'consecutiveInvisibleCount', {});
% 定义在main的坐标系中工作
axes(handles.main);
runTime=0;
tic;%计时开始
%重点的重点
%开始每一帧的处理
while hasFrame(handles.video)%runTime<timeLimit%~isDone(obj.reader)
if camSwitch>0
frame=snapshot(handles.cam);
else
frame=readFrame(handles.video);
end
%预处理frame,就是只对框选的范围用mask
if areaSwitch>0.5
[~,~,imc]=size(frame);
switch imc
case 1
I2=frame;
I2(~BW)=0;
case 2
IR=frame(:,:,1);
IG=frame(:,:,2);
IR(~BW)=0;
IG(~BW)=0;
I2=cat(3,IR,IG);
case 3
IR=frame(:,:,1);
IG=frame(:,:,2);
IB=frame(:,:,3);
IR(~BW)=0;
IG(~BW)=0;
IB(~BW)=0;
I2=cat(3,IR,IG,IB);
otherwise
disp('图像为不正规图像!无法处理!')
end
mask = obj.detector.step(I2);
else
mask = obj.detector.step(frame);
end
%进行扑拓处理
mask = imopen(mask, strel('rectangle', [3,3]));
mask = imclose(mask, strel('rectangle', [15, 15]));
mask = imfill(mask, 'holes');
%获取分析图像
[~, centroids, bboxes] = obj.blobAnalyser.step(mask);
%预测新的路径
for i = 1:length(tracks)
bbox = tracks(i).bbox;
%获取预测点的中心
predictedCentroid = predict(tracks(i).kalmanFilter);
%预测点与中心点进行数值处理
predictedCentroid = int32(predictedCentroid) - bbox(3:4) / 2;
tracks(i).bbox = [predictedCentroid, bbox(3:4)];
end
%获得测试数量
nTracks = length(tracks);
nDetections = size(centroids, 1);
% 计算路径与损失路径
cost = zeros(nTracks, nDetections);
for i = 1:nTracks
cost(i, :) = distance(tracks(i).kalmanFilter, centroids);
end
% Solve the assignment problem.
costOfNonAssignment = 20;%原路径与现有路径之间的距离区分的阈值
[assignments, unassignedTracks, unassignedDetections] = ...
assignDetectionsToTracks(cost, costOfNonAssignment);
%更新跟踪点
numAssignedTracks = size(assignments, 1);
for i = 1:numAssignedTracks
trackIdx = assignments(i, 1);
detectionIdx = assignments(i, 2);
centroid = centroids(detectionIdx, :);
bbox = bboxes(detectionIdx, :);
%验证数据
correct(tracks(trackIdx).kalmanFilter, centroid);
%更新跟踪点
tracks(trackIdx).bbox = bbox;
tracks(trackIdx).age = tracks(trackIdx).age + 1;
tracks(trackIdx).totalVisibleCount = ...
tracks(trackIdx).totalVisibleCount + 1;
tracks(trackIdx).consecutiveInvisibleCount = 0;
end
%跟新没有被分配的点;
for i = 1:length(unassignedTracks)
ind = unassignedTracks(i);
tracks(ind).age = tracks(ind).age + 1;
tracks(ind).consecutiveInvisibleCount = ...
tracks(ind).consecutiveInvisibleCount + 1;
end
%删除不匹配的点;
if ~isempty(tracks)%修改这部
invisibleForTooLong = 20;
ageThreshold = 8;
ages = [tracks(:).age];
totalVisibleCounts = [tracks(:).totalVisibleCount];
visibility = totalVisibleCounts ./ ages;
- 1
- 2
前往页