matlab2017b
clc;
clear;
close all;
warning off;
video = mmreader('vedios\test21.avi');
nFrames = video.NumberOfFrames; %得到帧数
H = video.Height; %得到高度
W = video.Width; %得到宽度
Rate = video.FrameRate;
mov(1:nFrames) = struct('cdata',zeros(H,W,3,'uint8'),'colormap',[]);
Npix = [video.Width video.Height];
F_update = [1 0 1 0;
0 1 0 1;
0 0 1 0;
0 0 0 1];
Npop = 5000;
Xstd_rgb = 50;
Xstd_pos = 25;
Xstd_vec = 5;
Xrgb_trgt = [255; 255; 255];
X = func_create_particles(Npix,Npop);
%%
%SVM预先训练
[svmModel1,svmModel2]=func_SVM_Train();
Xtrack=cell(1,nFrames);
Ytrack=cell(1,nFrames);
%read one frame every time
Xview=[];
Yview=[];
for i = 340:nFrames-10
i
Vedios = read(video,i);
P1 = im2double(rgb2gray(read(video,i)));
P2 = im2double(rgb2gray(read(video,i+1)));
P3 = im2double(rgb2gray(read(video,i+2)));
[Pd,X] = func_tracking(P1,P2,P3,X,F_update,Xstd_pos,Xstd_vec,Xstd_rgb,Xrgb_trgt);
%跟踪效果显示
[P3,Ss,Xd,Yd]= func_track(Vedios,Pd);
%基于SVM进行分类
%获得面积
C1 = [];
C2 = [];
C = [];
if isempty(Ss) == 0;
for jj = 1:length(Ss)
%对面积进行识别
C1 = svmclassify(svmModel1,Ss(jj),'showplot',false);
C2 = svmclassify(svmModel2,Ss(jj),'showplot',false);
if C1 == 'false'
C(jj) = 1;
end
if C1 == 'true'
if C2 == 'false'
C(jj) = 2;
end
if C2 == 'true'
C(jj) = 3;
end
end
end
end
% Ss
% C
[P4,Ss,Xd,Yd,Xt,Yt] = func_track2(Vedios,Pd,Ss,C);
Xtrack{i}=[Xd];
Ytrack{i}=[Yd];
figure(1);
subplot(221);
imshow(P1);
title('原始视频');
subplot(222);
image(Vedios);
hold on
plot(X(2,1:1:end), X(1,1:1:end), '.')
hold off
title('粒子滤波跟踪');
subplot(223);
%跟踪效果
imshow(Pd);
title('跟踪效果');
subplot(224);
%跟踪效果
imshow(P4);
title('跟踪效果');
for jj = 1:length(C)
if C(jj)==1
text(Yt(jj),Xt(jj),'People');
end
if C(jj)==2
text(Yt(jj),Xt(jj),'Electric');
end
if C(jj)==3
text(Yt(jj),Xt(jj),'Car');
end
end
hold on
hold on
end
function [svmModel1,svmModel2]=func_SVM_Train();
M = 3;
N = 1000;
trainData = M*rand(1,N);
for i = 1:length(trainData);
if trainData(i)<=0.5
trainLabel1(i) = 0;%人
end
if trainData(i)>0.5
trainLabel1(i) = 1;
end
end
trainLabel1s = nominal(ismember(trainLabel1,1));
for i = 1:length(trainData);
if trainData(i)<=1.8
trainLabel2(i) = 0;
end
if trainData(i)>1.8
trainLabel2(i) = 1;
end
end
trainLabel2s = nominal(ismember(trainLabel2,1));
svmModel1 = svmtrain(trainData, trainLabel1s,'kernel_function','rbf' ,'showplot',false);
svmModel2 = svmtrain(trainData, trainLabel2s,'kernel_function','rbf' ,'showplot',false);
A10-42
方式1:微信或者QQ联系博主
方式2:订阅MATLAB/FPGA教程,免费获得教程案例以及任意2份完整源码