Changeset 309 for trunk/src/civ_uvmat.m


Ignore:
Timestamp:
Nov 30, 2011, 11:09:12 PM (12 years ago)
Author:
sommeria
Message:

many bugs corrected, cleaning of civ.m, resize of the GUI civ

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/civ_uvmat.m

    r273 r309  
    1 % To develop....
    2 function [Data,errormsg]= civ_uvmat(Param,ncfile)
     1%'civ_matlab': Matlab version of the PIV programs CivX
     2% --- call the sub-functions:
     3%   civ: PIV function itself
     4%   fix: removes false vectors after detection by various criteria
     5%   patch: make interpolation-smoothing
     6%------------------------------------------------------------------------
     7% function [Data,errormsg,result_conv]= civ_uvmat(Param,ncfile)
     8%
     9%OUTPUT
     10% Data=structure containing the PIV results and information on the processing parameters
     11% errormsg=error message char string, default=''
     12% resul_conv: image inter-correlation function for the last grid point (used for tests)
     13%
     14%INPUT:
     15% Param: input images and processing parameters
     16% ncfile: name of a netcdf file to be created for the result (extension .nc)
     17%
     18%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
     19%  Copyright  2011, LEGI / CNRS-UJF-INPG, joel.sommeria@legi.grenoble-inp.fr.
     20%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
     21%     This is part of the toolbox UVMAT.
     22%
     23%     UVMAT is free software; you can redistribute it and/or modify
     24%     it under the terms of the GNU General Public License as published by
     25%     the Free Software Foundation; either version 2 of the License, or
     26%     (at your option) any later version.
     27%
     28%     UVMAT is distributed in the hope that it will be useful,
     29%     but WITHOUT ANY WARRANTY; without even the implied warranty of
     30%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     31%     GNU General Public License (open UVMAT/COPYING.txt) for more details.
     32%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
     33
     34function [Data,errormsg,result_conv]= civ_uvmat(Param,ncfile)
    335errormsg='';
    436Data.ListGlobalAttribute={'Conventions','Program','CivStage'};
     
    2052    image1=imread(par_civ1.filename_ima_a);
    2153    image2=imread(par_civ1.filename_ima_b);
    22     stepx=str2num(par_civ1.dx);
    23     stepy=str2num(par_civ1.dy);
    24     ibx2=ceil(str2num(par_civ1.ibx)/2);
    25     iby2=ceil(str2num(par_civ1.iby)/2);
    26     isx2=ceil(str2num(par_civ1.isx)/2);
    27     isy2=ceil(str2num(par_civ1.isy)/2);
    28     shiftx=str2num(par_civ1.shiftx);
    29     shifty=str2num(par_civ1.shifty);
     54    ibx2=ceil(par_civ1.Bx/2);
     55    iby2=ceil(par_civ1.By/2);
     56    isx2=ceil(par_civ1.Searchx/2);
     57    isy2=ceil(par_civ1.Searchy/2);
     58    shiftx=par_civ1.Shiftx;
     59    shifty=par_civ1.Shifty;
    3060    miniy=max(1+isy2+shifty,1+iby2);
    3161    minix=max(1+isx2-shiftx,1+ibx2);
    3262    maxiy=min(size(image1,1)-isy2+shifty,size(image1,1)-iby2);
    3363    maxix=min(size(image1,2)-isx2-shiftx,size(image1,2)-ibx2);
    34     [GridX,GridY]=meshgrid(minix:stepx:maxix,miniy:stepy:maxiy);
    35     PointCoord(:,1)=reshape(GridX,[],1);
    36     PointCoord(:,2)=reshape(GridY,[],1);
    37     if isfield(par_civ1,'maskname') && ~isempty(par_civ1.maskname) && ~isequal(par_civ1.maskname,'noFile use default')
    38         maskname=par_civ1.maskname;
     64    if ~isfield(par_civ1,'PointCoord')   
     65        [GridX,GridY]=meshgrid(minix:par_civ1.Dx:maxix,miniy:par_civ1.Dy:maxiy);
     66        par_civ1.PointCoord(:,1)=reshape(GridX,[],1);
     67        par_civ1.PointCoord(:,2)=reshape(GridY,[],1);
     68    end
     69    if par_civ1.CheckMask && isfield(par_civ1,'MaskName') && ~isempty(par_civ1.MaskName)
     70        maskname=par_civ1.MaskName;
    3971        mask=imread(maskname);
    4072    end
    4173    % caluclate velocity data (y and v in indices, reverse to y component)
    42     [xtable ytable utable vtable ctable F] = pivlab (image1,image2,ibx2,iby2,isx2,isy2,shiftx,-shifty,PointCoord,str2num(par_civ1.rho), mask);
     74    [xtable ytable utable vtable ctable F result_conv errormsg] = civ (image1,image2,ibx2,iby2,isx2,isy2,shiftx,-shifty,par_civ1.PointCoord,par_civ1.Rho, mask);
    4375    list_param=(fieldnames(par_civ1))';
    4476    list_remove={'pxcmx','pxcmy','npx','npy','gridflag','maskflag','term_a','term_b','T0'};
    45     index=zeros(size(list_param));
    46     for ilist=1:length(list_remove)
    47         index=strcmp(list_remove{ilist},list_param);
    48         if ~isempty(find(index,1))
    49             list_param(index)=[];
    50         end
    51     end
     77    index_remove=zeros(size(list_param));
     78    for name=list_remove %loop on the list of names
     79        index_remove=index_remove +strcmp(name{1},list_param);%index of the current name = name{1}
     80    end
     81    list_param(find(index_remove,1))=[];
     82    Civ1_param=list_param;%initialisation
    5283    for ilist=1:length(list_param)
    5384        Civ1_param{ilist}=['Civ1_' list_param{ilist}];
    54         eval(['Data.Civ1_' list_param{ilist} '=Param.Civ1.' list_param{ilist} ';'])
     85        Data.(['Civ1_' list_param{ilist}])=par_civ1.(list_param{ilist});
    5586    end
    5687    if isfield(Data,'Civ1_gridname') && strcmp(Data.Civ1_gridname(1:6),'noFile')
     
    96127
    97128%% Fix1
     129%                 if Param.CheckCiv1==1
     130%                     Param.Civ1=Param.Civ1;
     131%                 end
     132%                 if Param.CheckFix1==1
     133%                     Param.Fix1=Param.Fix1;
     134%                     fix1.WarnFlags=[];
     135%                     if get(handles.CheckFmin2,'Value')
     136%                         fix1.WarnFlags=[fix1.WarnFlags -2];
     137%                     end
     138%                     if get(handles.CheckF3,'Value')
     139%                         fix1.WarnFlags=[fix1.WarnFlags 3];
     140%                     end
     141%                     fix1.LowerBoundCorr=thresh_vecC1;
     142%                     if get(handles.num_MinVel,'Value')
     143%                         fix1.UppperBoundVel=thresh_vel1;
     144%                     else
     145%                         fix1.LowerBoundVel=thresh_vel1;
     146%                     end
     147%                     if get(handles.CheckMask,'Value')
     148%                         fix1.MaskName=maskname;
     149%                     end
     150%                     Param.Fix1=fix1;
     151%                 end
     152%                 if Param.CheckPatch1==1
     153%                     if strcmp(compare,'stereo PIV')
     154%                         filebase_A=filecell.filebase;
     155%                         [pp,ff]=fileparts(filebase_A);
     156%                         filebase_B=fullfile(pp,get(handles.RootName_1,'String'));
     157%                         %TO CHECK: filecell.nc.civ1{ifile,j},filecell.ncA.civ1{ifile,j} have been switched according to Matias Duran
     158%                         RUN_STLIN(filecell.nc.civ1{ifile,j},filecell.ncA.civ1{ifile,j},'civ1',filecell.st{ifile,j},...
     159%                             str2num(nx_patch1),str2num(ny_patch1),str2num(thresh_patch1),[filebase_A '.xml'],[filebase_B '.xml'])
     160%                     else
     161%                         Param.Patch1.Rho=rho_patch1;
     162%                         Param.Patch1.Threshold=thresh_patch1;
     163%                         Param.Patch1.SubDomain=subdomain_patch1;
     164%                     end
     165%                 end
     166%                 if Param.CheckCiv2==1
     167%                     Param.Civ2=Param.Civ2;
     168%                 end
     169%                 if Param.CheckFix2==1
     170%                     fix2.WarnFlags=[];
     171%                     if get(handles.CheckFmin2,'Value')
     172%                         fix2.WarnFlags=[fix2.WarnFlags -2];
     173%                     end
     174%                     if get(handles.CheckF4,'Value')
     175%                         fix2.WarnFlags=[fix2.WarnFlags 4];
     176%                     end
     177%                     if get(handles.CheckF3,'Value')
     178%                         fix2.WarnFlags=[fix2.WarnFlags 3];
     179%                     end
     180%                     fix2.LowerBoundCorr=thresh_vec2C;
     181%                     if get(handles.num_MinVel,'Value')
     182%                         fix2.UppperBoundVel=thresh_vel2;
     183%                     else
     184%                         fix2.LowerBoundVel=thresh_vel2;
     185%                     end
     186%                     if get(handles.CheckMask,'Value')
     187%                         fix2.MaskName=maskname;
     188%                     end
     189%                     Param.Fix2=fix2;
     190%                 end
     191%                 if Param.CheckPatch2==1
     192%                     if strcmp(compare,'stereo PIV')
     193%                         filebase_A=filecell.filebase;
     194%                         [pp,ff]=fileparts(filebase_A);
     195%                         filebase_B=fullfile(pp,get(handles.RootName_1,'String'));
     196%                         RUN_STLIN(filecell.ncA.civ2{ifile,j},filecell.nc.civ2{ifile,j},'civ2',filecell.st{ifile,j},...
     197%                             str2num(nx_patch2),str2num(ny_patch2),str2num(thresh_patch2),[filebase_A '.xml'],[filebase_B '.xml'])
     198%                     else
     199%                         Param.Patch2.Rho=rho_patch2;
     200%                         Param.Patch2.Threshold=thresh_patch2;
     201%                         Param.Patch2.SubDomain=subdomain_patch2;
     202%                     end
     203%                 end
     204
    98205if isfield (Param,'Fix1')
    99206    ListFixParam=fieldnames(Param.Fix1);
     
    117224            Data.VarDimName=[Data.VarDimName {'nb_vectors'}];
    118225        end
    119         Data.vec_FixFlag=fix_uvmat(Param.Fix1,Data.vec_F,Data.vec_C,Data.vec_U,Data.vec_V,Data.vec_X,Data.vec_Y);
     226        Data.vec_FixFlag=fix(Param.Fix1,Data.vec_F,Data.vec_C,Data.vec_U,Data.vec_V,Data.vec_X,Data.vec_Y);
    120227    else
    121228        Data.ListVarName=[Data.ListVarName {'Civ1_FF'}];
     
    123230        nbvar=length(Data.ListVarName);
    124231        Data.VarAttribute{nbvar}.Role='errorflag';   
    125         Data.Civ1_FF=fix_uvmat(Param.Fix1,Data.Civ1_F,Data.Civ1_C,Data.Civ1_U,Data.Civ1_V);
     232        Data.Civ1_FF=fix(Param.Fix1,Data.Civ1_F,Data.Civ1_C,Data.Civ1_U,Data.Civ1_V);
    126233        Data.CivStage=2;   
    127234    end
     
    148255    end
    149256    [Data.Civ1_X_SubRange,Data.Civ1_Y_SubRange,Data.Civ1_NbSites,FFres,Ures, Vres,Data.Civ1_X_tps,Data.Civ1_Y_tps,Data.Civ1_U_tps,Data.Civ1_V_tps]=...
    150                             patch_uvmat(Data.Civ1_X(ind_good)',Data.Civ1_Y(ind_good)',Data.Civ1_U(ind_good)',Data.Civ1_V(ind_good)',Data.Patch1_Rho,Data.Patch1_Threshold,Data.Patch1_SubDomain);
     257                            patch(Data.Civ1_X(ind_good)',Data.Civ1_Y(ind_good)',Data.Civ1_U(ind_good)',Data.Civ1_V(ind_good)',Data.Patch1_Rho,Data.Patch1_Threshold,Data.Patch1_SubDomain);
    151258      Data.Civ1_U_Diff(ind_good)=Data.Civ1_U(ind_good)-Ures;
    152259      Data.Civ1_V_Diff(ind_good)=Data.Civ1_V(ind_good)-Vres;
     
    164271            image2=imread(par_civ2.filename_ima_b);%read second image if not already done for civ1
    165272    end
    166     stepx=str2double(par_civ2.dx);
    167     stepy=str2double(par_civ2.dy);
     273%     stepx=str2double(par_civ2.dx);
     274%     stepy=str2double(par_civ2.dy);
    168275    ibx2=ceil(str2double(par_civ2.ibx)/2);
    169276    iby2=ceil(str2double(par_civ2.iby)/2);
     
    182289        end
    183290            [Data.Civ1_X_SubRange,Data.Civ1_Y_SubRange,Data.Civ1_NbSites,FFres,Ures, Vres,Data.Civ1_X_tps,Data.Civ1_Y_tps,Data.Civ1_U_tps,Data.Civ1_V_tps]=...
    184                                 patch_uvmat(Data.Civ1_X(ind_good)',Data.Civ1_Y(ind_good)',Data.Civ1_U(ind_good)',Data.Civ1_V(ind_good)',Data.Patch1_Rho,Data.Patch1_Threshold,Data.Patch1_SubDomain);
     291                                patch(Data.Civ1_X(ind_good)',Data.Civ1_Y(ind_good)',Data.Civ1_U(ind_good)',Data.Civ1_V(ind_good)',Data.Patch1_Rho,Data.Patch1_Threshold,Data.Patch1_SubDomain);
    185292        end
    186293%     shiftx=str2num(par_civ1.shiftx);
     
    192299    maxiy=min(size(image1,1)-isy2+shifty,size(image1,1)-iby2);
    193300    maxix=min(size(image1,2)-isx2-shiftx,size(image1,2)-ibx2);
    194     [GridX,GridY]=meshgrid(minix:stepx:maxix,miniy:stepy:maxiy);
     301    [GridX,GridY]=meshgrid(minix:par_civ2.Dx:maxix,miniy:par_civ2.Dy:maxiy);
    195302    PointCoord(:,1)=reshape(GridX,[],1);
    196303    PointCoord(:,2)=reshape(GridY,[],1);
     
    199306    end
    200307    % caluclate velocity data (y and v in indices, reverse to y component)
    201     [xtable ytable utable vtable ctable F] = pivlab (image1,image2,ibx2,iby2,isx2,isy2,shiftx,-shifty,PointCoord,str2num(par_civ1.rho),mask);
     308    [xtable ytable utable vtable ctable F] = civ (image1,image2,ibx2,iby2,isx2,isy2,shiftx,-shifty,PointCoord,str2num(par_civ1.rho),mask);
    202309    list_param=(fieldnames(par_civ1))';
    203310    list_remove={'pxcmx','pxcmy','npx','npy','gridflag','maskflag','term_a','term_b','T0'};
     
    254361            Data.VarDimName=[Data.VarDimName {'nb_vectors2'}];
    255362        end
    256         Data.vec_FixFlag=fix_uvmat(Param.Fix2,Data.vec2_F,Data.vec2_C,Data.vec2_U,Data.vec2_V,Data.vec2_X,Data.vec2_Y);
     363        Data.vec_FixFlag=fix(Param.Fix2,Data.vec2_F,Data.vec2_C,Data.vec2_U,Data.vec2_V,Data.vec2_X,Data.vec2_Y);
    257364    else
    258365        Data.ListVarName=[Data.ListVarName {'Civ2_FF'}];
     
    260367        nbvar=length(Data.ListVarName);
    261368        Data.VarAttribute{nbvar}.Role='errorflag';   
    262         Data.Civ2_FF=fix_uvmat(Param.Fix2,Data.Civ2_F,Data.Civ2_C,Data.Civ2_U,Data.Civ2_V);
     369        Data.Civ2_FF=fix(Param.Fix2,Data.Civ2_F,Data.Civ2_C,Data.Civ2_U,Data.Civ2_V);
    263370        Data.CivStage=5;   
    264371    end
     
    286393    end
    287394    [Data.Civ2_X_SubRange,Data.Civ2_Y_SubRange,Data.Civ2_NbSites,FFres,Ures, Vres,Data.Civ2_X_tps,Data.Civ2_Y_tps,Data.Civ2_U_tps,Data.Civ2_V_tps]=...
    288                             patch_uvmat(Data.Civ2_X(ind_good)',Data.Civ2_Y(ind_good)',Data.Civ2_U(ind_good)',Data.Civ2_V(ind_good)',Data.Patch2_Rho,Data.Patch2_Threshold,Data.Patch2_SubDomain);
     395                            patch(Data.Civ2_X(ind_good)',Data.Civ2_Y(ind_good)',Data.Civ2_U(ind_good)',Data.Civ2_V(ind_good)',Data.Patch2_Rho,Data.Patch2_Threshold,Data.Patch2_SubDomain);
    289396      Data.Civ2_U_Diff(ind_good)=Data.Civ2_U(ind_good)-Ures;
    290397      Data.Civ2_V_Diff(ind_good)=Data.Civ2_V(ind_good)-Vres;
     
    293400end   
    294401
    295 %% write result
    296 % 'TESTcalc'
    297 % [DataOut,errormsg]=calc_field('velocity',Data)
     402%% write result in a netcdf file if requested
    298403if exist('ncfile','var')
    299 errormsg=struct2nc(ncfile,Data);
     404    errormsg=struct2nc(ncfile,Data);
    300405end
    301406
     
    323428%fieldref: 'civ1','filter1'...feld used in fileref
    324429
    325 function FF=fix_uvmat(Param,F,C,U,V,X,Y)
     430function FF=fix(Param,F,C,U,V,X,Y)
    326431FF=zeros(size(F));%default
     432Param
    327433
    328434%criterium on warn flags
    329 if isfield (Param,'WarnFlags')
    330     for iflag=1:numel(Param.WarnFlags)
    331         FF=(FF==1| F==Param.WarnFlags(iflag));
    332     end
    333 end
    334 
     435FlagName={'CheckFmin2','CheckF2','CheckF3','CheckF4'};
     436FlagVal=[-2 2 3 4];
     437for iflag=1:numel(FlagVal)
     438    if Param.(CheckFlag(iflag))
     439        FF=(FF==1| F==FlagVal(iflag));
     440% if isfield (Param,'WarnFlags')
     441%     for iflag=1:numel(Param.WarnFlags)
     442%         FF=(FF==1| F==Param.WarnFlags(iflag));
     443%     end
     444% end
     445    end
     446end
    335447%criterium on correlation values
    336 if isfield (Param,'LowerBoundCorr')
    337     FF=FF==1 | C<Param.LowerBoundCorr;
    338 end
     448if isfield (Param,'MinCorr')
     449    FF=FF==1 | C<Param.MinCorr;
     450end
     451return
     452
    339453
    340454if isfield (Param,'LowerBoundVel')&& ~isequal(Param.LowerBoundVel,0)
     
    427541%------------------------------------------------------------------------
    428542% patch function
    429 function [SubRangx,SubRangy,nbpoints,FF,U_smooth,V_smooth,X_tps,Y_tps,U_tps,V_tps] =patch_uvmat(X,Y,U,V,Rho,Threshold,SubDomain)
     543function [SubRangx,SubRangy,nbpoints,FF,U_smooth,V_smooth,X_tps,Y_tps,U_tps,V_tps] =patch(X,Y,U,V,Rho,Threshold,SubDomain)
    430544%subdomain decomposition
    431545warning off
     
    542656
    543657
    544 
    545 
     658% 'civ': function piv.m adapted from PIVlab http://pivlab.blogspot.com/
     659%--------------------------------------------------------------------------
     660% function [xtable ytable utable vtable typevector] = civ (image1,image2,ibx,iby step, subpixfinder, mask, roi)
     661%
     662% OUTPUT:
     663% xtable: set of x coordinates
     664% ytable: set of y coordiantes
     665% utable: set of u displacements (along x)
     666% vtable: set of v displacements (along y)
     667% ctable: max image correlation for each vector
     668% typevector: set of flags, =1 for good, =0 for NaN vectors
     669%
     670%INPUT:
     671% image1:first image (matrix)
     672% image2: second image (matrix)
     673% ibx2,iby2: half size of the correlation box along x and y, in px (size=(2*iby2+1,2*ibx2+1)
     674% isx2,isy2: half size of the search box along x and y, in px (size=(2*isy2+1,2*isx2+1)
     675% shiftx, shifty: shift of the search box (in pixel index, yshift reversed)
     676% step: mesh of the measurement points (in px)
     677% subpixfinder=1 or 2 controls the curve fitting of the image correlation
     678% mask: =[] for no mask
     679% roi: 4 element vector defining a region of interest: x position, y position, width, height, (in image indices), for the whole image, roi=[];
     680function [xtable ytable utable vtable ctable F result_conv errormsg] = civ (image1,image2,ibx2,iby2,isx2,isy2,shiftx,shifty, GridIndices, subpixfinder,mask)
     681%this funtion performs the DCC PIV analysis. Recent window-deformation
     682%methods perform better and will maybe be implemented in the future.
     683nbvec=size(GridIndices,1);
     684xtable=zeros(nbvec,1);
     685ytable=xtable;
     686utable=xtable;
     687vtable=xtable;
     688ctable=xtable;
     689F=xtable;
     690result_conv=[];
     691errormsg='';
     692%warning off %MATLAB:log:logOfZero
     693[npy_ima npx_ima]=size(image1);
     694if ~isequal(size(image2),[npy_ima npx_ima])
     695    errormsg='image pair with unequal size';
     696    return
     697end
     698
     699%% mask
     700testmask=0;
     701image1=double(image1);
     702image2=double(image2);
     703if exist('mask','var') && ~isempty(mask)
     704   testmask=1;
     705   if ~isequal(size(mask),[npy_ima npx_ima])
     706        errormsg='mask must be an image with the same size as the images';
     707        return
     708   end
     709    % Convention for mask
     710    % mask >200 : velocity calculated
     711    %  200 >=mask>150;velocity not calculated, interpolation allowed (bad spots)
     712    % 150>=mask >100: velocity not calculated, nor interpolated
     713    %  100>=mask> 20: velocity not calculated, impermeable (no flux through mask boundaries)
     714    %  20>=mask: velocity=0
     715    test_noflux=(mask<100) ;
     716    test_undefined=(mask<200 & mask>=100 );
     717    image1(test_undefined)=min(min(image1));% put image to zero in the undefined  area
     718    image2(test_undefined)=min(min(image2));% put image to zero in the undefined  area
     719end
     720
     721%% calculate correlations: MAINLOOP on velocity vectors
     722corrmax=0;
     723sum_square=1;% default
     724for ivec=1:nbvec
     725    iref=GridIndices(ivec,1);
     726    jref=GridIndices(ivec,2);
     727    testmask_ij=0;
     728    test0=0;
     729    if testmask
     730        if mask(jref,iref)<=20
     731           vector=[0 0];
     732           test0=1;
     733        else
     734            mask_crop1=mask(jref-iby2:jref+iby2,iref-ibx2:iref+ibx2);
     735            mask_crop2=mask(jref+shifty-isy2:jref+shifty+isy2,iref+shiftx-isx2:iref+shiftx+isx2);
     736            if ~isempty(find(mask_crop1<=200 & mask_crop1>100,1)) || ~isempty(find(mask_crop2<=200 & mask_crop2>100,1));
     737                testmask_ij=1;
     738            end
     739        end
     740    end
     741    if ~test0   
     742        image1_crop=image1(jref-iby2:jref+iby2,iref-ibx2:iref+ibx2);%extract a subimage (correlation box) from images 1 
     743        image2_crop=image2(jref+shifty-isy2:jref+shifty+isy2,iref+shiftx-isx2:iref+shiftx+isx2);%extract a larger subimage (search box) from image 2
     744        image1_crop=image1_crop-mean(mean(image1_crop));%substract the mean
     745        image2_crop=image2_crop-mean(mean(image2_crop));
     746        %reference: Oliver Pust, PIV: Direct Cross-Correlation
     747        result_conv= conv2(image2_crop,flipdim(flipdim(image1_crop,2),1),'valid');
     748        corrmax= max(max(result_conv));
     749        result_conv=(result_conv/corrmax)*255; %normalize, peak=always 255
     750        %Find the correlation max, at 255
     751        [y,x] = find(result_conv==255,1);
     752        if ~isempty(y) && ~isempty(x)
     753            try
     754                if subpixfinder==1
     755                    [vector,F(ivec)] = SUBPIXGAUSS (result_conv,x,y);
     756                elseif subpixfinder==2
     757                    [vector,F(ivec)] = SUBPIX2DGAUSS (result_conv,x,y);
     758                end
     759                sum_square=sum(sum(image1_crop.*image1_crop));
     760                ctable(ivec)=corrmax/sum_square;% correlation value
     761%                 if vector(1)>shiftx+isx2-ibx2+subpixfinder || vector(2)>shifty+isy2-iby2+subpixfinder
     762%                     F(ivec)=-2;%vector reaches the border of the search zone
     763%                 end
     764            catch ME
     765                vector=[0 0]; %if something goes wrong with cross correlation.....
     766                F(ivec)=3;
     767            end
     768        else
     769            vector=[0 0]; %if something goes wrong with cross correlation.....
     770            F(ivec)=3;
     771        end
     772        if testmask_ij
     773            F(ivec)=3;
     774        end
     775    end
     776   
     777    %Create the vector matrix x, y, u, v
     778    xtable(ivec)=iref+vector(1)/2;% convec flow (velocity taken at the point middle from imgae1 and 2)
     779    ytable(ivec)=jref+vector(2)/2;
     780    utable(ivec)=vector(1)+shiftx;
     781    vtable(ivec)=vector(2)+shifty;
     782end
     783result_conv=result_conv*corrmax/(255*sum_square);% keep the last correlation matrix for output
     784
     785
     786function [vector,F] = SUBPIXGAUSS (result_conv,x,y)
     787vector=[0 0]; %default
     788F=0;
     789[npy,npx]=size(result_conv);
     790
     791% if (x <= (size(result_conv,1)-1)) && (y <= (size(result_conv,1)-1)) && (x >= 1) && (y >= 1)
     792    %the following 8 lines are copyright (c) 1998, Uri Shavit, Roi Gurka, Alex Liberzon, Technion – Israel Institute of Technology
     793    %http://urapiv.wordpress.com
     794    peaky = y;
     795    if y <= npy-1 && y >= 1
     796        f0 = log(result_conv(y,x));
     797        f1 = real(log(result_conv(y-1,x)));
     798        f2 = real(log(result_conv(y+1,x)));
     799        peaky = peaky+ (f1-f2)/(2*f1-4*f0+2*f2);
     800    else
     801        F=-2; % warning flag for vector truncated by the limited search box
     802    end
     803    peakx=x;
     804    if x <= npx-1 && x >= 1
     805        f0 = log(result_conv(y,x));
     806        f1 = real(log(result_conv(y,x-1)));
     807        f2 = real(log(result_conv(y,x+1)));
     808        peakx = peakx+ (f1-f2)/(2*f1-4*f0+2*f2);
     809    else
     810        F=-2; % warning flag for vector truncated by the limited search box
     811    end
     812    vector=[peakx-floor(npx/2)-1 peaky-floor(npy/2)-1];
     813% else
     814%     vector=[NaN NaN];
     815% end
     816
     817function [vector,F] = SUBPIX2DGAUSS (result_conv,x,y)
     818vector=[0 0]; %default
     819F=-2;
     820peaky=y;
     821peakx=x;
     822[npy,npx]=size(result_conv);
     823if (x <= npx-1) && (y <= npy-1) && (x >= 1) && (y >= 1)
     824    F=0;
     825    for i=-1:1
     826        for j=-1:1
     827            %following 15 lines based on
     828            %H. Nobach Æ M. Honkanen (2005)
     829            %Two-dimensional Gaussian regression for sub-pixel displacement
     830            %estimation in particle image velocimetry or particle position
     831            %estimation in particle tracking velocimetry
     832            %Experiments in Fluids (2005) 38: 511–515
     833            c10(j+2,i+2)=i*log(result_conv(y+j, x+i));
     834            c01(j+2,i+2)=j*log(result_conv(y+j, x+i));
     835            c11(j+2,i+2)=i*j*log(result_conv(y+j, x+i));
     836            c20(j+2,i+2)=(3*i^2-2)*log(result_conv(y+j, x+i));
     837            c02(j+2,i+2)=(3*j^2-2)*log(result_conv(y+j, x+i));
     838            %c00(j+2,i+2)=(5-3*i^2-3*j^2)*log(result_conv_norm(maxY+j, maxX+i));
     839        end
     840    end
     841    c10=(1/6)*sum(sum(c10));
     842    c01=(1/6)*sum(sum(c01));
     843    c11=(1/4)*sum(sum(c11));
     844    c20=(1/6)*sum(sum(c20));
     845    c02=(1/6)*sum(sum(c02));
     846    deltax=(c11*c01-2*c10*c02)/(4*c20*c02-c11^2);
     847    deltay=(c11*c10-2*c01*c20)/(4*c20*c02-c11^2);
     848    if abs(deltax)<1
     849        peakx=x+deltax;
     850    end
     851    if abs(deltay)<1
     852        peaky=y+deltay;
     853    end
     854end
     855vector=[peakx-floor(npx/2)-1 peaky-floor(npy/2)-1];
     856
     857
Note: See TracChangeset for help on using the changeset viewer.