function [] = select()
clc ;close all;clear all;
%------------------------------------------------------------------------------------------------------------------------
%根據一幅目標全可見的圖像圈定跟蹤目標%
I=imread('1s.jpg'); %選取圖片
figure(1); %創建一個窗口
imshow(I); %默認色值
[temp,rect]=imcrop(I); %利用"滑鼠""剪下""視窗"
[a,b,c]=size(temp); %長 寬 高大小=剪下
%------------------------------------------------------------------------------------------------------------------------
%計算目標圖像的權值矩陣%
y(1)=a/2;
y(2)=b/2;
tic_x =rect(1)+rect(3)/2;
tic_y=rect(2)+rect(4)/2;
m_wei=zeros(a,b);%權值矩陣
h=y(1)^2+y(2)^2 ;%帶寬
for i=1:a
for j=1:b
dist=(i-y(1))^2+(j-y(2))^2;
m_wei(i,j)=1-dist/h; %epanechnikov profile
end
end
C=1/sum(sum(m_wei));%歸一化係數
%------------------------------------------------------------------------------------------------------------------------
%計算目標權值直方圖qu
%hist1=C*wei_hist(temp,m_wei,a,b);%target model
hist1=zeros(1,a*b);
for i=1:a
for j=1:b
%rgb顏色空間量化為16*16*16 bins
q_r=fix(double(temp(i,j,1))/16); %fix為趨近0取整函數
q_g=fix(double(temp(i,j,2))/16);
q_b=fix(double(temp(i,j,3))/16);
q_temp=q_r*256+q_g*16+q_b;%設置每像素點紅色,綠色,藍色分量所占比重
hist1(q_temp+1)= hist1(q_temp+1)+m_wei(i,j);%計算直方圖統計中每個像素點佔的權重?
end
end
hist1=hist1*C;
rect(3)=ceil(rect(3));
rect(4)=ceil(rect(4));
%------------------------------------------------------------------------------------------------------------------------
myfile=dir('*.jpg');%尋找jpg類型的資料
lengthfile=length(myfile);
for l=1:lengthfile
Im=imread(myfile(l).name);
num=0;
Y=[2,2];
%mean shift跌代
while((Y(1)^2+Y(2)^2>0.5)&num<20)%跌代條件
num=num+1;
temp1=imcrop(Im,rect);
%計算候選區域直方圖
%hist2=C*wei_hist(temp1,m_wei,a,b);%target candidates pu
hist2=zeros(1,a*b);
for i=1:a
for j=1:b
q_r=fix(double(temp1(i,j,1))/16);
q_g=fix(double(temp1(i,j,2))/16);
q_b=fix(double(temp1(i,j,3))/16);
q_temp1(i,j)=q_r*256+q_g*16+q_b;
hist2(q_temp1(i,j)+1)= hist2(q_temp1(i,j)+1)+m_wei(i,j);
end
end
%------------------------------------------------------------------------------------------------------------------------
hist2=hist2*C;
figure(2);%創建一個窗口
subplot(1,2,1);%放置圖片位置
plot(hist2);
hold on;
w=zeros(1,a*b);
for i=1:a*b
if(hist2(i)~=0)
w(i)=sqrt(hist1(i)/hist2(i));
else
w(i)=0;
end
end
%------------------------------------------------------------------------------------------------------------------------
%變量初始化
sum_w=0;
xw=[0,0];
for i=1:a;
for j=1:b
sum_w=sum_w+w(uint32(q_temp1(i,j))+1);
xw=xw+w(uint32(q_temp1(i,j))+1)*[i-y(1)-0.5,j-y(2)-0.5];
end
end
%------------------------------------------------------------------------------------------------------------------------
Y=xw/sum_w;
%中心點位置更新
rect(1)=rect(1)+Y(2);
rect(2)=rect(2)+Y(1);
end
%------------------------------------------------------------------------------------------------------------------------
%跟蹤矩陣軌跡
tic_x=[tic_x;rect(1)+rect(3)/2];
tic_y=[tic_y;rect(2)+rect(4)/2];
v1=rect(1);
v2=rect(2);
v3=rect(3);
v4=rect(4);
%顯示跟蹤結果
%------------------------------------------------------------------------------------------------------------------------
subplot(1,2,2);%設定圖片位置
imshow(uint8(Im));%顯示圖像
title('目標跟蹤結果與運動軌跡');%命名圖像
hold on;
plot([v1,v1+v3],[v2,v2],[v1,v1],[v2,v2+v4],[v1,v1+v3],[v2+v4,v2+v4],[v1+v3,v1+v3],[v2,v2+v4],'LineWidth',2,'Color','r');
plot(tic_x,tic_y,'LineWidth',2,'Color','b');
end
%------------------------------------------------------------------------------------------------------------------------
