matlab對kcf跟蹤結果的分析
阿新 • • 發佈:2018-12-17
用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 操作的效果一樣,所以就不再加入說明。 第一章:一些函式的介紹
這裡還值得一提的是 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 資料夾內。
這樣就能夠節省很多的人力。當檔案比較多的情況下,人工去建立的話,還容易出錯
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繪製跟蹤效果對比圖並儲存
C:顯示跟蹤結果效果圖
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 操作的效果一樣,所以就不再加入說明。 第一章:一些函式的介紹
- 介紹三個常用函式
- typefunction
- ver
- docfunction /help function
- 影象的讀取
- 影象的顯示
- imshow(I)
- imtool(I)
- image(I)
這裡還值得一提的是 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 資料夾內。
這樣就能夠節省很多的人力。當檔案比較多的情況下,人工去建立的話,還容易出錯