1. 程式人生 > >matlab對kcf跟蹤結果的分析

matlab對kcf跟蹤結果的分析

用matlab對kcf跟蹤結果的分析 1.MATLAB程式碼:
A:讀取跟蹤過程中各種置信度,apce psr peakvalue 並分析 其對判別跟蹤失敗檢測的效果 clear all;clc; target='Woman';%%CarScale, Woman text=importdata(['trackerlog_',target,'.txt']); frame=text(:,1); fps=text(:,2); apce=text(:,3); psr=text(:,4); peakvalue=text(:,5); roi=text(:,6:9); %%pos=text(:,5:6); flag=text(:,10:13); groundtruth=importdata([target,'/groundtruth_rect.txt']);
n=size(peakvalue,1); for i=1:1:n if i<6 meanpeakvalue(i)=peakvalue(i); meanapce(i)=apce(i); meanpsr(i)=psr(i); else meanpeakvalue(i)=0.70*(peakvalue(i-5)+peakvalue(i-4)+peakvalue(i-3)+peakvalue(i-2)+peakvalue(i-1))/5; meanapce(i)=0.40*(apce(i-5)+apce(i-4)+apce(i-3)+apce(i-2)+apce(i-1))/5; meanpsr(i)=0.40*(psr(i-5)+psr(i-4)+psr(i-3)+psr(i-2)+psr(i-1))/5; end if(flag(i,4)==3) fail_flag(i)=1; else fail_flag(i)=0; end end meanpeakvalue = meanpeakvalue(:); meanapce = meanapce(:); figure('Name','result') subplot(2,3,1); plot(frame,fps); title('fps') subplot(2,3,2); plot(frame,peakvalue); hold on plot(frame,meanpeakvalue); plot(frame,fail_flag); title('peak') subplot(2,3,4); plot(frame,apce); hold on plot(frame,meanapce); plot(frame,fail_flag*100); title('apce') subplot(2,3,5); plot(frame,psr); hold on plot(frame,meanpsr); plot(frame,fail_flag*500000); title('psr') subplot(2,3,[3 6]); precision_plot(roi,groundtruth);

