source: trunk/src/series/avi2png.m @ 175

Last change on this file since 175 was 175, checked in by sommeria, 13 years ago

function avi2png to extract a series of png images from an avi movie

File size: 3.4 KB
Line 
1% 'avi2png': copy an avi movie to a series of B/W .png images (take the average of green and blue color components)
2%----------------------------------------------------------------------
3function GUI_input=avi2png(num_i1,num_i2,num_j1,num_j2,Series)
4
5if ~exist('num_i1','var')
6    GUI_input={};
7        return
8end
9hh=guidata(Series.hseries);
10increment=4;
11set(hh.incr_i,'String',num2str(increment))
12nbfield=length(num_i1);
13% basename=[fullfile(Series.RootPath,Series.RootFile) ];
14aviname=fullfile(Series.RootPath,[Series.RootFile Series.FileExt]);
15rootname=fullfile(Series.RootPath,Series.RootFile);
16if ~exist(rootname,'dir')
17    mkdir(rootname)% will put the extracted images in a subdirectory with the same lname as the avi file (without extension)
18end
19basename=fullfile(rootname,'frame');
20
21%enable waitbar and stop buttons on the series interface:
22hRUN=findobj(Series.hseries,'Tag','RUN');% handle of the RUN button
23hwaitbar=findobj(Series.hseries,'Tag','waitbar');%handle of the waitbar
24waitbarpos(1)=Series.WaitbarPos(1);%x position of the waitbar
25waitbarpos(3)=Series.WaitbarPos(3);% width of the waitbar
26
27if isequal(lower(Series.FileExt),'.avi')
28    hhh=which('mmreader');%look for the existence of 'mmreader'for movie reading
29    if ~isequal(hhh,'')&& mmreader.isPlatformSupported()
30        MovieObject=mmreader(aviname);
31        FileType='movie';
32    else
33        FileType='avi';
34    end
35end
36%main loop
37for ifile=1:nbfield
38     stopstate=get(hRUN,'BusyAction');
39     if isequal(stopstate,'queue')% if STOP command is not activated
40        waitbarpos(4)=(ifile/nbfield)*Series.WaitbarPos(4);
41        waitbarpos(2)=Series.WaitbarPos(4)+Series.WaitbarPos(2)-waitbarpos(4);
42        set(hwaitbar,'Position',waitbarpos)%update waitbar on the series interface
43        drawnow
44        D=read_image(aviname,'movie',num_i1(ifile),MovieObject);
45        C=uint8(D);% transform to 8 bit integers
46        new_index=1+floor((num_i1(ifile)-num_i1(1))/increment);
47        filename=[basename '_' num2str(new_index) '.png'];%create image name
48        imwrite(C,filename,'BitDepth',8);%write image
49     end
50end
51
52%create xml file with timing:
53info=aviinfo(aviname);
54t=xmltree;
55t=set(t,1,'name','ImaDoc');
56[t,uid]=add(t,1,'element','Heading');
57% A AJOUTER
58% Heading.Project='';
59Heading.ImageName=[Series.RootFile '_' num2str(num_i1(1)) '.png'];
60t=struct2xml(Heading,t,uid);
61[t,uid]=add(t,1,'element','Camera');
62Camera.TimeUnit='s';
63Camera.BurstTiming.FrameFrequency=info.FramesPerSecond/increment;
64Camera.BurstTiming.Dti=1;
65Camera.BurstTiming.NbDti=numel(num_i1)-1;
66Camera.BurstTiming.Time=(num_i1(1)-1)/info.FramesPerSecond;%time of the first frame of the avi movie
67t=struct2xml(Camera,t,uid);
68save(t,[basename '.xml'])
69
70%------------------------------------------------------------------------
71%--read images and convert them to the uint16 format used for PIV
72function A=read_image(filename,type_ima,num,MovieObject)
73%------------------------------------------------------------------------
74%num is the view number needed for an avi movie
75switch type_ima
76    case 'movie'
77        A=read(MovieObject,num);
78    case 'avi'
79        mov=aviread(filename,num);
80        A=frame2im(mov(1));
81    case 'multimage'
82        A=imread(filename,num);
83    case 'image'   
84        A=imread(filename);
85end
86siz=size(A);
87if length(siz)==3;%color images
88    A=sum(double(A(:,:,2:3)),3)/2;% average green and blue components
89end
Note: See TracBrowser for help on using the repository browser.