找回密码
 注册
Simdroid-非首页
查看: 138|回复: 6

[求助]如何在matlab中输出一个渐变圆的avi图像,已经附了一个渐变方块图像的程序

[复制链接]
发表于 2008-6-4 03:48:31 | 显示全部楼层 |阅读模式 来自 美国
%   MakingShrinkingSquareMovie.m
%
%  Discussion:
%
%    This is a simple program to create an
%    Audio Video Interleaved (AVI) movie that can be played
%    independently of Matlab. We capture NUMFRAMES frames for shrinking/Enlarging square to/from the center  
%    for the ADHD research
%    Before running this program close all figures and don't interfere
%    with the generated figures until the recording process is done.
%

close all;
clear all;
fprintf ( 1, '\n' );
fprintf ( 1, 'MAKEING MOVIES FRAMES FOR AVI FILE \n' );
fprintf ( 1, '  Create an AVI animation of simple graphics,\n' );
fprintf ( 1, '  generating one frame at a time.\n' );

%
%  Control resolution of and graph default
%
  pixel_x=360;
  pixel_y=360;
  A= zeros([pixel_y pixel_x]);
  max_square_bandwidth = 20;
  min_square_bandwidth = 8;
  min_square_side = 20;  % center target size equal to side x side;
  A(max_square_bandwidth : pixel_y - max_square_bandwidth , max_square_bandwidth : pixel_x-max_square_bandwidth)=255;
  A((pixel_y-min_square_side)/2:(pixel_y+min_square_side)/2, (pixel_x-min_square_side)/2:(pixel_x+min_square_side)/2) = 0;
  imshow(A)
  
%
%  Set the total number of frames that we will generate.
%
  numframes = 180;
  sliding_axes = sqrt(((pixel_y-min_square_side)/2 - max_square_bandwidth)^2 + ((pixel_x-min_square_side)/2-max_square_bandwidth)^2);
  hp = sliding_axes / numframes;
  
%
%  Control how fast final movie will play; total time
%  of movie in seconds will be about (numframes)/(num_frames_per_second)
%  
  num_frames_per_second = 20;
  dur = numframes / num_frames_per_second;
  
  fprintf ( 1, '\n' );
  fprintf ( 1, '  This movie will contain %d frames.\n', numframes );
  fprintf ( 1, '  The number of frames per second will be %f\n', num_frames_per_second );
  fprintf ( 1, '  so the movie should take %f seconds to play.\n', dur );
%
%
%  The AVIFILE function creates a new Audio Video Interleaved (AVI) file.
%  We specify its name, and we also set the value of "FPS", which is the
%  rate at which we will display frames (individual snapshots) per second.
%

aviobj = avifile ( 'flyingsquare.avi', 'fps', num_frames_per_second );

%
%  Control resolution of square
%
  num_nodes = 100;
  i = 1 : num_nodes + 1;
  

%
%  Control speed of contraction/
%
  %h1 = i - 1 ;   % for equal speed contraction
  h2 = exp((i-1) / (num_nodes/2));
  h3y = h2/sum(h2) * ((pixel_y-min_square_side)/2 - max_square_bandwidth);
  h3x = h2/sum(h2) * ((pixel_x-min_square_side)/2 - max_square_bandwidth);
  %%%  h = 2 * pi / num_nodes;
  
%
%  Control square band width
%
  b2 = exp((i-1) / (num_nodes/2));
  band_range = max_square_bandwidth -  min_square_bandwidth;
  b3 = band_range * b2/sum(b2);
  white_band_frames = 20;

