1 | % 'avi2png': copy an avi movie to a series of B/W .png images (take the average of green and blue color components)
|
---|
2 | %----------------------------------------------------------------------
|
---|
3 | function GUI_input=avi2png(num_i1,num_i2,num_j1,num_j2,Series)
|
---|
4 | %% INPUT PARAMETERS (to edit)
|
---|
5 | increment=4% frame increment: the frequency of the png images will be (initial frequency)/increment.
|
---|
6 | colorweight=[0 0.5 0.5]; % relative weight of color components [r g b] for the resulting B/W image
|
---|
7 | colorweight=colorweight/sum(colorweight)
|
---|
8 |
|
---|
9 | %% default output (set the input options in the GUI series)
|
---|
10 | if ~exist('num_i1','var')
|
---|
11 | GUI_input={};
|
---|
12 | return
|
---|
13 | end
|
---|
14 | hh=guidata(Series.hseries);
|
---|
15 | set(hh.incr_i,'String',num2str(increment))% preset the increment in the GUI
|
---|
16 |
|
---|
17 | %% set file names
|
---|
18 | nbfield=length(num_i1);
|
---|
19 | aviname=fullfile(Series.RootPath,[Series.RootFile Series.FileExt]);
|
---|
20 | rootname=fullfile(Series.RootPath,Series.RootFile);
|
---|
21 | if ~exist(rootname,'dir')
|
---|
22 | mkdir(rootname)% will put the extracted images in a subdirectory with the same lname as the avi file (without extension)
|
---|
23 | end
|
---|
24 | basename=fullfile(rootname,'frame');
|
---|
25 |
|
---|
26 | %% enable waitbar and stop buttons on the series interface:
|
---|
27 | hRUN=findobj(Series.hseries,'Tag','RUN');% handle of the RUN button
|
---|
28 | hwaitbar=findobj(Series.hseries,'Tag','waitbar');%handle of the waitbar
|
---|
29 | waitbarpos(1)=Series.WaitbarPos(1);%x position of the waitbar
|
---|
30 | waitbarpos(3)=Series.WaitbarPos(3);% width of the waitbar
|
---|
31 |
|
---|
32 | %% read the movie object
|
---|
33 | if 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
|
---|
42 | end
|
---|
43 |
|
---|
44 | %% main loop on frames
|
---|
45 | for 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
|
---|
63 | end
|
---|
64 |
|
---|
65 | %% create xml file with timing:
|
---|
66 | info=aviinfo(aviname);
|
---|
67 | t=xmltree;
|
---|
68 | t=set(t,1,'name','ImaDoc');
|
---|
69 | [t,uid]=add(t,1,'element','Heading');
|
---|
70 | % A AJOUTER
|
---|
71 | % Heading.Project='';
|
---|
72 | Heading.ImageName='frame_1.png';
|
---|
73 | t=struct2xml(Heading,t,uid);
|
---|
74 | [t,uid]=add(t,1,'element','Camera');
|
---|
75 | Camera.TimeUnit='s';
|
---|
76 | % Camera.BurstTiming.FrameFrequency=info.FramesPerSecond/increment;
|
---|
77 | Camera.BurstTiming.Dti=increment/info.FramesPerSecond;
|
---|
78 | Camera.BurstTiming.NbDti=numel(num_i1)-1;
|
---|
79 | Camera.BurstTiming.Time=(num_i1(1)-1)/info.FramesPerSecond;%time of the first frame of the avi movie
|
---|
80 | t=struct2xml(Camera,t,uid);
|
---|
81 | save(t,[basename '.xml'])
|
---|
82 |
|
---|
83 | %------------------------------------------------------------------------
|
---|
84 | %--read images and convert them to the uint16 format used for PIV
|
---|
85 | function A=read_image(filename,type_ima,num,MovieObject)
|
---|
86 | %------------------------------------------------------------------------
|
---|
87 | %num is the view number needed for an avi movie
|
---|
88 | switch 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);
|
---|
98 | end
|
---|
99 |
|
---|