Changeset 231 for trunk/src/pivlab.m


Ignore:
Timestamp:
Apr 5, 2011, 12:46:34 AM (13 years ago)
Author:
sommeria
Message:

interactive test for piv introduced various bug corrections

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/pivlab.m

    r227 r231  
    1919% mask: =[] for no mask
    2020% roi: 4 element vector defining a region of interest: x position, y position, width, height, (in image indices), for the whole image, roi=[];
    21 function [xtable ytable utable vtable ctable typevector] = pivlab (image1,image2,ibx2,iby2,isx2,isy2,shiftx,shifty, PointCoord, subpixfinder,mask)
     21function [xtable ytable utable vtable ctable typevector result_conv errormsg] = pivlab (image1,image2,ibx2,iby2,isx2,isy2,shiftx,shifty, GridIndices, subpixfinder,mask)
    2222%this funtion performs the DCC PIV analysis. Recent window-deformation
    2323%methods perform better and will maybe be implemented in the future.
     24errormsg='';
    2425warning off %MATLAB:log:logOfZero
    25 % if numel(roi)>0
    26 %     xroi=roi(1);
    27 %     yroi=roi(2);
    28 %     widthroi=roi(3);
    29 %     heightroi=roi(4);
    30 %     image1_roi=double(image1(yroi:yroi+heightroi,xroi:xroi+widthroi));
    31 %     image2_roi=double(image2(yroi:yroi+heightroi,xroi:xroi+widthroi));
    32 % else
     26[npy_ima npx_ima]=size(image1);
     27if ~isequal(size(image1),size(image2))
     28    errormsg='image pair with unequal size';
     29    return
     30end
    3331    xroi=0;
    3432    yroi=0;
    3533    image1_roi=double(image1);
    3634    image2_roi=double(image2);
    37 % end
    3835if numel(mask)>0
    3936    cellmask=mask;
    40     mask=zeros(size(image1_roi));
     37    mask=zeros(size(image1));
    4138    for i=1:size(cellmask,1);
    4239        masklayerx=cellmask{i,1};
    4340        masklayery=cellmask{i,2};
    44         mask = mask + poly2mask(masklayerx-xroi,masklayery-yroi,size(image1_roi,1),size(image1_roi,2)); %kleineres eingangsbild und maske geshiftet
     41        mask = mask + poly2mask(masklayerx-xroi,masklayery-yroi,npy_ima,npx_ima); %kleineres eingangsbild und maske geshiftet
    4542    end
    4643else
    47     mask=zeros(size(image1_roi));
     44    mask=zeros(size(image1));
    4845end
    4946mask(mask>1)=1;
    50 % ibx=2*ibx2-1%ibx and iby odd, reduced by 1 if even
    51 % iby=2*iby2-1
     47
     48% ibx=2*ibx2-1;%ibx and iby odd, reduced by 1 if even
     49% iby=2*iby2-1;
    5250% miniy=1+iby2
    5351% minix=1+ibx2
     
    7775image2_roi=padarray(image2_roi,[iby2 ibx2], min(min(image1_roi)));
    7876mask=padarray(mask,[iby2 ibx2],0);
    79 SubPixOffset=0.5;%odd values chosen for ibx and iby
    80 
    81 nbvec=size(PointCoord,1);
     77%SubPixOffset=0.5;%odd values chosen for ibx and iby
     78
     79nbvec=size(GridIndices,1);
    8280xtable=zeros(nbvec,1);
    8381ytable=xtable;
     
    9694%% MAINLOOP
    9795for ivec=1:nbvec
    98     iref=PointCoord(ivec,1);
    99     jref=PointCoord(ivec,2);
     96    iref=GridIndices(ivec,1);
     97    jref=GridIndices(ivec,2);
     98    %jref=npy_ima-PointCoord(ivec,2)+1;
    10099    image1_crop=image1_roi(jref-iby2:jref+iby2,iref-ibx2:iref+ibx2);
    101100    image2_crop=image2_roi(jref+shifty-isy2:jref+shifty+isy2,iref+shiftx-isx2:iref+shiftx+isx2);
    102 %     n=round((j-miniy)/maxiy*100);
    103 %     for i = minix:step:maxix % horizontal loop
    104 %         nrx=nrx+1;%used to determine the pos of the vector in resulting matrix
    105 %         if nrxreal < numelementsx
    106 %             nrxreal=nrxreal+1;
    107 %         else
    108 %             nrxreal=1;
    109 %         end
    110 %         startpoint=[i j];
    111 %         image1_crop=image1_roi(j:j+iby-1, i:i+ibx-1);
    112 %         image2_crop=image2_roi(ceil(j-iby/2):ceil(j+1.5*iby-1), ceil(i-ibx/2):ceil(i+1.5*ibx-1));
    113 %         corrmax=0;
    114101        if mask(jref,iref)==0
    115102           %reference: Oliver Pust, PIV: Direct Cross-Correlation
     
    121108            corrmax= max(max(result_conv));
    122109            result_conv=(result_conv/corrmax)*255; %normalize, peak=always 255
     110           % result_conv=flipdim(result_conv,2);%reverse x direction
    123111            %Find the 255 peak
    124112            [y,x] = find(result_conv==255);
     
    126114                try
    127115                    if subpixfinder==1
    128                         [vector] = SUBPIXGAUSS (result_conv,ibx,iby,x,y,SubPixOffset);
     116                        [vector] = SUBPIXGAUSS (result_conv,x,y);
    129117                    elseif subpixfinder==2
    130                         [vector] = SUBPIX2DGAUSS (result_conv,ibx,iby,x,y,SubPixOffset);
     118                        [vector] = SUBPIX2DGAUSS (result_conv,x,y);
    131119                    end
    132                 catch
    133                     vector=[NaN NaN]; %if something goes wrong with cross correlation.....
     120                catch ME
     121                    errormsg=ME.message
     122                    vector=[0 0]; %if something goes wrong with cross correlation.....
    134123                end
    135124            else
    136                 vector=[NaN NaN]; %if something goes wrong with cross correlation.....
     125                vector=[0 0]; %if something goes wrong with cross correlation.....
    137126            end
    138127        else %if mask was not 0 then
    139             vector=[NaN NaN];
     128            vector=[0 0];
    140129            typevector(ivec)=0;
    141130        end
    142131
    143132        %Create the vector matrix x, y, u, v
    144         xtable(ivec)=PointCoord(ivec,2);
    145         ytable(ivec)=PointCoord(ivec,1);
     133        xtable(ivec)=GridIndices(ivec,1);
     134        ytable(ivec)=GridIndices(ivec,2);
    146135        utable(ivec)=vector(1);
    147136        vtable(ivec)=vector(2);
    148         ctable(ivec)=corrmax/sum(sum(image1_crop.*image1_crop));
    149 end
    150 % xtable=xtable-ibx2;
    151 % ytable=ytable-iby2;
    152 %
    153 % xtable=xtable+xroi;
    154 % ytable=ytable+yroi;
    155 %
    156 % utable(utable>ibx/1.5)=NaN;
    157 % vtable(utable>ibx/1.5)=NaN;
    158 % vtable(vtable>iby/1.5)=NaN;
    159 % utable(vtable>iby/1.5)=NaN;
    160 
    161 function [vector] = SUBPIXGAUSS (result_conv,ibx,iby,x,y,SubPixOffset)
     137        sum_square=sum(sum(image1_crop.*image1_crop));
     138        ctable(ivec)=corrmax/sum_square;
     139end
     140result_conv=result_conv/255;
     141
     142
     143function [vector] = SUBPIXGAUSS (result_conv,x,y)
     144
    162145if size(x,1)>1 %if there are more than 1 peaks just take the first
    163146    x=x(1:1);
     
    177160    f2 = log(result_conv(y,x+1));
    178161    peakx = x+ (f1-f2)/(2*f1-4*f0+2*f2);
    179     %
    180     SubpixelX=peakx-(ibx/2)-SubPixOffset;
    181     SubpixelY=peaky-(iby/2)-SubPixOffset;
    182     vector=[SubpixelX, SubpixelY];
     162    [npy,npx]=size(result_conv);
     163    vector=[peakx-floor(npx/2)-1 peaky-floor(npy/2)-1];
    183164else
    184165    vector=[NaN NaN];
    185166end
    186167
    187 function [vector] = SUBPIX2DGAUSS (result_conv,ibx,iby,x,y,SubPixOffset)
     168function [vector] = SUBPIX2DGAUSS (result_conv,x,y)
    188169if size(x,1)>1 %if there are more than 1 peaks just take the first
    189170    x=x(1:1);
     
    213194    c11=(1/4)*sum(sum(c11));
    214195    c20=(1/6)*sum(sum(c20));
    215     c02=(1/6)*sum(sum(c02));
    216     %c00=(1/9)*sum(sum(c00));
    217 
     196    c02=(1/6)*sum(sum(c02));
    218197    deltax=(c11*c01-2*c10*c02)/(4*c20*c02-c11^2);
    219198    deltay=(c11*c10-2*c01*c20)/(4*c20*c02-c11^2);
    220199    peakx=x+deltax;
    221200    peaky=y+deltay;
    222 
    223     SubpixelX=peakx-(ibx/2)-SubPixOffset;
    224     SubpixelY=peaky-(iby/2)-SubPixOffset;
    225     vector=[SubpixelX, SubpixelY];
     201   
     202    [npy,npx]=size(result_conv);
     203    vector=[peakx-floor(npx/2)-1 peaky-floor(npy/2)-1];
     204%     SubpixelX=peakx-(ibx/2)-SubPixOffset;
     205%     SubpixelY=peaky-(iby/2)-SubPixOffset;
     206%     vector=[SubpixelX, SubpixelY];
    226207else
    227208    vector=[NaN NaN];
Note: See TracChangeset for help on using the changeset viewer.