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

Last change on this file since 811 was 810, checked in by g7moreau, 10 years ago
  • Add license
File size: 4.8 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%----------------------------------------------------------------------
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
22function GUI_input=avi2png(num_i1,num_i2,num_j1,num_j2,Series)
23%% INPUT PARAMETERS (to edit)
24increment=4% frame increment: the frequency of the png images will be (initial frequency)/increment.
25colorweight=[0 0.5 0.5]; % relative weight of color components [r g b] for the resulting B/W image
26colorweight=colorweight/sum(colorweight)
27
28%% default output (set the input options in the GUI series)
29if ~exist('num_i1','var')
30    GUI_input={};
31        return
32end
33hh=guidata(Series.hseries);
34set(hh.incr_i,'String',num2str(increment))% preset the increment in the GUI
35
36%% set file names
37nbfield=length(num_i1);
38aviname=fullfile(Series.RootPath,[Series.RootFile Series.FileExt]);
39rootname=fullfile(Series.RootPath,Series.RootFile);
40if ~exist(rootname,'dir')
41    mkdir(rootname)% will put the extracted images in a subdirectory with the same lname as the avi file (without extension)
42end
43basename=fullfile(rootname,'frame');
44
45%% enable waitbar and stop buttons on the series interface:
46hRUN=findobj(Series.hseries,'Tag','RUN');% handle of the RUN button
47hwaitbar=findobj(Series.hseries,'Tag','waitbar');%handle of the waitbar
48waitbarpos(1)=Series.WaitbarPos(1);%x position of the waitbar
49waitbarpos(3)=Series.WaitbarPos(3);% width of the waitbar
50
51%% read the movie object
52if 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
61end
62
63%% main loop on frames
64for 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
82end
83
84%% create xml file with timing:
85info=aviinfo(aviname);
86t=xmltree;
87t=set(t,1,'name','ImaDoc');
88[t,uid]=add(t,1,'element','Heading');
89% A AJOUTER
90% Heading.Project='';
91Heading.ImageName='frame_1.png';
92t=struct2xml(Heading,t,uid);
93[t,uid]=add(t,1,'element','Camera');
94Camera.TimeUnit='s';
95% Camera.BurstTiming.FrameFrequency=info.FramesPerSecond/increment;
96Camera.BurstTiming.Dti=increment/info.FramesPerSecond;
97Camera.BurstTiming.NbDti=numel(num_i1)-1;
98Camera.BurstTiming.Time=(num_i1(1)-1)/info.FramesPerSecond;%time of the first frame of the avi movie
99t=struct2xml(Camera,t,uid);
100save(t,[basename '.xml'])
101
102%------------------------------------------------------------------------
103%--read images and convert them to the uint16 format used for PIV
104function A=read_image(filename,type_ima,num,MovieObject)
105%------------------------------------------------------------------------
106%num is the view number needed for an avi movie
107switch 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);
117end
118
Note: See TracBrowser for help on using the repository browser.