%
%  Make plots of squares.  Each new plot is a frame which
%  can be added to the AVI movie using the ADDFRAME function.
%
j=0;k=0;l=0; o=0;
for z =  1 : numframes+1
    j=j+1;
    if j > num_nodes+1
    else
    band = max_square_bandwidth - sum(b3(1:j));
    p1 = [ sum(h3y(1:j)) + max_square_bandwidth sum(h3x(1:j)) + max_square_bandwidth  ];
    p2 = [ pixel_y - (sum(h3y(1:j)) + max_square_bandwidth) sum(h3x(1:j)) + max_square_bandwidth  ];
    p3 = [ sum(h3y(1:j)) + max_square_bandwidth pixel_x - (sum(h3x(1:j)) + max_square_bandwidth) ];
    A( p1(1) - band : p2(1) + band , p1(2) - band : p3(2) + band ) = 0;
    A(p1(1):p2(1), p1(2):p3(2)) = 255;
    end
    A(max_square_bandwidth : pixel_y - max_square_bandwidth , max_square_bandwidth : pixel_x-max_square_bandwidth)=255;
    if j < white_band_frames
    else
        k=k+1;
        if k > num_nodes+1
        else
        band2 = max_square_bandwidth - sum(b3(1:k));
        p11 = [ sum(h3y(1:k)) + max_square_bandwidth sum(h3x(1:k)) + max_square_bandwidth  ];
        p12 = [ pixel_y - (sum(h3y(1:k)) + max_square_bandwidth) sum(h3x(1:k)) + max_square_bandwidth  ];
        p13 = [ sum(h3y(1:k)) + max_square_bandwidth pixel_x - (sum(h3x(1:k)) + max_square_bandwidth) ];
        end
        if k < white_band_frames
        else
            l=l+1;
            if l > num_nodes+1
            else
            band3 = max_square_bandwidth - sum(b3(1:l));
            p21 = [ sum(h3y(1:l)) + max_square_bandwidth sum(h3x(1:l)) + max_square_bandwidth  ];
            p22 = [ pixel_y - (sum(h3y(1:l)) + max_square_bandwidth) sum(h3x(1:l)) + max_square_bandwidth  ];
            p23 = [ sum(h3y(1:l)) + max_square_bandwidth pixel_x - (sum(h3x(1:l)) + max_square_bandwidth) ];
            end
            if l < white_band_frames
            else
                o=o+1;
                if o > num_nodes+1
                else
                band4 = max_square_bandwidth - sum(b3(1:o));
                p31 = [ sum(h3y(1:o)) + max_square_bandwidth sum(h3x(1:o)) + max_square_bandwidth  ];
                p32 = [ pixel_y - (sum(h3y(1:o)) + max_square_bandwidth) sum(h3x(1:o)) + max_square_bandwidth  ];
                p33 = [ sum(h3y(1:o)) + max_square_bandwidth pixel_x - (sum(h3x(1:o)) + max_square_bandwidth) ];
                end
                A( p31(1) - band4 : p32(1) + band4 , p31(2) - band4 : p33(2) + band4 ) = 0;
                A( p31(1) : p32(1), p31(2) : p33(2)) = 255;
            end
               
            A( p21(1) - band3 : p22(1) + band3 , p21(2) - band3 : p23(2) + band3 ) = 0;
            A( p21(1) : p22(1), p21(2) : p23(2)) = 255;
          end
       A( p11(1) - band2 : p12(1) + band2 , p11(2) - band2 : p13(2) + band2 ) = 0;
       A( p11(1):p12(1), p11(2):p13(2)) = 255;                     

    end
   
     A( p1(1) - band : p2(1) + band , p1(2) - band : p3(2) + band ) = 0;
     A(p1(1):p2(1), p1(2):p3(2)) = 255;
     A((pixel_y-min_square_side)/2:(pixel_y+min_square_side)/2, (pixel_x-min_square_side)/2:(pixel_x+min_square_side)/2) = 0;
     imshow(A);
     axis tight
     frame = getframe ( gca );
     aviobj = addframe ( aviobj, frame );
end

%
%  Tell MATLAB we have completed the movie.
%
  aviobj = close ( aviobj );
  
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Normal end of execution.\n' );
  fprintf ( 1, '  The movie file "flyingsquare.avi" has been created.\n' );


-----------------------------------------------------------------------------------------------------------
上面这个是一个渐变方块的图像,如何将其修改为渐变圆的。。大家帮忙看看。。

非常感谢!!

[ 本帖最后由 quyinhong 于 2008-6-6 12:05 编辑 ]
 楼主| 发表于 2008-6-5 00:51:57 | 显示全部楼层 来自 美国
Simdroid开发平台
顶一下,大家帮我看看,多谢拉
回复 不支持

使用道具 举报

 楼主| 发表于 2008-6-6 12:05:57 | 显示全部楼层 来自 美国

或者给我点提示吧:)顶一下。。

或者给我点提示吧:)顶一下。。
回复 不支持

使用道具 举报

发表于 2008-6-7 16:52:09 | 显示全部楼层 来自 上海浦东新区
搂住厉害,这部分属于MATLAB的什么方面呀!
让我开眼界了,以前一直以为只能计算。
回复 不支持

使用道具 举报

 楼主| 发表于 2008-6-14 03:55:22 | 显示全部楼层 来自 美国
UP一下- -!
回复 不支持

使用道具 举报

 楼主| 发表于 2008-6-17 03:30:39 | 显示全部楼层 来自 美国
upupupupupupupup
回复 不支持

使用道具 举报

发表于 2008-6-17 03:54:13 | 显示全部楼层 来自 美国
这个用Matlab做movie的功能,原来用过,好像要下载一个小插件。然后就是先画图,然后去把图变成avi。
刚才查了一下,好像不需要插件。就是用getframe和addframe函数。
楼主是做什么? 我同学原来做大气模型的时候,我帮他做了一些movie。 不过我不太喜欢Matlab,现在手头上也没有。

你的难处可能是画图吧。
回复 不支持

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Simapps系列直播

Archiver|小黑屋|联系我们|仿真互动网 ( 京ICP备15048925号-7 )

GMT+8, 2024-9-21 04:32 , Processed in 0.039185 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表