B:讀取跟蹤結果輸出,根據跟蹤的roi(跟蹤的框)以及groundtruth繪製跟蹤效果對比圖並儲存
figure('Name','tracking Boxs') for i=1:n I=imread([target,'\img\',num2str(i),'.jpg']);%%change dataset folder imshow(I);%%show frame hold on; axis off; %%change folder of datasets %%waitkey(1); rectangle('Position',groundtruth(i,:),'Curvature',[0 0],'EdgeColor','b','LineWidth',2);%%plot groundtruth rectangle('Position',roi(i,:),'Curvature',[0 0],'EdgeColor','r','LineWidth',2);%%plot roi of tracking saveas(gca,[target,'\result\',num2str(i),'.jpg']);%%change dataset folder %%pause(0.000001); %%groundtruth_x(i),groundtruth_y(i),groundtruth_w(i),groundtruth_h(i)wen end
C:顯示跟蹤結果效果圖
i=1; figure('Name',['tracking Boxs of ',target]) while(1) I=imread([target,'\result\',num2str(i),'.jpg']);%%change dataset folder imshow(I); hold on; axis off; pause(0.01); i=i+1; end 精度計算函式 function precisions = precision_plot(positions, ground_truth) %PRECISION_PLOT % Calculates precision for a series of distance thresholds (percentage of % frames where the distance to the ground truth is within the threshold). % Accepts positions and ground truth as Nx2 matrices (for N frames), and % a title string.
max_threshold = 50; %used for graphs in the paper precisions = zeros(max_threshold, 1); if size(positions,1) ~= size(ground_truth,1), % fprintf('%12s - Number of ground truth frames does not match number of tracked frames.\n', title) %just ignore any extra frames, in either results or ground truth n = min(size(positions,1), size(ground_truth,1)); positions(n+1:end,:) = []; ground_truth(n+1:end,:) = []; end %calculate distances to ground truth over all frames distances = sqrt((positions(:,1) - ground_truth(:,1)).^2 + ... (positions(:,2) - ground_truth(:,2)).^2); distances(isnan(distances)) = []; %compute precisions for p = 1:max_threshold, precisions(p) = nnz(distances <= p) / numel(distances); end %plot the precisions %%figure('Name',['Precisions of ',title]) plot(precisions, 'k-', 'LineWidth',2) xlabel('Threshold'), ylabel('Precision') title('Precisions') end 2.結果:


分析了跟蹤過程中的執行幀數fps變化,以及peakvalue/apce/psr的變化(藍色線),以及對應的平均值注:乘上一個懲罰因子:meanpeakvalue:0.7/meanapce:0.4/meanpsr:0.4(紅色線),用來做判別,還有跟蹤失敗的標誌位:fail_flag,途中所示為frame121時出現一次跟蹤失敗(黃色線)。 最後還有計算跟蹤準確度的分析圖。



116-121幀影象,紅色框為跟蹤框,藍色框為groundtruth:
3.乾貨: matlab rectangle畫矩形函式: rectangle('Position',[x,y,w,h]) 從點(x,y)開始繪製一個寬w高h的矩形,對座標軸資料單元指定值。 注意,按指定的比例顯示矩形,需要設定座標軸資料寬高比來使得x和y軸有等長的單位。你可以用命令axis equal 或者daspect([1,1,1])。
rectangle繪製一個矩形,其預設位置是[0,0,1,1] ,預設曲率是 [0,0](也就是說,沒有曲率)。
rectangle(...,'Curvature',[x,y])指定矩陣邊的曲率,可以使它從矩形到橢圓不同變化,水平曲率x為矩形寬度的分數,是沿著矩形的頂部和底部的邊進行彎曲。豎直曲率y為矩形高度的分數,是沿著矩形的左面和右面的邊進行彎曲。x和y取值範圍是從0(無曲率)到1(最大麴率)。值[0,0]繪製一個成直角的矩形,值[x,y]繪製一個橢圓。如果僅僅指定曲率的一個值,那麼在水平曲率和豎直曲率都有相同的值。
h = rectangle(...)返回建立矩形物件的控制代碼。 備註:矩形物件是2維的,僅僅能在[0 90](i.e., view(2))範圍內被繪製,矩形物件是座標的子物件,被定義在座標軸資料內。 例:rectangle('position',[1,1,5,5],'edgecolor','b'); 還可以設定其他引數 'position',[1,1,5,5]表示從(1,1)點開始高為5,寬為5; 'curvature',[1,1]表示x,y方向上的曲率都為1,即是圓弧; 'edgecolor','r'表示邊框顏色是紅色; 'facecolor','g'表示面內填充顏色為綠色。 注意在使用時需要注意先將圖片顯示出來,然後再對影象中你所需要的區域標記。 MATLAB 影象處理,等函式 包括imread,批量讀取影象: 影象處理,主要是對已有的影象進行處理,然後再儲存起來,並不是無中生有,創造出一幅影象。因此,影象的讀取和儲存顯得比較重要。     這裡建立 test.m 檔案放入已經建立的 Matlab 資料夾裡,之後的操作都在這裡進行。由於在檔案中操作和 Command Window 操作的效果一樣,所以就不再加入說明。 第一章:一些函式的介紹
  1. 介紹三個常用函式
clc;clear;closeall; 這三個函式不分家,在一個檔案的開頭經常會看到。那麼他們的作用是什麼呢? clc 的作用就是清螢幕,即 Command Window 裡的內容會被清除掉,但是他的值仍然存在軟體裡。  clear 是刪除所有的變數。比如在前面定義了  A=5; clear 函式之後, A 就被清除了。後面想要用,就需要重新定義。 close all 是將所有開啟的圖片關掉。在函式的開頭寫上這樣的函式,就保證不會受到之前變數等的影響。 當然,也可以單獨使用。這樣就能單獨完成任務。這時最好在 Command Window 裡操作。
  1. typefunction
Matlab  裡面有很多內建的函式,當我們想檢視函式內容的時候,就可以用 type function 來實現。比如想要檢視 mean 函式, Matlab 到底是怎麼實現,直接 type mean 就可以看到。如果是檢視別人的寫的程式,在 mean 上點選右鍵, open mean 就可以了。
  1. ver
檢視當前 Matlab 版本的函式。一般用不到。
  1. docfunction /help function
可以用來檢視函式的用途,語法,用途。可以說 Matlab help 本身就是一本百科全書。很多內容都可以找到 第二章讀取影象
  1. 影象的讀取    
I=imread(‘pout.tif’); imread 是讀取影象的函式。 pout.tif matlab 內建的影象,不管在什麼程式內都可以直接讀取。那麼,如果我們要讀取其他位置的函式怎麼做呢?這裡我們來說幾種常見的情形。 注: Matlab 資料夾內有 test.m,1.jpg,image 資料夾,同時 image 資料夾內有 2.jpg (1) 讀取 1.jpg I=imread(‘1.jpg’); (2) 讀取 2.jpg I=imread(‘image\2.jpg’);% 相對路徑的讀取 (3) 讀取 D:\1023\25\1.jpg I=imread(‘D:\1023\25\1.jpg’);% 絕對路徑的讀取 (4) 讀取 D:\1023\25 20 個影象。 for i=1:20  I=imread([‘D:\1023\25\’,num2str(i),’.jpg’]); end 注:第四種情況需要注意的是,裡面添加了 [ ], 來保證這是一個整句。 num2str(i) 是將 i 由數字轉換成字元形式。這樣就能實現迴圈讀取。一般來說,會將影象跟 .m 檔案放在一起。當影象較多時,影象放入 image 文件中,讀取方式將( 2 )和( 4 )結合起來。利用相對路徑來讀取,這樣當程式和影象同時放在其他電腦上時,不會因為路徑而產生問題。
  1. 影象的顯示
  1. imshow(I)
  2. imtool(I)
  3. image(I)
http://blog.sina.com.cn/s/blog_49ea41a20101fndv.html 關於這三個函式用法的區別,就在上面這個文章內。主要的意思是: imshow 顯示按照原來的比例,而 image 會改變原來影象的比例。 i mtool ,很少用到。那麼用到的時候就再研究吧。一般用 imshow 就足夠了。 那麼什麼時候用 figure 呢?當程式中只顯示一幅影象時,直接 imshow 就可以。然後當影象多的時候,就需要用到 figuure;imshow(I1);figure;imshow(I2);figure;imshow(I3) 如果想要在一張圖片內,顯示好幾個圖怎麼辦呢? title 顯示在影象的上方,起到提示的作用。 I=imread('pout.tif'); subplot(221);imshow(I);title('1'); subplot(222);imshow(I);title('2'); subplot(223);imshow(I);title('3'); subplot(224);imshow(I);title('4'); 其中前面的 22 代表是 2*2 的分佈,也可以是 2*3,3*4 等。按照行來排列,從第一行開始分別為 1 2 3 4 。如圖所示。



這裡還值得一提的是 figure; 如果沒有要求的話,直接用 figure 。此時希望圖 2 來顯示某個影象,那麼 figure(2) ,就指定來顯示某個影象。當然了,如果僅有一個影象顯示這樣做,那麼可能會被覆蓋掉,因此,一個程式中的用法一定要統一,並且保證後面的,不要把前面的覆蓋掉。
  3 、檢視和分析結果:
  workspace 中會有 I 291*240 uint8  74 224 , 就這說明了 I 是二維影象 , 大小是 291*240,uint8 型的。最小值為 74, 最大值是 224. 這裡指的是灰度值。
  用函式 whos 就能顯示 I 的相關資訊:






第三章影象的儲存
1 、影象的儲存
imwrite(I,’pout1.jpg’);
同樣, imwrite 也有相對路徑和絕對路徑的儲存,以及連續的儲存方法。同 imread
imwrite(I,’pout1.jpg’);
imwrite(I,’image\pout1.jpg’);
imwrite(I,’D:\Matlab\image\pout1.jpg’);
imwrite(I,[’image\’,num2str(i),’.jpg’]) %i 是變數,需要定義
這裡的 .jpg 可以改成 .bmp 等,想要的格式。
需要註明的是,儲存所選擇的資料夾,需要已經建立好。那麼怎麼去自動建立資料夾呢?
需要用 mkdir 函式。
mkdir(‘D:\image\1’)% 絕對路徑的建立。
mkdir(‘image\1’);% 絕對路徑資料夾的建立。建立之後,在 Matlab 資料夾內。
這樣就能夠節省很多的人力。當檔案比較多的情況下,人工去建立的話,還容易出錯