1. 程式人生 > >matlab視訊儲存以及速度向量箭頭

matlab視訊儲存以及速度向量箭頭

最近用matlab畫圖,主要涉及的matlab函式是:

VideoWriter:儲存視訊的新函式,取代了之前的movie2avi。需要結合writeVideo函式。

quiver:速度向量繪圖函式。


做了一些動畫效果的視訊,擷取效果如下:



主要功能:1、顯示目標在整個過程中的運動軌跡如圖a所示

2、顯示目標的方向變化情況,右轉為正,左轉為負,如圖b所示

3、顯示之前軌跡和當前方向的比較,如圖c所示

4、顯示目標之間的方向變化的前後關係以及網路,如圖d所示。


程式碼如下:

clc
clear

% M(i)=getframe;
% i=i+1;
% moviename=  strcat('prediction.avi');
%movie2avi(M,moviename,'fps',1);% title('Toggle Switch Dynamics (Simulation Versus Real Data)','FontSize',20);

%1360 - 1960  - 2560 

load('posi.mat');
load('index2.mat');

% pos = posi(1360:1960, :, :);
% index = index(1360:1960, :);
startIndex = 1360;
endIndex = 2560;
showLen = 20;  %(a)(b)圖的軌跡長度
direcLen = 1;  %(c)圖的方向箭頭,取得後面第幾個點,1為下一個點
tailLen = 10;  %(c)圖的尾部長度
types ='rgbcmkrgbc'; %{'r'; 'g.'; 'b.'; 'c.'; 'm.'; 'y.'; 'k.'; 'r.'; 'g.';'b.'};
figure;
f_index = 1;
wobj = VideoWriter('newmove.avi');
wobj.FrameRate = 10;
open(wobj);
xmax= max(max(max(posi(startIndex:endIndex,1, :))));
xmin= min(min(min(posi(startIndex:endIndex,1, :))));
ymax= max(max(max(posi(startIndex:endIndex,2, :))));
ymin= min(min(min(posi(startIndex:endIndex,2, :))));
spos = [];
sindex = [];
for i = startIndex:endIndex-1
    posLen = size(spos, 1); 
    if posLen<showLen
        spos(posLen + 1, :, :) = posi(i,:,:);
        sindex(posLen + 1) = index2(i);
        if posLen < showLen-1
            continue
        end
    else
        spos = spos(2:end,:,:);
        spos(posLen, :, :) = posi(i,:,:);
        sindex = sindex(2:end);
        sindex(posLen) = index2(i);
    end
    %執行到此處 posLen一定為showLen
    subplot(2,2,1)
    cla
    title('(a)', 'FontSize',12);
    posLen = size(spos, 1);
    x1 = posi(i, 1, :);
    x1 = reshape(x1, 1, 10);
    y1 = posi(i, 2, :);
    y1 = reshape(y1, 1, 10);
    plot(x1, y1, 'o','MarkerSize',4);
    hold on
    for j = 1:10
        xtmp = spos(:,1,j);
        x2 = reshape(xtmp, 1, posLen);
        ytmp = spos(:,2,j);
        y2 = reshape(ytmp, 1, posLen);
        plot(x2, y2, types(j));
        hold on
    end
    axis([xmin-5,xmax+5,ymin-5,ymax+5])
    
    subplot(2,2,2)
    cla
    title('(b)', 'FontSize',12);
    x3 = i-19:i;
    y3 = sindex;
    plot(x3, y3);
    hold on;
    grid on;
    axis([i-20,i+20,-11,11])

    f3 = subplot(2,2,3);
    cla
    title('(c)', 'FontSize',12);
    posLen = size(spos, 1);
    x4 = posi(i, 1, :);
    x4 = reshape(x4, 1, 10);
    y4 = posi(i, 2, :);
    y4 = reshape(y4, 1, 10);
    plot(x4, y4, 'o','MarkerSize',2);
    x4_1 = posi(i + direcLen, 1, :);
    x4_1 = reshape(x4_1, 1, 10);
    y4_1 = posi(i + direcLen, 2, :);
    y4_1 = reshape(y4_1, 1, 10);
    for j = 1:10
        xtmp = spos(:,1,j);
        x5 = reshape(xtmp, 1, posLen);
        ytmp = spos(:,2,j);
        y5 = reshape(ytmp, 1, posLen);
        plot(x5(:, showLen - tailLen : end), y5(:, showLen - tailLen : end), types(j));
        hold on;
        quiver(x4(j), y4(j), x4_1(j) - x4(j), y4_1(j) - y4(j), 4, types(j), 'maxheadsize',2);
        hold on;
    end
%    quiver(x4, y4, x4_1 - x4, y4_1 - y4,'b');
%     axis([xmin-5,xmax+5,ymin-5,ymax+5])
    
    subplot(2,2,4)
    cla
    title('(d)', 'FontSize',12);
    y6 = [1.0, 0.8, 0.8, 0.6, 0.6, 0.6, 0.6, 0.4, 0.4, 0.2];
    x6 = [0.5, 0.3, 0.7, 0.2, 0.4, 0.6, 0.8, 0.3, 0.7, 0.5];
    colors = zeros(10, 3);
    num = (10 - index2(i))/2;
    for j = 1:num
        colors(j,:)=[1,1,1];
    end   
    for j = 1:10
        plot(x6(j), y6(j), 'o','MarkerSize',8, 'markerfacecolor', colors(j,:));
        hold on;
    end
    axis([0,1,0,1.2])    
    set(gcf,'papersize',[800,600]);
    frame=getframe(gcf);
    writeVideo(wobj, frame);
end
close(wobj)