2017-12-28 51 views
0

Ich habe eine 2D-AVI-Animation in Matlab, gibt es einen grünen Punkt und einen blauen Punkt entlang einer Linie in verschiedene Richtungen bewegt. Aber jetzt möchte ich in diesen AVI Animationsfilm einen weiteren Punkt einfügen, der sich zusammen mit den anderen beiden Punkten auf einem zufälligen Pfad bewegt, was soll ich jetzt hier schreiben? unten angehängt sind meine Codes. Vielen Dank für Ihre Hilfe!2D-Avi-Animation-Generation in Matlab

clc; clear; 
%time steps 

N=20; 
%start position of one moving object 

x_pos=10; 

y_pos=10; 

x1_pos=100; 

y1_pos=100; 
%speed of the moving object 

speed_x=3; 

speed_y=2; 

speed_x1=-4; 

speed_y1=-2; 
%ini white image with size 100 x 150 

image=ones(100,150,3); 
%initialization of videowriter 

outputVideo = VideoWriter('test.avi'); 

outputVideo.FrameRate = 5; %set frame rate of the image 

open(outputVideo) 
%drawing a moving object on an image plane and creating the video 

size=2; 

for i=1:N 
%update states of moving object 

x_pos=x_pos+speed_x; 

y_pos=y_pos+speed_y; 

x1_pos=x1_pos+speed_x1; 

y1_pos=y1_pos+speed_y1; 
%draw the measurement on the image using a green point with specific size size=4; 

image((x_pos-size/2):(x_pos+size/2),(y_pos-size/2):(y_pos+size/2),1)=0; 

image((x_pos-size/2):(x_pos+size/2),(y_pos-size/2):(y_pos+size/2),3)=0; 

image((x1_pos-size/2):(x1_pos+size/2),(y1_pos-size/2):(y1_pos+size/2),2)=0; 

image((x1_pos-size/2):(y1_pos+size/2),(y1_pos-size/2):(y1_pos+size/2),1)=0; 
%give the image to the video writer object 
writeVideo(outputVideo,image) 

end 

close(outputVideo); 
+0

Äh, was bedeutet "zufällig"? Beginnt es zufällig und bewegt sich zufällig? –

+0

@HunterJiang Ja, ist es. Im gleichen Videobild, zusammen mit den anderen beiden, nicht in einem Linienpfad bewegen, sondern bewegen sich zufällig –

Antwort

0

Diese Codes können helfen, und neue Codes liegen zwischen "****" -Leitungen.

clc; clear; 
%time steps 

N=20; 
%start position of one moving object 

x_pos=10; 

y_pos=10; 

x1_pos=100; 

y1_pos=100; 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%set the start point 
rdx_pos=50+floor((rand()-0.5)*20); 

rdy_pos=50+floor((rand()-0.5)*20); 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

%speed of the moving object 

speed_x=3; 

speed_y=2; 

speed_x1=-4; 

speed_y1=-2; 

%ini white image with size 100 x 150 

image=ones(100,150,3); 
%initialization of videowriter 

outputVideo = VideoWriter('test.avi'); 

outputVideo.FrameRate = 5; %set frame rate of the image 

open(outputVideo) 
%drawing a moving object on an image plane and creating the video 

size=2; 

for i=1:N 
%update states of moving object 

x_pos=x_pos+speed_x; 

y_pos=y_pos+speed_y; 

x1_pos=x1_pos+speed_x1; 

y1_pos=y1_pos+speed_y1; 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%set the new point 

rdx_pos=rdx_pos+floor((rand()-0.5)*5); 

rdy_pos=rdy_pos+floor((rand()-0.5)*5); 

%check the point 
if (rdx_pos >150) rdx_pos=150; end 
if (rdx_pos <1) rdx_pos=1; end 
if (rdy_pos >100) rdx_pos=100; end 
if (rdy_pos <1) rdx_pos=1; end 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

%draw the measurement on the image using a green point with specific size size=4; 

image((x_pos-size/2):(x_pos+size/2),(y_pos-size/2):(y_pos+size/2),1)=0; 

image((x_pos-size/2):(x_pos+size/2),(y_pos-size/2):(y_pos+size/2),3)=0; 

image((x1_pos-size/2):(x1_pos+size/2),(y1_pos-size/2):(y1_pos+size/2),2)=0; 

image((x1_pos-size/2):(y1_pos+size/2),(y1_pos-size/2):(y1_pos+size/2),1)=0; 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%output image 
image((rdx_pos-size/2):(rdx_pos+size/2),(rdy_pos-size/2):(rdy_pos+size/2),2)=0; 

image((rdx_pos-size/2):(rdx_pos+size/2),(rdy_pos-size/2):(rdy_pos+size/2),1)=0; 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

%give the image to the video writer object 
writeVideo(outputVideo,image) 

end 

close(outputVideo); 
+0

Ich werde Klasse in zehn Minuten haben, so kann ich nicht überprüfen, den Teil "überprüfen Sie den Punkt" sorgfältig, aber Ihr Code muss habe sie (nicht nur den Zufallspunkt, sondern auch Punkt & Punkt1). –

+0

kein Problem. Wenn es irgendeine Frage mit meinem Code PLZ geben Sie einen Kommentar und ich werde es überprüfen a.s.a.p (Nach meiner Klasse). @Jing Huang –