matlab光流法前景分割,背景建模运动目标跟踪 matlab程序

题目:背景建模和前景分割的方式把运动车辆提取出来。并进行最近临关联,输出目标轨迹。

混合高斯模型使用K(基本为3到5个) 个高斯模型来表征图像中各个像素点的特征,在新一帧图像获得后更新混合高斯模型,用当前图像中的每个像素点与混合高斯模型匹配,如果成功则判定该点为背景点, 否则为前景点。通观整个高斯模型,他主要是有方差和均值两个参数决定,,对均值和方差的学习,采取不同的学习机制,将直接影响到模型的稳定性、精确性和收敛性。由于我们是对运动目标的背景提取建模,因此需要对高斯模型中方差和均值两个参数实时更新。

程序:(参考其他程序改进而得)

% mixture of Gaussians algorithm for background混合高斯模型背景建模

%混合高斯模型适用于相机固定的运动目标检测,光流法适用于相机运动的运动目标检测

close all;

clear all;

source=dir('*.bmp');

% -----------------------  frame size variables -----------------------

% read in 1st frame as background frame

fr=imread(source(1).name);

fr_bw = rgb2gray(fr);     % convert background to greyscale

fr_size = size(fr);

width = fr_size(2);

height = fr_size(1);

fg = zeros(height, width);

bg_bw = zeros(height, width);

% --------------------- mog variables -----------------------------------

C = 3;                                  % number of gaussian components (typically 3-5)   Cgegaosimoxing

M = 3;                                  % number of background components,

D = 2.5;                                % positive deviation threshold

alpha = 0.01;                           % learning rate (between 0 and 1) (from paper 0.01) the background will change slowly with the time, so the mean(u)should be updated slowly.

thresh = 0.25;                          % foreground threshold (0.25 or 0.75 in paper)

sd_init = 6;                            % initial standard deviation (for new components) var = 36 in paper

w = zeros(height,width,C);              % initialize weights array

mean = zeros(height,width,C);           % pixel means  , the u of gaosi(u,d)

sd = zeros(height,width,C);             % pixel standard deviations , the d of gaosi(u,d)

u_diff = zeros(height,width,C);         % difference of each pixel from mean

p = alpha/(1/C);                        % initial p variable (used to update mean and sd)

rank = zeros(1,C);                      % rank of components (w/sd)

% --------------------- initialize component means and weights -----------

pixel_depth = 8;                        % 8-bit resolution

