clc;clear all;close all; 清除所有
y_Center = 115;
x_Center = 210; % center;285445 以上两句为中心点的坐标
Hy = 10;
Hx = 10; 定义核函数的带宽,一般为搜索窗口的一半
NImage = 11; % number of frame; 处理图像的总帧数
bin = 32; % number of bin; 灰度级分组, 32组
path = 'hall_bitmap_cif\0'; 图像的路径
weighed_each_bins = zeros(bin,NImage); 定义一个0矩阵weighed_each_bins 每列为相应图像的一个直方图
%
% show the target region of first frame;
iImage0 = 25;
ImageFileName =[path, num2str(iImage0),'.bmp']; 以上两句将iImage0作为图像名,并将图像赋给ImageFileName
Image0 = imread(ImageFileName); 读取图像ImageFileName ,作为第一帧图像
Image0 = rgb2gray(Image0); 将图像转化为灰度图像
weighed_each_bins(:,1) = kernelweigh( y_Center, x_Center, Hy, Hx, Image0); 对第一帧图像建立直方图
% define the target region;
Image0((x_Center - Hx):(x_Center + Hx), (y_Center - Hy)) = zeros(length((x_Center - Hx):(x_Center + Hx)),1);
Image0((x_Center - Hx):(x_Center + Hx), (y_Center + Hy)) = zeros(length((x_Center - Hx):(x_Center + Hx)),1);
Image0((x_Center - Hx), (y_Center - Hy):(y_Center + Hy)) = zeros(1,length((y_Center - Hy):(y_Center + Hy)));
Image0((x_Center + Hx), (y_Center - Hy):(y_Center + Hy)) = zeros(1,length((y_Center - Hy):(y_Center + Hy)));
figure; 以上几句确定第一帧图像的目标区域
imshow(Image0);
hold on;
plot(y_Center, x_Center, 'o'); 目标区域中心点处画上一个圈
title('Frame1') 确定标题Frame1
%
for iImage = iImage0+1 : iImage0 + NImage
%
ImageFileName =[path, num2str(iImage),'.bmp'];
Image = imread(ImageFileName);
Image = rgb2gray(Image); 取当前图像存入Image
index = iImage-iImage0;
% Step 1: Initialize the location of the target in the current frame index表示当前是第几帧图像
% with y0, compute pdf
% 在当前窗口得到目标的初始位置
weighed_each_bins(:,index) = kernelweigh( y_Center, x_Center, Hy, Hx, Image); 对当前图像建立直方图
%bar(weighed_each_bins(:,iImage))
% Evalute the Bhattacharyya coefficient
Rou0 = 0;
for iBins = 1:bin
Rou0 = Rou0 + sqrt(weighed_each_bins( iBins, 1) * weighed_each_bins( iBins, index));
end 此for循环用于计算相似性函数,将每组灰度级下的相似性函数值计算出来后相加
% Calculate the weight of each pixel position in the candidate target
x_Center_tmp = 0;
y_Center_tmp = 0;
iteration = 0; 定义了三个变量并初始化为0
% iterate for the optimal solution
while (sqrt((x_Center_tmp - x_Center)^2 +(y_Center_tmp - y_Center)^2) > 1)&&(iteration < 10) iteration为最大迭代次数
% Step 2: Derive the weights wi and find the next location of the target candidate;
wi = 0;
% x_Center1_Den: denominator of formula to compute new center y1;
% x_Center1_Nom: numerator of formula to compute new center y1;
% <Renference>
x_Center1_Nom = 0;
x_Center1_Den = 0;
y_Center1_Nom = 0;
y_Center1_Den = 0;
%
for ix = (x_Center - Hx):(x_Center + Hx)
for iy = (y_Center - Hy):(y_Center + Hy)
wi = 0;
for iBins = 1:bin
% the interval segment of every "bin";
BoundBins = round((255 - 0 + 1)/bin); % the "bin" size; 计算每组的灰度级个数
% the upper bound and the lower bound of the current bin
valueBinsLow = BoundBins * (iBins - 1); 每组灰度级的最小值
valueBinsHigh = BoundBins * (iBins) - 1; 每组灰度级的最大值
% compute wi;
if (Image(ix,iy) >= valueBinsLow) && (Image(ix,iy) <= valueBinsHigh) % whether the pixel belongs to this "bin"; 如果像素属于该组
if weighed_each_bins(iBins, 1)==0 && weighed_each_bins(iBins, index)==0 如果第一帧图像且当前图像在该灰度组的像素为0
wi = wi + 1; 权重值就加1
else
if weighed_each_bins(iBins, index) == 0
wi = wi +1;
%weighed_each_bins(iBins, 1)
else
wi = wi + sqrt( weighed_each_bins(iBins, 1)/weighed_each_bins( iBins, index));
end
end
break;
end 整个if嵌套就是在计算该组的权重值
end
x_Center1_Nom = x_Center1_Nom + (ix - x_Center)*wi;
x_Center1_Den = x_Center1_Den + wi;
y_Center1_Nom = y_Center1_Nom + (iy - y_Center)*wi;
y_Center1_Den = y_Center1_Den + wi;
end % end for iy = (y_Center - Hy):(y_Center + Hy)
end % end for ix = (x_Center - Hx):(x_Center + Hx)
%Step3: modify the new center candidate y1 (x_Center1, y_Center1);
x_Center1 = x_Center1_Nom/x_Center1_Den + x_Center;
y_Center1 = y_Center1_Nom/y_Center1_Den + y_Center; 根据算出的mean shift向量计算新的中心位置
%
% Step 4: Compute the newer target model at location (x_Center1, y_Center1) and the Bhattacharyya coefficient
%
weighed_each_bins(:,index) = kernelweigh( round(y_Center1), round(x_Center1), Hy, Hx, Image);中心移动了,窗口也跟着滑动,在新窗口下建立直方图
% Evalute the Bhattacharyya coefficient
Rou1 = 0;
for iBins = 1:bin
Rou1 = Rou1 + sqrt(weighed_each_bins(iBins, 1) * weighed_each_bins( iBins, index));
end
计算新的相似性函数Rou1
%Step5:
if (Rou1<Rou0)
x_Center1 = (x_Center1 + x_Center)/2;
y_Center1 = (y_Center1 + y_Center)/2; 如果相似性降低,那么窗口中心位置在原来中心和当前中心的中点处
weighed_each_bins(:, index) = kernelweigh( round(y_Center1), round(x_Center1), Hy, Hx, Image);在对当前候选区建直方图模型
Rou1 = 0;
for iBins = 1:bin
Rou1 = Rou1 + sqrt(weighed_each_bins(iBins, 1) * weighed_each_bins( iBins, index));
end
end; 再计算当前的相似性函数值
%上次的中心位置
x_Center_tmp = x_Center;
y_Center_tmp = y_Cen
- 1
- 2
- 3
- 4
- 5
- 6
前往页