[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Creating Colored MPEG Movies



Hi Folks,

I've had several e-mails lately about how to create MPEG
movies, and in particular, color MPEG movies. I've added
an article to my web page and wrote this little example
program.

The trick for color MPEG movies is to make a 24-bit color
image out of your 2D image data. You do this, of course,
(this really is getting old hat by now, isn't it :-) by
running the 2D image though the color vectors.

(I notice that there is a COLOR keyword written for the
MPEG_PUT routine. From what I saw of the code the writer
was *trying* to do the right thing. The keyword is undocumented
in my IDL version, probably because they didn't quite get
things sorted out. If I try to use it, I crash MPEG_PUT.
This may be because I am running on a 24-bit display.)

In any case, here is a little example program. If you
want color movies, set the COLOR keyword. You can pass 
your favorite color table in with the TABLE keyword. If
you don't use it, the Standard Gamma II color table will
be used by default. If you don't set the COLOR keyword,
you will get gray-scale output.

Cheers,

David

----------------------------------------------------------
 PRO Make_MPEG_Movie, data, Color=color, Table=table

   ; Check keywords.

IF N_Elements(table) EQ 0 THEN table = 5
color = Keyword_Set(color)

   ; Load a 3D data set if none passed into program.
   
IF N_Elements(data) EQ 0 THEN BEGIN

      ; Open the MRI head data set.

   file = Filepath(SubDir=['examples', 'data'], 'head.dat')
   data = BytArr(80, 100, 57)
   OpenR, lun, file, /Get_Lun
   ReadU, lun, data
   Free_Lun, lun

   ; Rebin the data to make it a little bigger.

   data = Rebin(data, 80*3, 100*3, 57)

ENDIF

   ; Is this a 3D data set?

IF Size(data, /N_Dimensions) NE 3 THEN BEGIN
   ok = Dialog_Message('Data must have three dimensions.')
   RETURN
ENDIF

   ; Get the size of the data set.

s = Size(data, /Dimensions)
xsize = s[0]
ysize = s[1]
frames = s[2]

   ; Open the MPEG object.

mpegID = MPEG_Open([xsize, ysize], Filename='head.mpg')

   ; Need a color movie?
   
IF color THEN BEGIN

      ; Load a color table.

   LoadCT, 0 > table < 41, /Silent

      ; Create a 24-bit image for viewing. Get color table vectors.

   image24 = BytArr(3, xsize, ysize)
   TVLCT, r, g, b, /Get

   ; Load the frames.

   FOR j=0,frames-1 DO BEGIN
      image24[0,*,*] = r(data[*,*,j])
      image24[1,*,*] = g(data[*,*,j])
      image24[2,*,*] = b(data[*,*,j])
      MPEG_Put, mpegID, Image=image24, Frame=j
   ENDFOR

ENDIF ELSE BEGIN

   FOR j=0,frames-1 DO MPEG_Put, mpegID, Image=data[*,*,j], Frame=j

ENDELSE

   ; Save the MPEG sequence. Be patient this will take several seconds.

MPEG_Save, mpegID

   ; Close the MPEG sequence and file.

MPEG_Close, mpegID

END
----------------------------------------------------------
David Fanning, Ph.D.
Fanning Software Consulting
E-Mail: davidf@dfanning.com
Phone: 970-221-0438, Toll-Free Book Orders: 1-888-461-0155
Coyote's Guide to IDL Programming: http://www.dfanning.com/