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 |
|
---|
4 | %=======================================================================
|
---|
5 | % Copyright 2008-2014, LEGI UMR 5519 / CNRS UJF G-INP, Grenoble, France
|
---|
6 | % http://www.legi.grenoble-inp.fr
|
---|
7 | % Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
|
---|
8 | %
|
---|
9 | % This file is part of the toolbox UVMAT.
|
---|
10 | %
|
---|
11 | % UVMAT is free software; you can redistribute it and/or modify
|
---|
12 | % it under the terms of the GNU General Public License as published
|
---|
13 | % by the Free Software Foundation; either version 2 of the license,
|
---|
14 | % or (at your option) any later version.
|
---|
15 | %
|
---|
16 | % UVMAT is distributed in the hope that it will be useful,
|
---|
17 | % but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | % GNU General Public License (see LICENSE.txt) for more details.
|
---|
20 | %=======================================================================
|
---|
21 |
|
---|
22 | function GUI_input=avi2png(num_i1,num_i2,num_j1,num_j2,Series)
|
---|
23 | %% INPUT PARAMETERS (to edit)
|
---|
24 | increment=4% frame increment: the frequency of the png images will be (initial frequency)/increment.
|
---|
25 | colorweight=[0 0.5 0.5]; % relative weight of color components [r g b] for the resulting B/W image
|
---|
26 | colorweight=colorweight/sum(colorweight)
|
---|
27 |
|
---|
28 | %% default output (set the input options in the GUI series)
|
---|
29 | if ~exist('num_i1','var')
|
---|
30 | GUI_input={};
|
---|
31 | return
|
---|
32 | end
|
---|
33 | hh=guidata(Series.hseries);
|
---|
34 | set(hh.incr_i,'String',num2str(increment))% preset the increment in the GUI
|
---|
35 |
|
---|
36 | %% set file names
|
---|
37 | nbfield=length(num_i1);
|
---|
38 | aviname=fullfile(Series.RootPath,[Series.RootFile Series.FileExt]);
|
---|
39 | rootname=fullfile(Series.RootPath,Series.RootFile);
|
---|
40 | if ~exist(rootname,'dir')
|
---|
41 | mkdir(rootname)% will put the extracted images in a subdirectory with the same lname as the avi file (without extension)
|
---|
42 | end
|
---|
43 | basename=fullfile(rootname,'frame');
|
---|
44 |
|
---|
45 | %% enable waitbar and stop buttons on the series interface:
|
---|
46 | hRUN=findobj(Series.hseries,'Tag','RUN');% handle of the RUN button
|
---|
47 | hwaitbar=findobj(Series.hseries,'Tag','waitbar');%handle of the waitbar
|
---|
48 | waitbarpos(1)=Series.WaitbarPos(1);%x position of the waitbar
|
---|
49 | waitbarpos(3)=Series.WaitbarPos(3);% width of the waitbar
|
---|
50 |
|
---|
51 | %% read the movie object
|
---|
52 | if isequal(lower(Series.FileExt),'.avi')
|
---|
53 | display('opening the avi movie ...')
|
---|
54 | hhh=which('mmreader');%look for the existence of 'mmreader'for movie reading
|
---|
55 | if ~isequal(hhh,'')&& mmreader.isPlatformSupported()
|
---|
56 | MovieObject=mmreader(aviname);
|
---|
57 | FileType='movie';
|
---|
58 | else
|
---|
59 | FileType='avi';
|
---|
60 | end
|
---|
61 | end
|
---|
62 |
|
---|
63 | %% main loop on frames
|
---|
64 | for ifile=1:nbfield
|
---|
65 | stopstate=get(hRUN,'BusyAction');
|
---|
66 | if isequal(stopstate,'queue')% if STOP command is not activated
|
---|
67 | waitbarpos(4)=(ifile/nbfield)*Series.WaitbarPos(4);
|
---|
68 | waitbarpos(2)=Series.WaitbarPos(4)+Series.WaitbarPos(2)-waitbarpos(4);
|
---|
69 | set(hwaitbar,'Position',waitbarpos)%update waitbar on the series interface
|
---|
70 | drawnow
|
---|
71 | A=read_image(aviname,'movie',num_i1(ifile),MovieObject);
|
---|
72 | if ndims(A)==3% convert color image to B/W
|
---|
73 | A=double(A);
|
---|
74 | A=colorweight(1)*A(:,:,1)+colorweight(2)*A(:,:,2)+colorweight(3)*A(:,:,3);
|
---|
75 | A=uint8(A);% transform to 8 bit integers
|
---|
76 | end
|
---|
77 | new_index=1+floor((num_i1(ifile)-num_i1(1))/increment);
|
---|
78 | filename=[basename '_' num2str(new_index) '.png'];%create image name
|
---|
79 | imwrite(A,filename,'BitDepth',8);%write image
|
---|
80 | display(['new frame ' num2str(new_index) ' written as png image'])
|
---|
81 | end
|
---|
82 | end
|
---|
83 |
|
---|
84 | %% create xml file with timing:
|
---|
85 | info=aviinfo(aviname);
|
---|
86 | t=xmltree;
|
---|
87 | t=set(t,1,'name','ImaDoc');
|
---|
88 | [t,uid]=add(t,1,'element','Heading');
|
---|
89 | % A AJOUTER
|
---|
90 | % Heading.Project='';
|
---|
91 | Heading.ImageName='frame_1.png';
|
---|
92 | t=struct2xml(Heading,t,uid);
|
---|
93 | [t,uid]=add(t,1,'element','Camera');
|
---|
94 | Camera.TimeUnit='s';
|
---|
95 | % Camera.BurstTiming.FrameFrequency=info.FramesPerSecond/increment;
|
---|
96 | Camera.BurstTiming.Dti=increment/info.FramesPerSecond;
|
---|
97 | Camera.BurstTiming.NbDti=numel(num_i1)-1;
|
---|
98 | Camera.BurstTiming.Time=(num_i1(1)-1)/info.FramesPerSecond;%time of the first frame of the avi movie
|
---|
99 | t=struct2xml(Camera,t,uid);
|
---|
100 | save(t,[basename '.xml'])
|
---|
101 |
|
---|
102 | %------------------------------------------------------------------------
|
---|
103 | %--read images and convert them to the uint16 format used for PIV
|
---|
104 | function A=read_image(filename,type_ima,num,MovieObject)
|
---|
105 | %------------------------------------------------------------------------
|
---|
106 | %num is the view number needed for an avi movie
|
---|
107 | switch type_ima
|
---|
108 | case 'movie'
|
---|
109 | A=read(MovieObject,num);
|
---|
110 | case 'avi'
|
---|
111 | mov=aviread(filename,num);
|
---|
112 | A=frame2im(mov(1));
|
---|
113 | case 'multimage'
|
---|
114 | A=imread(filename,num);
|
---|
115 | case 'image'
|
---|
116 | A=imread(filename);
|
---|
117 | end
|
---|
118 |
|
---|