pixel_range = 2^pixel_depth -1;         % pixel range (# of possible values)

for i=1:height

for j=1:width

for k=1:C

mean(i,j,k) = rand*pixel_range;     % means random (0-255)

w(i,j,k) = 1/C;                     % weights uniformly dist

sd(i,j,k) = sd_init;                % initialize to sd_init

end

end

end

%--------------------- process frames -----------------------------------

for n = 8:(length(source)-2)              %there will be false route line,so it only include the pictures which has the car

fr = imread(source(n).name);       % read in frame

fr_bw = rgb2gray(fr);       % convert frame to grayscale

% calculate difference of pixel values from mean

for m=1:C

u_diff(:,:,m) = abs(double(fr_bw) - double(mean(:,:,m)));

end

sum_x=0;

sum_y=0;

num=0;

% update gaussian components for each pixel

for i=1:height                        %%%%%%%%%%%%%search each pixal of one image, if it is in the C ge gaosimoxing, it is belong to the background,and undate the background.

for j=1:width                     %%%%%%%%%% If it is not in the C ge gaosimoxing, create a new gaosi and replace the least possible gaosi. Finally the the first several gaosi is background, and the last several is foreground

match = 0;

for k=1:C

if (abs(u_diff(i,j,k)) <= D*sd(i,j,k))       % pixel matches component

match = 1;                          % variable to signal component match

% update weights, mean, sd, p

w(i,j,k) = (1-alpha)*w(i,j,k) + alpha;

p = alpha/w(i,j,k);

mean(i,j,k) = (1-p)*mean(i,j,k) + p*double(fr_bw(i,j));

sd(i,j,k) =   sqrt((1-p)*(sd(i,j,k)^2) + p*((double(fr_bw(i,j)) - mean(i,j,k)))^2);

else                                    % pixel doesn't match component

w(i,j,k) = (1-alpha)*w(i,j,k);      % weight slighly decreases

end

end

bg_bw(i,j)=0;

for k=1:C

bg_bw(i,j) = bg_bw(i,j)+ mean(i,j,k)*w(i,j,k);

end

% if no components match, create new component

if (match == 0)

[min_w, min_w_index] = min(w(i,j,:));

mean(i,j,min_w_index) = double(fr_bw(i,j));

sd(i,j,min_w_index) = sd_init;

end

rank = w(i,j,:)./sd(i,j,:);             % calculate component rank

rank_ind = [1:1:C];

% calculate foreground

fg(i,j) = 0;

while ((match == 0)&&(k<=M))

if (abs(u_diff(i,j,rank_ind(k))) <= D*sd(i,j,rank_ind(k)))

fg(i,j) = 0; %black = 0

else

fg(i,j) = fr_bw(i,j);

sum_x=sum_x+j;

sum_y=sum_y+i;

num=num+1;

end

k = k+1;

end

end

end

if n==8||n==9||n==10

route_x=[round(sum_x/num),round(sum_x/num)];

route_y=[round(sum_y/num),round(sum_y/num)];

else

next_x=round(sum_x/num);

next_y=round(sum_y/num);

route_x=[route_x,next_x];

route_y=[route_y,next_y];

end

num=0;

figure(1);

subplot(3,1,1);

imshow(fr);

title('原始图像');

subplot(3,1,2);

imshow(uint8(bg_bw));

title('背景图像');

subplot(3,1,3);

imshow(uint8(fg));

hold on;

plot(route_x,route_y,'LineWidth',1,'Color','r');

title('前景图像');

%Mov1(n)  = im2frame(uint8(fg),gray);           % put frames into movie

%Mov2(n)  = im2frame(uint8(bg_bw),gray);           % put frames into movie

end

%movie2avi(Mov1,'mixture_of_gaussians_output','fps',30);           % save movie as avi

%movie2avi(Mov2,'mixture_of_gaussians_background','fps',30);           % save movie as avi

运行结果:

0818b9ca8b590ca3270a3433284dd417.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在MATLAB中,可以使用光流法实现目标检测。光流法是一种通过计算连续图像之间的像素运动来估计目标运动的方法。下面是一个基本的光流法目标检测的步骤: 1. 导入图像序列:首先,需要导入包含目标的图像序列。可以通过使用MATLAB的imread函数来读取每一帧的图像。 2. 光流估计:利用MATLAB中的光流估计函数,例如opticalFlowLK函数,计算每个像素的光流向量。这些向量表示了每个像素在两帧图像之间的运动。 3. 特征点提取:为了减少计算量和提高检测准确率,可以使用MATLAB的提取特征点函数,例如detectSURFFeatures或detectFASTFeatures等,来选择特定区域的特征点。 4. 特征匹配:使用MATLAB的matchFeatures函数将两个图像帧之间的特征点进行匹配。 5. 目标跟踪:根据帧间特征点的匹配结果,可以通过计算特征点的平均运动向量或其他跟踪方法来估计目标的运动轨迹。 6. 目标检测:根据目标的运动轨迹,可以通过设置阈值或其他规则来检测和识别目标。 7. 可视化结果:最后,在MATLAB中可以使用图形函数,如plot或imshow等,来可视化目标的检测结果,并将其显示在图像上。 总之,利用MATLAB光流法实现目标检测可以帮助我们从图像序列中提取目标的运动信息,并进行跟踪和检测。这种方法可以广泛应用于视频监控、自动驾驶、行人检测等领域。 ### 回答2: 光流法是一种常用的计算机视觉技术,用于检测视频序列中的目标运动。在MATLAB中,可以使用光流法进行目标检测的步骤如下: 1. 导入视频序列:首先,需要将视频序列导入到MATLAB中,使用VideoReader函数读取视频文件,并获取每个帧的图像数据。 2. 预处理图像:对于每个帧的图像,可以进行预处理步骤,如灰度化、降噪等,以减少计算量和提高光流估计的准确性。 3. 计算光流估计:使用opticalFlow函数计算每个像素点的光流向量。光流向量表示了当前帧中每个像素点相对于前一帧的位置变化。 4. 光流可视化:可以使用plot函数将光流向量可视化,以便观察目标的运动情况。可以通过调整阈值或其他参数来筛选出与目标相关的光流向量。 5. 目标检测:根据目标的运动特点,在光流图中检测和跟踪目标。可以通过设置阈值、区域大小等参数来筛选出目标的光流向量。 6. 目标跟踪:使用跟踪算法(如卡尔曼滤波器)来跟踪目标的位置和运动轨迹。可以使用plot函数将跟踪结果可视化。 7. 输出结果:根据目标检测和跟踪的结果,可以将结果保存为视频或图像文件,或者输出目标的位置和运动参数。 总的来说,MATLAB提供了丰富的函数和工具箱来实现光流法目标检测。通过对视频序列进行预处理、计算光流估计、可视化和目标检测跟踪等步骤,可以有效地实现目标的检测和跟踪任务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值