2017-12-28 22 views
0

Matlab에서 2D AVI 애니메이션을 만들었습니다. 녹색 점 하나와 파란색 점 하나가 선을 따라 다른 방향으로 움직입니다. 하지만 이제는이 AVI 애니메이션 비디오에 다른 점을 추가하고 싶습니다.이 점은 다른 두 점과 함께 무작위로 이동하며, 여기에 코드를 작성해야합니까? 첨부 된 코드는 내 코드입니다. 도와 주셔서 대단히 감사합니다!Matlab에서 2D avi 애니메이션 생성

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

어, "임의"의미는 무엇입니까? 무작위로 시작하여 무작위로 이동합니까? –

+0

@HunterJiang 네, 맞습니다. 같은 비디오 이미지에서 라인 패스가 아닌 다른 두 개와 함께 움직이기는하지만 무작위로 움직입니다. –

답변

0

이 코드는 도움이 될 수 있으며 새 코드는 "****"줄 사이에 있습니다.

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

10 분 안에 수업을 할 것이므로 "요점을 확인하십시오"부분을주의 깊게 확인할 수는 없지만 코드는 반드시 확인해야합니다. 그들 (무작위 포인트뿐만 아니라 포인트 & 포인트 1)을 가져라. –

+0

문제 없습니다. 내 코드 plz에 질문이 있으면 의견을 남기고 나는 그것을 확인합니다 a.s.a.p (내 수업 후). @ 징 황 –