Changeset 435 for trunk/src/civ_matlab.m


Ignore:
Timestamp:
May 31, 2012, 8:21:06 AM (12 years ago)
Author:
sommeria
Message:

civ improved to deal with movies. Introduction of a file type mmreader needed for Matlab 2009 (Videoreader not available)

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src

    • Property svn:ignore
      •  

        old new  
        44XML_SCHEMAS
        55bin
         6check_files.m
        67toolbox_calib
        78uvmat_doc
  • trunk/src/civ_matlab.m

    r411 r435  
    5252check_patch1=0;%default
    5353
     54% case of input Param set by an xml file (batch mode)
    5455if ischar(Param)
    5556    Param=xml2struct(Param); %if Param is the name of an xml file, read this file as a Matlab structure
     57    if isfield(Param,'Civ1')
     58        if strcmp(Param.Civ1.FileTypeA,'video')
     59            Param.Civ1.ImageA=VideoReader(Param.Civ1.ImageA);
     60        elseif strcmp(Param.Civ1.FileTypeA,'mmreader')
     61            Param.Civ1.ImageA=mmreader(Param.Civ1.ImageA);
     62        end
     63        if strcmp(Param.Civ1.FileTypeB,'video')
     64            Param.Civ1.ImageB=VideoReader(Param.Civ1.ImageB);
     65        elseif strcmp(Param.Civ1.FileTypeB,'mmreader')
     66            Param.Civ1.ImageB=mmreader(Param.Civ1.ImageB);
     67        end
     68    end
     69    if isfield(Param,'Civ2')
     70        if strcmp(Param.Civ2.FileTypeA,'video')
     71            Param.Civ2.ImageA=VideoReader(Param.Civ2.ImageA);
     72        elseif strcmp(Param.Civ2.FileTypeA,'mmreader')
     73            Param.Civ2.ImageA=mmreader(Param.Civ2.ImageA);
     74        end
     75         if strcmp(Param.Civ2.FileTypeB,'video')
     76            Param.Civ2.ImageB=VideoReader(Param.Civ2.ImageB);
     77        elseif strcmp(Param.Civ2.FileTypeB,'mmreader')
     78            Param.Civ2.ImageB=mmreader(Param.Civ2.ImageB);
     79        end
     80    end
    5681end
    5782
    5883%% Civ1
    5984if isfield (Param,'Civ1')
    60 %     check_civ1=1;% test for further use of civ1 results
     85    %     check_civ1=1;% test for further use of civ1 results
    6186    % %% prepare images
    6287    par_civ1=Param.Civ1;
     
    7297        end
    7398    else
    74         if isfield(par_civ1,'ImageA')&&(ischar(par_civ1.ImageA)||strcmp(class(par_civ1.ImageA),'VideoReader')) % case with no image: only the PIV grid is calculated
    75             [Field,ParamOut,errormsg] = read_field(par_civ1.ImageA,par_civ1.FileTypeA,[],par_civ1.i1);
    76             par_civ1.ImageA=Field.A;%imread(par_civ1.ImageA);%[Field,ParamOut,errormsg] = read_field(ObjectName,FileType,ParamIn,num)
    77         end
    78         if isfield(par_civ1,'ImageB')&& (ischar(par_civ1.ImageB)||strcmp(class(par_civ1.ImageA),'VideoReader'))
    79             [Field,ParamOut,errormsg] = read_field(par_civ1.ImageB,par_civ1.FileTypeB,[],par_civ1.i2);
    80             par_civ1.ImageB=Field.A;%=imread(par_civ1.ImageB);
     99        if isfield(par_civ1,'ImageA')%&&(ischar(par_civ1.ImageA)||strcmp(class(par_civ1.ImageA),'VideoReader')) % case with no image: only the PIV grid is calculated
     100           
     101            [Field,ParamOut,errormsg] = read_field(par_civ1.ImageA,par_civ1.FileTypeA,[],par_civ1.FrameIndexA);
     102            if ~isempty(errormsg)
     103                errormsg=['error in civ_matlab/read_field:' errormsg];
     104                return
     105            end
     106            par_civ1.ImageA=Field.A;%= image matrix A in the first input field
     107        end
     108        if isfield(par_civ1,'ImageB')%&& (ischar(par_civ1.ImageB)||strcmp(class(par_civ1.ImageA),'VideoReader'))
     109            [Field,ParamOut,errormsg] = read_field(par_civ1.ImageB,par_civ1.FileTypeB,[],par_civ1.FrameIndexB);
     110            if ~isempty(errormsg)
     111                errormsg=['error in civ_matlab/read_field:' errormsg];
     112                return
     113            end
     114            par_civ1.ImageB=Field.A;%= image matrix A in the second input field
    81115        end
    82116    end
     
    121155    Data.Civ1_C=reshape(ctable,[],1);
    122156    Data.Civ1_F=reshape(F,[],1);
    123     Data.CivStage=1; 
     157    Data.CivStage=1;
    124158else
    125159    if exist('ncfile','var')
     
    162196        Data.VarDimName=[Data.VarDimName {'nb_vec_1'}];
    163197        nbvar=length(Data.ListVarName);
    164         Data.VarAttribute{nbvar}.Role='errorflag';   
     198        Data.VarAttribute{nbvar}.Role='errorflag';
    165199        Data.Civ1_FF=fix(Param.Fix1,Data.Civ1_F,Data.Civ1_C,Data.Civ1_U,Data.Civ1_V);
    166         Data.CivStage=2;   
    167     end
    168 end   
     200        Data.CivStage=2;
     201    end
     202end
    169203%% Patch1
    170204if isfield (Param,'Patch1')
     
    180214    nbvar=length(Data.ListVarName);
    181215    Data.ListVarName=[Data.ListVarName {'Civ1_U_smooth','Civ1_V_smooth','Civ1_SubRange','Civ1_NbSites','Civ1_Coord_tps','Civ1_U_tps','Civ1_V_tps'}];
    182         Data.VarDimName=[Data.VarDimName {'nb_vec_1','nb_vec_1',{'nb_coord','nb_bounds','nb_subdomain_1'},{'nb_subdomain_1'},...
    183              {'nb_tps_1','nb_coord','nb_subdomain_1'},{'nb_tps_1','nb_subdomain_1'},{'nb_tps_1','nb_subdomain_1'}}];
     216    Data.VarDimName=[Data.VarDimName {'nb_vec_1','nb_vec_1',{'nb_coord','nb_bounds','nb_subdomain_1'},{'nb_subdomain_1'},...
     217        {'nb_tps_1','nb_coord','nb_subdomain_1'},{'nb_tps_1','nb_subdomain_1'},{'nb_tps_1','nb_subdomain_1'}}];
    184218    Data.VarAttribute{nbvar+1}.Role='vector_x';
    185219    Data.VarAttribute{nbvar+2}.Role='vector_y';
     
    191225    if isfield(Data,'Civ1_FF')
    192226        ind_good=find(Data.Civ1_FF==0);
    193     else 
     227    else
    194228        ind_good=1:numel(Data.Civ1_X);
    195229    end
    196230    [Data.Civ1_SubRange,Data.Civ1_NbSites,Data.Civ1_Coord_tps,Data.Civ1_U_tps,Data.Civ1_V_tps,tild,Ures, Vres,tild,FFres]=...
    197             filter_tps([Data.Civ1_X(ind_good) Data.Civ1_Y(ind_good)],Data.Civ1_U(ind_good),Data.Civ1_V(ind_good),[],Data.Patch1_SubDomain,Data.Patch1_Rho,Data.Patch1_Threshold);
    198       fill=zeros(3,2,size(Data.Civ1_SubRange,3)); %matrix of zeros to complement the matrix Data.Civ1_Coord_tps (conveninent for file storage)
    199       Data.Civ1_Coord_tps=cat(1,Data.Civ1_Coord_tps,fill);
    200       Data.Civ1_U_smooth(ind_good)=Ures;
    201       Data.Civ1_V_smooth(ind_good)=Vres;
    202       Data.Civ1_FF(ind_good)=FFres;
    203       Data.CivStage=3;                             
    204 end   
     231        filter_tps([Data.Civ1_X(ind_good) Data.Civ1_Y(ind_good)],Data.Civ1_U(ind_good),Data.Civ1_V(ind_good),[],Data.Patch1_SubDomain,Data.Patch1_Rho,Data.Patch1_Threshold);
     232    fill=zeros(3,2,size(Data.Civ1_SubRange,3)); %matrix of zeros to complement the matrix Data.Civ1_Coord_tps (conveninent for file storage)
     233    Data.Civ1_Coord_tps=cat(1,Data.Civ1_Coord_tps,fill);
     234    Data.Civ1_U_smooth(ind_good)=Ures;
     235    Data.Civ1_V_smooth(ind_good)=Vres;
     236    Data.Civ1_FF(ind_good)=FFres;
     237    Data.CivStage=3;
     238end
    205239
    206240%% Civ2
     
    209243    if ~isfield (Param,'Civ1') || ~strcmp(Param.Civ1.ImageA,par_civ2.ImageA)
    210244        %read first image if not already done for civ1
    211         [Field,ParamOut,errormsg] = read_field(par_civ2.ImageA,par_civ2.FileTypeA,[],par_civ2.i1);
     245        [Field,ParamOut,errormsg] = read_field(par_civ2.ImageA,par_civ2.FileTypeA,[],par_civ2.FrameIndexA);
     246                    if ~isempty(errormsg)
     247                errormsg=['error in civ_matlab/read_field:' errormsg];
     248                return
     249            end
    212250        par_civ2.ImageA=Field.A;
    213251    else
     
    216254    if ~isfield (Param,'Civ1') || ~strcmp(Param.Civ1.ImageB,par_civ2.ImageB)
    217255        %read first image if not already done for civ1
    218         [Field,ParamOut,errormsg] = read_field(par_civ2.ImageB,par_civ2.FileTypeB,[],par_civ2.i2);
     256        [Field,ParamOut,errormsg] = read_field(par_civ2.ImageB,par_civ2.FileTypeB,[],par_civ2.FrameIndexB);
     257         if ~isempty(errormsg)
     258                errormsg=['error in civ_matlab/read_field:' errormsg];
     259                return
     260            end
    219261        par_civ2.ImageB=Field.A;
    220262    else
     
    233275    [GridX,GridY]=meshgrid(minix:par_civ2.Dx:maxix,miniy:par_civ2.Dy:maxiy);
    234276    GridX=reshape(GridX,[],1);
    235     GridY=reshape(GridY,[],1); 
     277    GridY=reshape(GridY,[],1);
    236278    Shiftx=zeros(size(GridX));% shift expected from civ1 data
    237279    Shifty=zeros(size(GridX));
     
    270312    par_civ2.Shiftx=Shiftx(nbval>=1)./nbval(nbval>=1);
    271313    par_civ2.Shifty=Shifty(nbval>=1)./nbval(nbval>=1);
    272     par_civ2.Grid=[GridX(nbval>=1)-par_civ2.Shiftx/2 GridY(nbval>=1)-par_civ2.Shifty/2];% grid taken at the extrapolated origin of the displacement vectors   
     314    par_civ2.Grid=[GridX(nbval>=1)-par_civ2.Shiftx/2 GridY(nbval>=1)-par_civ2.Shifty/2];% grid taken at the extrapolated origin of the displacement vectors
    273315    if par_civ2.CheckDeformation
    274316        par_civ2.DUDX=DUDX./nbval;
     
    279321    % caluclate velocity data (y and v in indices, reverse to y component)
    280322    [xtable ytable utable vtable ctable F] = civ (par_civ2);
    281 %     diff_squared=(utable-par_civ2.Shiftx).*(utable-par_civ2.Shiftx)+(vtable+par_civ2.Shifty).*(vtable+par_civ2.Shifty);
    282 %     F(diff_squared>=4)=4; %flag vectors whose distance to the guess exceeds 2 pixels
     323    %     diff_squared=(utable-par_civ2.Shiftx).*(utable-par_civ2.Shiftx)+(vtable+par_civ2.Shifty).*(vtable+par_civ2.Shifty);
     324    %     F(diff_squared>=4)=4; %flag vectors whose distance to the guess exceeds 2 pixels
    283325    list_param=(fieldnames(Param.Civ2))';
    284326    list_remove={'pxcmx','pxcmy','npx','npy','gridflag','maskflag','term_a','term_b','T0'};
     
    300342    end
    301343    Data.ListGlobalAttribute=[Data.ListGlobalAttribute Civ2_param {'Civ2_Time','Civ2_Dt'}];
    302 %     Data.Civ2_Time=par_civ2.Time;
    303 %     Data.Civ2_Dt=par_civ2.Dt;
     344    %     Data.Civ2_Time=par_civ2.Time;
     345    %     Data.Civ2_Dt=par_civ2.Dt;
    304346    nbvar=numel(Data.ListVarName);
    305347    Data.ListVarName=[Data.ListVarName {'Civ2_X','Civ2_Y','Civ2_U','Civ2_V','Civ2_F','Civ2_C'}];%  cell array containing the names of the fields to record
     
    340382        Data.VarDimName=[Data.VarDimName {'nb_vec_2'}];
    341383        nbvar=length(Data.ListVarName);
    342         Data.VarAttribute{nbvar}.Role='errorflag';   
     384        Data.VarAttribute{nbvar}.Role='errorflag';
    343385        Data.Civ2_FF=fix(Param.Fix2,Data.Civ2_F,Data.Civ2_C,Data.Civ2_U,Data.Civ2_V);
    344         Data.CivStage=Data.CivStage+1;   
     386        Data.CivStage=Data.CivStage+1;
    345387    end
    346388   
    347 end   
     389end
    348390
    349391%% Patch2
     
    356398    Data.ListVarName=[Data.ListVarName {'Civ2_U_smooth','Civ2_V_smooth','Civ2_SubRange','Civ2_NbSites','Civ2_Coord_tps','Civ2_U_tps','Civ2_V_tps'}];
    357399    Data.VarDimName=[Data.VarDimName {'nb_vec_2','nb_vec_2',{'nb_coord','nb_bounds','nb_subdomain_2'},{'nb_subdomain_2'},...
    358              {'nb_tps_2','nb_coord','nb_subdomain_2'},{'nb_tps_2','nb_subdomain_2'},{'nb_tps_2','nb_subdomain_2'}}];
    359 
    360         Data.VarAttribute{nbvar+1}.Role='vector_x';
     400        {'nb_tps_2','nb_coord','nb_subdomain_2'},{'nb_tps_2','nb_subdomain_2'},{'nb_tps_2','nb_subdomain_2'}}];
     401   
     402    Data.VarAttribute{nbvar+1}.Role='vector_x';
    361403    Data.VarAttribute{nbvar+2}.Role='vector_y';
    362404    Data.VarAttribute{nbvar+5}.Role='coord_tps';
     
    369411    else
    370412        ind_good=1:numel(Data.Civ2_X);
    371     end 
     413    end
    372414    [Data.Civ2_SubRange,Data.Civ2_NbSites,Data.Civ2_Coord_tps,Data.Civ2_U_tps,Data.Civ2_V_tps,tild,Ures, Vres,tild,FFres]=...
    373          filter_tps([Data.Civ2_X(ind_good) Data.Civ2_Y(ind_good)],Data.Civ2_U(ind_good),Data.Civ2_V(ind_good),[],Data.Patch2_SubDomain,Data.Patch2_Rho,Data.Patch2_Threshold);
    374            fill=zeros(3,2,size(Data.Civ2_SubRange,3)); %matrix of zeros to complement the matrix Data.Civ1_Coord_tps (conveninent for file storage)
    375       Data.Civ2_Coord_tps=cat(1,Data.Civ2_Coord_tps,fill);
     415        filter_tps([Data.Civ2_X(ind_good) Data.Civ2_Y(ind_good)],Data.Civ2_U(ind_good),Data.Civ2_V(ind_good),[],Data.Patch2_SubDomain,Data.Patch2_Rho,Data.Patch2_Threshold);
     416    fill=zeros(3,2,size(Data.Civ2_SubRange,3)); %matrix of zeros to complement the matrix Data.Civ1_Coord_tps (conveninent for file storage)
     417    Data.Civ2_Coord_tps=cat(1,Data.Civ2_Coord_tps,fill);
    376418    Data.Civ2_U_smooth(ind_good)=Ures;
    377419    Data.Civ2_V_smooth(ind_good)=Vres;
    378420    Data.Civ2_FF(ind_good)=FFres;
    379     Data.CivStage=Data.CivStage+1;                             
    380 end 
     421    Data.CivStage=Data.CivStage+1;
     422end
    381423
    382424%% write result in a netcdf file if requested
Note: See TracChangeset for help on using the changeset viewer.