Shrediquette / PIVlab

Particle Image Velocimetry for Matlab, official repository
https://shrediquette.github.io/PIVlab/
MIT License
126 stars 30 forks source link

How to mask moving boundaries #90

Closed Leiot01 closed 11 months ago

Shrediquette commented 1 year ago

Here is some code that I wrote that masks shadows and also reflections dynamically:

clear clc close all foldername='C:\Users\trash\Desktop\PIV_DATA\flugel\5m_s_4Hz_10deg\'; tic for i=0:1599

    %% load both images
    filename1=['PIVlab_' sprintf('%4.4d',i) '_A.tif']
    filename2=['PIVlab_' sprintf('%4.4d',i) '_B.tif'];
    outfilename=['PIVlab_MASK_' sprintf('%4.4d',i) '.tif'];

    A1=imread(fullfile(foldername,filename1));
    A2=imread(fullfile(foldername,filename2));
    A=uint8((double(A1)+double(A2))/2); %add both images

    %% shadow mask
    B=medfilt2(A,[10 10]);
    B=im2bw(B,0.00001);
    B=imclose(B,strel('disk',25)); %close holes
    B=imopen(B,strel('disk',50)); %remove unconnected islands

    %% reflection mask
    D=im2bw(A,0.1);
    E=imclose(D,strel("rectangle",[25*3 50*3]));
    E=imdilate(E,strel("rectangle",[25 50]));
    E=imclose(E,strel("rectangle",[25 50]));
    E=imerode(E,strel("rectangle",[10 10]));
    E=imopen(E,strel('disk',10));
    E=1-E;

    %% combine shdow + reflection
    G=im2bw(E+B,1);
    G=imopen(G,strel('disk',25));

    mask=1-G;
    imwrite(mask,fullfile(foldername,outfilename));

end toc

PIVlab_0000_A.zip