Steve on Image Processing with MATLAB

Image processing concepts, algorithms, and MATLAB

A Short Game of Life

I have written here, as well as in Digital Image Processing Using MATLAB, about using image processing operations to implement Conway's Game of Life. MATLAB creator Cleve Moler has written about the Game of Life several times (03-Sep-2012, 10-Sep-2012, 17-Sep-2012, and 08-Nov-2018) on Cleve's Corner.
Recently, I saw a particularly concise implementation of the game. My MathWorks colleague, Christopher Creutzig, recently posted the following code snippet on a MathWorks company forum. These seven lines of code initialize a 750x750 game board, run the board through 500 generations, and save the result as an animated GIF file.
im = rand(750,750)>0.8;
for k=1:500
t = conv2(im(:,:,k),[2,2,2;2,1,2;2,2,2],'same');
im(:,:,1,k+1) = (t > 4) & (t < 8);
end
imwrite(~im,'life.gif','DelayTime',0,'LoopCount',1)
Matt McDonnell, a former MathWorks consultant, explored several short implementation techniques in his 19-Jan-2010 post on Loren's Art of MATLAB. More good ideas can be found in the reader comments on that post.
Enjoy!
life.gif
|
  • print

评论

要发表评论,请点击 此处 登录到您的 MathWorks 帐户或创建一个新帐户。