資源簡介
機器視覺,視頻圖像跟蹤方向,很適合初學者,每條代碼都做了詳細的注釋
代碼片段和文件信息
function?[]?=?select()??
close?all;??
clear?all;??
%%%%%%%%%%%%%%%%%%根據一幅目標全可見的圖像圈定跟蹤目標%%%%%%%%%%%%%%%%%%%%%%%??
I=imread(‘E:\Matlab\bin\tracking_Matlabforum\1.bmp‘);??
figure(1);??
imshow(I);??
??
??
[temprect]=imcrop(I);??????%rect表示,[xywidthheight];xy為截圖時的初選點,width=bheight=a
[abc]=size(temp);?????????%temp是一個三維矩陣;ab表示像素點位置,c表示該點的rgb值(分為三層,分別為紅綠藍)
????????????????????????????%像素可以理解為一個長度單位
????????????????????????????
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%計算目標圖像的權值矩陣%%%%%%%%%%%%%%%%%%%%%%%??
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(ab);???????????%權值矩陣??
h=y(1)^2+y(2)^2?;???????????????%帶寬??????MeanShift目標跟蹤算法的帶寬選取策略;rect(4)/2,是傳統三種選取方式中,以半長邊為帶寬最優
??
for?i=1:a??
????for?j=1:b??
????????dist=(i-y(1))^2+(j-y(2))^2;??
????????m_wei(ij)=1-dist/h;?%epanechnikov?profile??
????end??
end??
C=1/sum(sum(m_wei));%歸一化系數??
??
??
%計算目標權值直方圖qu??
%hist1=C*wei_hist(tempm_weiab);%target?model??
hist1=zeros(14096);??
for?i=1:a??
????for?j=1:b??
????????%rgb顏色空間量化為16*16*16?bins??
????????q_r=fix(double(temp(ij1))/16);??%fix為趨近0取整函數??
????????q_g=fix(double(temp(ij2))/16);??
????????q_b=fix(double(temp(ij3))/16);??
????????q_temp=q_r*256+q_g*16+q_b;????????????%設置每個像素點紅色、綠色、藍色分量所占比重??
????????hist1(q_temp+1)=?hist1(q_temp+1)+m_wei(ij);????%計算直方圖統計中每個像素點占的權重??
????end??
end??
hist1=hist1*C;??
rect(3)=ceil(rect(3));??
rect(4)=ceil(rect(4));??
??
??
??
??
%%%%%%%%%%%%%%%%%%%%%%%%%讀取序列圖像??
myfile=dir(‘E:\Matlab\bin\tracking_Matlabforum\*.bmp‘);??
lengthfile=length(myfile);??
??
for?l=1:lengthfile??
????image_name?=?strcat(num2str(l)‘.bmp‘);
????Im=imread(image_name);??
????num=0;??
????Y=[22];
???????????
????%%%%%%%mean?shift迭代??
????while((Y(1)^2+Y(2)^2>0.5)&num<20)???%迭代條件??%%%%這一句表示當目標中心偏移的距離平方小于等于0.5?或?迭代次數超過20次,退出本次迭代。
????????num=num+1;??
????????%pause;
????????temp1=imcrop(Imrect);??
????????[ab
- 上一篇:模電課程設計小型音頻功放
- 下一篇:信號互相關仿真,計算時延差
評論
共有 條評論