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

Last change on this file since 339 was 176, checked in by sommeria, 14 years ago

avi2png cleaned and generalised

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