import%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App()%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Compare%20original%20median%20filter%20with%20normalized%20median%20filter.%0A%20%20%20%20Once%20a%20velocity%20field%20has%20been%20obtained%20from%20a%20PIV%20processor%2C%20there%20might%20be%20vectors%20that%2C%20visually%2C%20look%20as%20outliers.%20One%20way%20to%20get%20rid%20of%20them%20is%20to%20apply%20a%20median%20filter%3A%20compare%20every%20velocity%20vector%20to%20the%20average%20vector%20in%20its%2C%20say%2C%203x3%20neighborhoud%20and%2C%20if%20it%20is%20bigger%20than%20a%20threshold%2C%20replace%20it%20with%20the%20average%20vector.%0A%0A%20%20%20%20As%20described%20in%20Adrain%20%26%20Westerweel%2C%20%22Particle%20image%20velocimetry%22%2C%202011%2C%20J.%20Westerweel%20and%20coauthors%20created%20two%20versions%20of%20the%20median%20filter.%20The%20original%20one%20was%20created%20in%201994.%20Its%20main%20disadvantage%20is%20that%20it%20uses%20one%20(global)%20threshold%20for%20all%20the%20vectors%20in%20the%20velocity%20field.%20In%202005%2C%20it%20was%20proposed%20to%20normalize%20every%20vector%20in%20the%20velocity%20field%20before%20comparing%20it%20to%20the%20global%20threshold%2C%20thereby%20mitigating%20the%20disadvantage%20of%20the%201994%20version%20of%20the%20median%20filter%20(see%20J.%20Westerweel%2C%20F.%20Scarano%2C%20%22Universal%20outlier%20detection%20for%20PIV%20data%22%2C%20Experiments%20in%20fluids%2C%2039(6)%2C%20p.1096-1100%2C%202005).%0A%0A%20%20%20%20OpenPIV%20has%20implemented%20both%20versions%20of%20the%20filter.%20The%201994%20version%20(the%20original%20version)%20is%20given%20by%20the%20function%20%60validation.local_median_val()%60.%20The%202005%20version%20(the%20normalized%20version)%20is%20given%20by%20the%20function%20%60validation.local_norm_median_val()%60.%0A%0A%20%20%20%20The%20phylosophy%20of%20their%20usage%20is%20the%20following.%20Both%20filters%20just%20check%20every%20vector%20in%20the%20velocity%20field%20and%20create%20a%20%22mask%22%20of%20the%20velocity%20field%20where%20those%20vectors%20that%20didn't%20pass%20the%20threshold%20requirement%20are%20marked%20as%20NaNs.%20Then%20the%20OpenPIV%20function%20%60filters.replace_outliers()%60%20must%20be%20implemented.%20That%20function%20reads%20the%20%22mask%22%20and%20replace%20every%20NaN%20vector%20with%20the%20average%20of%20its%20neighbourhood.%0A%0A%20%20%20%20The%20purpose%20of%20this%20tutorial%20is%20to%20compare%20the%20two%20fitlers%20for%20a%20rather%20difficult%20case%20of%20PIV%20of%20a%20two-phase%20cap-bubbly%20air-water%20upward%20flow.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20cv2%20%23%20conda%20install%20opencv%3B%20alternatively%20use%20imread()%20from%20openpiv%20tools%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20from%20openpiv%20import%20tools%2C%20pyprocess%2C%20preprocess%2C%20validation%2C%20filters%2C%20scaling%0A%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20cv2%2C%0A%20%20%20%20%20%20%20%20filters%2C%0A%20%20%20%20%20%20%20%20np%2C%0A%20%20%20%20%20%20%20%20plt%2C%0A%20%20%20%20%20%20%20%20preprocess%2C%0A%20%20%20%20%20%20%20%20pyprocess%2C%0A%20%20%20%20%20%20%20%20scaling%2C%0A%20%20%20%20%20%20%20%20tools%2C%0A%20%20%20%20%20%20%20%20validation%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Now%20let's%20write%20a%20function%20that%20does%20basic%20PIV%20processing%20on%20a%20pair%20of%20PIV%20frames.%20The%20function%20will%20calculate%20correlations%20with%20%60pyprocess.fft_correlate_images()%60%2C%20then%20it%20will%20convert%20correlations%20to%20displacements%20with%20%60pyprocess.correlation_to_displacement%60%20and%2C%20finally%2C%20it%20will%20validate%20the%20velocity%20field%20and%20replace%20the%20outliers.%20It%20is%20very%20imporatant%20to%20understand%20what%20the%20right%20strategy%20is%20for%20the%20validation%20and%20replacement%20of%20the%20outliers.%20There%20are%20two%20types%20of%20validation%3A%20validation%20of%20the%20correlations%20and%20validation%20of%20the%20velocity%20field.%20Validation%20of%20the%20correlations%20makes%20sure%20that%20the%20largest%20correlation%20peak%20is%20much%20bigger%20than%20the%20rest%20of%20the%20peaks%2C%20thereby%20making%20sure%20that%20the%20largest%20correlation%20peak%20is%20not%20just%20noice%20but%20a%20valid%20signal.%20Once%20the%20correlations%20have%20been%20validated%20this%20way%2C%20one%20goes%20ahead%20and%20calculates%20the%20velocity%20field.%20Surprisingly%2C%20one%20can%2C%20still%2C%20obtain%20a%20velocity%20field%20with%20outliers%20(some%20vectors%20are%20much%20bigger%20or%20point%20in%20the%20direction%20different%20than%20the%20vectors%20surrounding%20it).%20I.e.%2C%20correlations%20validation%20doesn't%20give%20a%20100%25%20warranty%20that%20the%20resultant%20velocity%20field%20will%20be%20absolutely%20accurate.%20So%2C%20one%20goes%20ahead%20and%20validates%20the%20velocity%20field%20with%20a%20median%20filter%20which%20compares%20every%20vector%20to%20several%20of%20its%20surrounding%20vectors.%20Both%20validation%20types%20just%20mark%20the%20invalid%20vectors.%20Then%20the%20replacement%20function%20goes%20through%20all%20the%20marked%20vectors%20and%20replaces%20every%20one%20of%20them%20with%20the%20average%20of%20its%20surrounding%20vectors.%20Now%20imagine%20if%20the%20majority%20of%20the%20vectors%20have%20been%20marked%20as%20invalid.%20In%20this%20case%2C%20the%20chance%20of%20getting%20invalid%20vectors%20in%20the%20surroundings%20of%20other%20invalid%20vectors%20is%20big.%20And%20we%20end%20up%20with%20completely%20%22remodeled%22%20velocity%20field%20where%20almost%20every%20vector%20has%20been%20replaced%20with%20the%20average%20of%20its%20surroundings.%20I.e.%2C%20the%20resultant%20field%20is%20not%20physical.%20Obviously%2C%20if%20you%20choose%20to%20do%20both%20validations%20types%20(which%2C%20very%20well%2C%20can%20be%20the%20case)%2C%20then%20you%20have%20invalid%20vectors%20due%20to%20correlations%20and%20due%20to%20outliers.%20Of%20course%2C%20some%20of%20them%20may%20be%20the%20same%2C%20but%20some%20of%20them%20may%20not.%20I.e.%2C%20you%20increasing%20the%20number%20of%20invalid%20vectors%20for%20further%20replacement%20procedure.%20You%20want%20to%20avoid%20it.%20In%20order%20to%20avoid%20it%2C%20do%20validation-replacement%20in%20steps.%20First%2C%20validate%20correlations%20and%20replace%20them%20at%20once.%20Second%2C%20validate%20outliers%20and%20replace%20them%20at%20once.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20cv2%2C%0A%20%20%20%20filters%2C%0A%20%20%20%20np%2C%0A%20%20%20%20plt%2C%0A%20%20%20%20preprocess%2C%0A%20%20%20%20pyprocess%2C%0A%20%20%20%20scaling%2C%0A%20%20%20%20tools%2C%0A%20%20%20%20validation%2C%0A)%3A%0A%20%20%20%20def%20PIVanalysis(dataArray%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20imageShape%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20searchAreaSize%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20overlap%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20scalingFactor%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20deltaT%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20maskBubble%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20nameTXT%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20medianTest%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20numRuns%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20onImg%20%3D%20None%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20s2nThresh%20%3D%200.8%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%3A%0A%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20The%20procedure%20is%20adapted%20from%20ensemble_correlation.ipynb%20OpenPIV%20tutorial.%0A%20%20%20%20%20%20%20%20Parameters%3A%20dataArray%20(numpy%20array)%20-%20an%20array%20of%20PIV%20images%20that%20contains%20only%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20one%20PIV%20pair%2C%20its%20structure%20is%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20dataArray%20%3D%20np.asarray(%5BframeA%2C%20frameB%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20onImg%20(cv2.imread)%20-%20an%20image%20to%20plot%20the%20velocity%20fields%20on%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20searchAreaSize%20(int)%20-%20is%20the%20size%20of%20the%20interrogation%20window%20on%20frameB%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20it%20can%20be%20a%20bit%20bigger%2C%20which%2C%20reportedly%2C%20is%20better%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20PIV%20pair%20with%20high%20dynamic%20range%20(like%20in%20our%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20case%2C%20where%20velocities%20in%20the%20bubble%20wake%20are%20much%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20bigger%20than%20velocities%20closer%20to%20the%20walls%20of%20the%20test%20section%20-%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20the%20range%20of%20velocities%20(i.e.%2C%20dynamic%20range)%20is%20big)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20maskBubble%20(numpy%20array)%20-%20a%20binary%20image%20where%20bubbles%20are%20white%20and%20background%20is%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20black%3B%20is%20used%20to%20mask%20out%20bubbles%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20deltaT%20(float)%20-%20s%2C%20the%20time%20between%20the%20two%20PIV%20frames%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20scalingFactor%20(float)%20-%20pix%2Fmm%2C%20scaling%20factor%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20nameTXT%20(str)%20-%20.txt%2C%20the%20name%20of%20the%20file%20to%20save%20the%20resultant%20OpenPIV%20velocity%20field%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20medianTest%20(int)%20-%20can%20be%20either%201994%20or%202005%3B%201994%20refers%20to%20original%20Westerweel's%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20median%20test%20given%20by%20the%20function%20validation.local_median_val()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%202005%20refers%20to%20the%20normalized%20Westerweel's%20median%20test%20given%20by%20the%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20function%20validation.local_norm_median_val()%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20numRuns%20(int)%20-%20can%20be%201%20or%202%3B%20the%20number%20of%20times%20to%20run%20the%20normalized%20median%20filter%0A%20%20%20%20%20%20%20%20Returns%3A%20None%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%23%20Mask%20out%20the%20bubbles.%0A%20%20%20%20%20%20%20%20dataArray%5B0%5D%20%3D%20cv2.bitwise_and(dataArray%5B0%5D%2C%20dataArray%5B0%5D%2C%20mask%3Dcv2.bitwise_not(maskBubble))%0A%20%20%20%20%20%20%20%20dataArray%5B1%5D%20%3D%20cv2.bitwise_and(dataArray%5B1%5D%2C%20dataArray%5B1%5D%2C%20mask%3Dcv2.bitwise_not(maskBubble))%0A%0A%20%20%20%20%20%20%20%20%23%20Find%20the%20correlations%20map.%0A%20%20%20%20%20%20%20%20%23%20%22linear%22%20correlation_method%20together%20with%20normalized_correlation%3DTrue%0A%20%20%20%20%20%20%20%20%23%20helps%20to%20boost%20the%20s2n%20threshold%20from%201.003%20to%202%20for%2095%25%20valid%20vectors.%0A%20%20%20%20%20%20%20%20%23%20See%20OpenPIV%20Jupiter%20notebook%20compare_correlations_algorithms.ipynb%20for%20%0A%20%20%20%20%20%20%20%20%23%20why%20correlation_method%20is%20chosen%20to%20be%20'linear'.%0A%20%20%20%20%20%20%20%20corrs%20%3D%20pyprocess.fft_correlate_images(%0A%20%20%20%20%20%20%20%20%20%20%20%20pyprocess.moving_window_array(dataArray%5B0%5D%2CsearchAreaSize%2Coverlap)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20pyprocess.moving_window_array(dataArray%5B1%5D%2CsearchAreaSize%2Coverlap)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20normalized_correlation%3DTrue%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20correlation_method%3D%22linear%22%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%20%20%20%20mesh%20%3D%20pyprocess.get_field_shape(imageShape%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20search_area_size%20%3D%20searchAreaSize%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20overlap%20%3D%20overlap%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%20%20%20%20nrows%2C%20ncols%20%3D%20mesh%5B0%5D%2C%20mesh%5B1%5D%0A%0A%20%20%20%20%20%20%20%20%23%20Having%20found%20correlations%2C%20we%20can%20find%20corresponding%20displacements%20in%20pix.%0A%20%20%20%20%20%20%20%20%23%20IMPOTANT%3A%20OpenPIV%20uses%20u%20and%20v%20for%20displacements%20as%20well%20as%20for%20velocities%20in%0A%20%20%20%20%20%20%20%20%23%20Since%20that's%20confusing%2C%20we%20are%20going%20to%20use%20xDisp%20and%20yDisp%20for%20%0A%20%20%20%20%20%20%20%20%23%20displacements%20and%20u%20and%20v%20for%20velocities.%0A%20%20%20%20%20%20%20%20xDisp%2C%20yDisp%20%3D%20pyprocess.correlation_to_displacement(corrs%2C%20nrows%2C%20ncols)%0A%0A%20%20%20%20%20%20%20%20%23%20Having%20found%20displacements%20in%20pix%2C%20we%20can%20find%20corresponding%20velocities%20in%0A%20%20%20%20%20%20%20%20%23%20pix%2Ftime%0A%20%20%20%20%20%20%20%20u%20%3D%20xDisp%2FdeltaT%20%23%20pix%2Fs%0A%20%20%20%20%20%20%20%20v%20%3D%20yDisp%2FdeltaT%20%23%20pix%2Fs%0A%0A%20%20%20%20%20%20%20%20x%2C%20y%20%3D%20pyprocess.get_coordinates(imageShape%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20search_area_size%20%3D%20searchAreaSize%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20overlap%20%3D%20overlap%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%20%20%20%20%23%20Mask%20out%20the%20areas%20without%20the%20flow%20right%20now%20(bubbles).%0A%20%20%20%20%20%20%20%20grid_mask%20%3D%20preprocess.prepare_mask_on_grid(x%2C%20y%2C%20maskBubble%5B%3A%2C%3A%3A-1%5D)%0A%20%20%20%20%20%20%20%20masked_u%20%3D%20np.ma.masked_array(u%2C%20mask%3Dgrid_mask)%0A%20%20%20%20%20%20%20%20masked_v%20%3D%20np.ma.masked_array(v%2C%20mask%3Dgrid_mask)%0A%0A%20%20%20%20%20%20%20%20uShape%20%3D%20(masked_u.T).shape%0A%0A%20%20%20%20%20%20%20%20%23%20Now%20let's%20do%20validation.%20Pay%20attention%2C%20that%20validation%20doesn't%20mean%20automatic%0A%20%20%20%20%20%20%20%20%23%20replacement%20of%20the%20outliers%2C%20it%20means%20that%20we%20are%20going%20to%20detect%20the%20outliers%0A%20%20%20%20%20%20%20%20%23%20and%20mark%20them%20as%20outliers.%20Their%20replacement%20is%20a%20separate%20procedure%20and%20will%0A%20%20%20%20%20%20%20%20%23%20be%20done%20next.%0A%0A%20%20%20%20%20%20%20%20%23%20Do%20the%20validation%20of%20the%20correlation%20peaks.%0A%20%20%20%20%20%20%20%20corrs%20%3D%20corrs.astype('float64')%0A%20%20%20%20%20%20%20%20s2n%20%3D%20pyprocess.sig2noise_ratio(corrs%2C%20%22peak2mean%22)%0A%20%20%20%20%20%20%20%20invalid_mask_s2n%20%3D%20validation.sig2noise_val(s2n%2C%20threshold%20%3D%20s2nThresh)%0A%20%20%20%20%20%20%20%20%23%20Now%20we%20can%20replace%20outliers%20flagged%20by%20the%20invalid_mask_s2n.%0A%20%20%20%20%20%20%20%20masked_u%2C%20masked_v%20%3D%20filters.replace_outliers(masked_u.flatten()%2C%20masked_v.flatten()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20invalid_mask_s2n%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20method%20%3D%20'localmean'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20max_iter%20%3D%203%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20kernel_size%20%3D%2017%20%0A%20%20%20%20%20%20%20%20)%20%23%20IMPORTANT%20to%20flatten%20u%20and%20v%0A%0A%20%20%20%20%20%20%20%20%23%20Do%20the%20validation%20based%20on%20the%20median%20test%20(as%20described%20in%20the%20%0A%20%20%20%20%20%20%20%20%23%20German%20PIV%20book%20on%20p.185).%20We%20can%20do%20it%20in%20two%20ways.%20First%20-%20use%0A%20%20%20%20%20%20%20%20%23%20the%20original%20Westerweel's%20median%20test%20as%20implemented%20in%20OpenPIV's%20%0A%20%20%20%20%20%20%20%20%23%20function%20validation.local_median_val.%20Second%20-%20use%20the%20improved%20(normalized)%0A%20%20%20%20%20%20%20%20%23%20Westerweel's%20median%20test%20which%20is%20implemented%20in%20OpenPIV's%20function%0A%20%20%20%20%20%20%20%20%23%20validation.local_norm_median_val.%0A%20%20%20%20%20%20%20%20%23%20The%20problem%20is%20that%20%20filters.replace_outliers()%20returned%20flattened%20arrays%20on%0A%20%20%20%20%20%20%20%20%23%20the%20previous%20step.%20But%20both%20validation.local_median_val()%20and%20%0A%20%20%20%20%20%20%20%20%23%20validation.local_norm_median_val()%20require%202D%20arrays%20as%20the%20input.%20The%20way%20to%0A%20%20%20%20%20%20%20%20%23%20transfrom%20the%20flattened%20arrays%20to%20the%202D%20arrays%20is%20the%20following%3A%0A%20%20%20%20%20%20%20%20masked_u%20%3D%20np.reshape(masked_u%2C(uShape%5B0%5D%2CuShape%5B1%5D))%20%23%20a%202D%20array%0A%20%20%20%20%20%20%20%20masked_v%20%3D%20np.reshape(masked_v%2C(uShape%5B0%5D%2CuShape%5B1%5D))%20%23%20a%202D%20array%0A%20%20%20%20%20%20%20%20if%20medianTest%20%3D%3D%201994%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20ORIGINAL%20WESTERWEEL'S%20MEDIAN%20TEST.%0A%20%20%20%20%20%20%20%20%20%20%20%20invalid_mask_median%20%3D%20validation.local_median_val(masked_u%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20masked_v%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20u_threshold%3D4000%2C%20%23%20abs%20difference%20with%20the%20mean%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20v_threshold%3D4000%2C%20%23%20abs%20difference%20with%20the%20mean%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20size%3D2%20%23%20e.g.%2C%20size%3D2%20is%20a%205x5%20kernel%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Now%20we%20can%20replace%20outliers%20flagged%20by%20the%20invalid_mask_median.%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20masked_u%2C%20masked_v%20%3D%20filters.replace_outliers(masked_u.flatten()%2C%20masked_v.flatten()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20invalid_mask_median.flatten()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20method%20%3D%20'localmean'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20max_iter%20%3D%205%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20kernel_size%20%3D%202%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20)%20%23%20IMPORTANT%20to%20flatten%20u%20and%20v%0A%20%20%20%20%20%20%20%20elif%20medianTest%20%3D%3D%202005%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20NORMALIZED%20WESTERWEEL'S%20MEDIAN%20TEST.%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20One%20doesn't%20have%20to%20restrict%20oneself%20to%20just%20one%20instance%20of%20the%20median%20test.%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20For%20this%20particular%20dataset%2C%20after%20an%20extensive%20test%20of%20different%20parameters%2C%20it%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20was%20concluded%20that%20two%20median%20tests%20are%20necessary.%20The%20reason%20for%20that%20was%20the%20following.%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20The%20velocity%20field%20had%20a%20lot%20of%20very%20obvious%20outliers%20close%20to%20each%20other.%20Yet%20it%20also%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20had%20less%20obvious%20outliers%20sitting%20far%20away%20from%20each%20other.%20The%20first%20median%20filter%20uses%20a%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20bigger%20kernel%20size%20which%20is%20bigger%20than%20the%20width%20of%20the%20set%20of%20the%20neighbouring%20outliers.%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20I.e.%2C%20if%20I%20have%205%20outliers%20in%20a%20row%2C%20I%20want%20my%20kernels%20size%20to%20be%20bigger%20than%205%20because%2C%20in%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20this%20case%2C%20valid%20vectors%20will%20be%20included%20into%20the%20averaging%20procedure.%20If%20the%20kernel%20is%20less%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20than%205%2C%20it%20will%20include%20only%20the%20outliers%20into%20the%20averaging%20procedure%20resulting%20in%20no%20good.%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20I.e.%2C%20we%20will%20be%20compare%20the%20outlier%20to%20the%20average%20of%20the%20outliers%20around%20it%2C%20therefore%20we%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20will%20not%20detect%20it%20as%20the%20outlier%20because%2C%20obviously%2C%20it%20will%20be%20on%20the%20order%20of%20the%20average%20of%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20the%20outliers.%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20At%20the%20second%20step%2C%20when%20we%20are%20left%20with%20only%20less%20obvious%20outliers%20which%20are%20sittig%20far%20away%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20from%20each%20other%2C%20we're%20doing%20median%20filter%20with%20the%20smallest%20kernel%20size%20possible.%20Since%20the%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20outliers%20are%20less%20obvious%2C%20they%20can%20be%20barealy%20visually%20distinguishable%20from%20other%20vectors.%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20That%20is%20why%20the%20kernel%20size%20must%20be%20so%20small%3A%20it%20compares%20the%20outlier%20only%20to%20its%20immediate%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20neighbours%20and%20detects%20it%20as%20an%20outlier.%20If%20it%20compared%20the%20outlier%20to%20more%20vectors%2C%20the%20chance%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20that%20more%20vectors%20make%20the%20average%20of%20them%20bigger%20is%20high.%20And%20since%20we%20compare%20the%20outlier%20to%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20the%20average%20of%20the%20surrounding%20vectors%2C%20we%20might%20not%20detect%20it%20as%20an%20outlier.%20That's%20why%20we%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20we%20want%20the%20smallest%20kernel%20size%20in%20this%20case.%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Do%20the%20first%20run%20of%20the%20median%20test.%20%0A%20%20%20%20%20%20%20%20%20%20%20%20invalid_mask_median1%20%3D%20validation.local_norm_median_val(masked_u%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20masked_v%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%CE%B5%20%3D%200.2%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20threshold%20%3D%202.5%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20size%20%3D%202%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Now%20we%20can%20replace%20outliers%20flagged%20by%20the%20invalid_mask_median1.%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20masked_u%2C%20masked_v%20%3D%20filters.replace_outliers(masked_u.flatten()%2C%20masked_v.flatten()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20invalid_mask_median1.flatten()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20method%20%3D%20'localmean'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20max_iter%20%3D%205%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20kernel_size%20%3D%202%20%0A%20%20%20%20%20%20%20%20%20%20%20%20)%20%23%20IMPORTANT%20to%20flatten%20u%20and%20v%0A%20%20%20%20%20%20%20%20%20%20%20%20invalid_mask_median%20%3D%20invalid_mask_median1%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20numRuns%20%3D%3D%202%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20Do%20the%20second%20run%20of%20the%20median%20test.%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20Start%20with%20reshaping%20masked_u%20and%20masked_v%20to%202D%20arrays.%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20masked_u%20%3D%20np.reshape(masked_u%2C(uShape%5B0%5D%2CuShape%5B1%5D))%20%23%20a%202D%20array%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20masked_v%20%3D%20np.reshape(masked_v%2C(uShape%5B0%5D%2CuShape%5B1%5D))%20%23%20a%202D%20array%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20invalid_mask_median2%20%3D%20validation.local_norm_median_val(masked_u%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20masked_v%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%CE%B5%20%3D%200.2%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20threshold%20%3D%202.5%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20size%20%3D%201%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20masked_u%2C%20masked_v%20%3D%20filters.replace_outliers(masked_u.flatten()%2C%20masked_v.flatten()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20invalid_mask_median2.flatten()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20method%20%3D%20'localmean'%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20max_iter%20%3D%205%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20kernel_size%20%3D%201%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%20%23%20IMPORTANT%20to%20flatten%20u%20and%20v%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20When%20we%20will%20be%20saving%20the%20results%20into%20the%20OpenPIV%20.txt%20file%2C%20we%20will%20want%20to%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20know%20about%20all%20the%20invalid%20vectors.%20That's%20why%20we%20are%20combining%20the%20masks.%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20The%20way%20they%20are%20combined%20is%20copied%20from%20the%20code%20for%20OpenPIV.validation.typical_validation%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20line%20295.%20These%20are%2C%20actually%2C%20flags%2C%20not%20masks.%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20invalid_mask_median%20%3D%20invalid_mask_median1%20%7C%20invalid_mask_median2%0A%0A%20%20%20%20%20%20%20%20%23%20When%20we%20will%20be%20saving%20the%20results%20into%20the%20OpenPIV%20.txt%20file%2C%20we%20will%20want%20to%0A%20%20%20%20%20%20%20%20%23%20know%20about%20all%20the%20invalid%20vectors.%20That's%20why%20we%20are%20combining%20the%20masks.%0A%20%20%20%20%20%20%20%20%23%20The%20way%20they%20are%20combined%20is%20copied%20from%20the%20code%20for%20OpenPIV.validation.typical_validation%0A%20%20%20%20%20%20%20%20%23%20line%20295.%20These%20are%2C%20actually%2C%20flags%2C%20not%20masks.%0A%20%20%20%20%20%20%20%20invalid_mask%20%3D%20invalid_mask_s2n%20%7C%20invalid_mask_median.flatten()%0A%0A%20%20%20%20%20%20%20%20x%2C%20y%2C%20masked_u%2C%20masked_v%20%3D%20scaling.uniform(x%2C%20y%2C%20masked_u%2C%20masked_v%2C%20scaling_factor%20%3D%20scalingFactor)%0A%0A%20%20%20%20%20%20%20%20%23%20MASK%20OUT%20VELOCITY%20FIELD.%0A%20%20%20%20%20%20%20%20%23%20Before%20saving%20the%20field%20to%20a%20.txt%20file%2C%20give%20zeros%20to%20those%20vectors%20that%20lie%20in%20the%20masked%20regions.%0A%20%20%20%20%20%20%20%20%23%20Right%20now%2C%20x%20and%20y%20are%20in%20the%20image%20system%20of%20coordinates%3A%20x%20is%20to%20the%20right%2C%20y%20is%20downwards%2C%20(0%2C0)%0A%20%20%20%20%20%20%20%20%23%20is%20in%20the%20top%20left%20corner.%20It%20can%20be%20learnt%20from%20the%20GitHub%20code%20of%20tools.transform_coordinates.%0A%20%20%20%20%20%20%20%20%23%20Since%20our%20x%20and%20y%20are%20in%20mm%2C%20we're%20going%20to%20use%20scaling%20factor%20to%20convert%20them%20to%20pix.%20Then%20we're%20going%0A%20%20%20%20%20%20%20%20%23%20to%20use%20them%20to%20identify%20whether%20or%20not%20their%20place%20on%20an%20example%20masked%20image%20is%20masked.%0A%20%20%20%20%20%20%20%20xFlat%20%3D%20x.flatten()%0A%20%20%20%20%20%20%20%20yFlat%20%3D%20y.flatten()%0A%20%20%20%20%20%20%20%20for%20i%20in%20range(xFlat.size)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20maskBubble%5Bint(yFlat%5Bi%5D*scalingFactor)%2C%20int(xFlat%5Bi%5D*scalingFactor)%5D%20%3D%3D%20255%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20masked_u%5Bi%5D%20%3D%200%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20masked_v%5Bi%5D%20%3D%200%0A%0A%20%20%20%20%20%20%20%20x%2C%20y%2C%20masked_u%2C%20masked_v%20%3D%20tools.transform_coordinates(x%2C%20y%2C%20masked_u%2C%20masked_v)%0A%0A%20%20%20%20%20%20%20%20tools.save(nameTXT%2C%20x%2C%20y%2C%20masked_u%2C%20masked_v%2C%20invalid_mask)%0A%0A%20%20%20%20%20%20%20%20%23%20Calculate%20the%20percentage%20of%20the%20invalid%20vectors%20due%20to%20median%20validation.%0A%20%20%20%20%20%20%20%20invMedOutPer%20%3D%20(np.count_nonzero(invalid_mask_median)%20%2F%20invalid_mask_median.size)%20*%20100%20%23%20percent%20of%20the%20invalid%20vectors%0A%20%20%20%20%20%20%20%20%23%20Calculate%20the%20percentage%20of%20the%20invalid%20vectors%20due%20to%20sig2noise%20validation.%0A%20%20%20%20%20%20%20%20invSnrOutPer%20%3D%20(np.count_nonzero(invalid_mask_s2n)%20%2F%20invalid_mask_s2n.size)%20*%20100%20%23%20percent%20of%20the%20invalid%20vectors%0A%20%20%20%20%20%20%20%20%23%20Calculate%20the%20percentage%20of%20the%20invalid%20vectors%20due%20to%20both%20median%20and%20sig2noise%20validations.%0A%20%20%20%20%20%20%20%20invOutPer%20%3D%20(np.count_nonzero(invalid_mask)%20%2F%20invalid_mask.size)%20*%20100%20%23%20percent%20of%20the%20invalid%20vectors%0A%20%20%20%20%20%20%20%20print(%22%5CnPercentage%20of%20invalid%20vectors%3A%5CninvalidMedianOutliersPercent%3D%7B%3A.2f%7D%2C%5CninvalidS2NOutliersPercent%3D%7B%3A.2f%7D%2C%5CninvalitOutliersPercent%3D%7B%3A.2f%7D%22.format(invMedOutPer%2CinvSnrOutPer%2CinvOutPer))%0A%20%20%20%20%20%20%20%20print(%22%5CnThe%20resultant%20velocity%20field%20(red%20are%20vectors%20that%20were%20validated%20as%20outliers%5Cnand%20replaced%20with%20the%20average%20of%20their%20neighbouring%20vectors)%3A%22)%0A%20%20%20%20%20%20%20%20figure%2C%20_%20%3D%20tools.display_vector_field(filename%20%3D%20str(nameTXT)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20scaling_factor%20%3D%20scalingFactor%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20scale%20%3D%203500%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20on_img%20%3D%20True%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20show_invalid%20%3D%20True%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20image_name%20%3D%20onImg%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20plt.show()%0A%0A%20%20%20%20return%20(PIVanalysis%2C)%0A%0A%0A%40app.cell%0Adef%20_(cv2%2C%20np)%3A%0A%20%20%20%20%23%20Global%20variables%0A%20%20%20%20tPulse%20%3D%200.0015%20%23%20s%20-%20time%20between%20frame%20A%20and%20frame%20B%0A%20%20%20%20ol%20%3D%2016%20%23%20overlap%3A%20give%20it%20in%20pix%2C%20not%20%25%0A%20%20%20%20interW%20%3D%2032%20%23%20the%20size%20of%20the%20interrogation%20window%0A%20%20%20%20searchAS%20%3D%20np.round(1.1*interW).astype(np.uint32)%20%23%20the%20size%20of%20the%20interrogation%20window%20in%20frameB%20%0A%20%20%20%20sf%20%3D%201%2F0.052%20%23%20pix%2Fmm%20scaling%20factor%0A%0A%20%20%20%20%23%20The%20name%20of%20the%20OpenPIV%20txt%20file%20to%20save%20the%20results%20to.%0A%20%20%20%20fileTXT%20%3D%20'..%2Ftest21%2FbubblesOpenPIVtxtFile.txt'%0A%0A%20%20%20%20%23%20The%20file%20names%20of%20PIV%20frames%20and%20bubbles%20mask.%0A%20%20%20%20frameA%20%3D%20'..%2Ftest21%2FbubblesFrameA.png'%0A%20%20%20%20frameB%20%3D%20'..%2Ftest21%2FbubblesFrameB.png'%0A%20%20%20%20bubblesMaskFile%20%3D%20'..%2Ftest21%2FbubblesMask.png'%0A%0A%20%20%20%20pairPIV%20%3D%20np.asarray(%5Bcv2.imread(frameA%2C0)%2C%20cv2.imread(frameB%2C0)%5D)%20%23%200%20flag%20means%20read%20as%20gray%20scale%0A%20%20%20%20bubblesMask%20%3D%20cv2.imread(bubblesMaskFile%2C0)%20%23%200%20flag%20means%20read%20as%20gray%20scale%0A%20%20%20%20imageShape%20%3D%20pairPIV%5B0%5D.shape%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20bubblesMask%2C%0A%20%20%20%20%20%20%20%20bubblesMaskFile%2C%0A%20%20%20%20%20%20%20%20fileTXT%2C%0A%20%20%20%20%20%20%20%20imageShape%2C%0A%20%20%20%20%20%20%20%20ol%2C%0A%20%20%20%20%20%20%20%20pairPIV%2C%0A%20%20%20%20%20%20%20%20searchAS%2C%0A%20%20%20%20%20%20%20%20sf%2C%0A%20%20%20%20%20%20%20%20tPulse%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20PIVanalysis%2C%0A%20%20%20%20bubblesMask%2C%0A%20%20%20%20bubblesMaskFile%2C%0A%20%20%20%20fileTXT%2C%0A%20%20%20%20imageShape%2C%0A%20%20%20%20ol%2C%0A%20%20%20%20pairPIV%2C%0A%20%20%20%20searchAS%2C%0A%20%20%20%20sf%2C%0A%20%20%20%20tPulse%2C%0A)%3A%0A%20%20%20%20print(%22Using%20original%201994%20Westerweel's%20median%20filter%3A%5Cn%22)%0A%20%20%20%20PIVanalysis(dataArray%20%3D%20pairPIV%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20imageShape%20%3D%20imageShape%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20searchAreaSize%20%3D%20searchAS%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20overlap%20%3D%20ol%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20scalingFactor%20%3D%20sf%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20deltaT%20%3D%20tPulse%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20maskBubble%20%3D%20bubblesMask%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20nameTXT%20%3D%20fileTXT%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20medianTest%20%3D%201994%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20numRuns%20%3D%201%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20s2nThresh%20%3D%202%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20onImg%20%3D%20bubblesMaskFile%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20PIVanalysis%2C%0A%20%20%20%20bubblesMask%2C%0A%20%20%20%20bubblesMaskFile%2C%0A%20%20%20%20fileTXT%2C%0A%20%20%20%20imageShape%2C%0A%20%20%20%20ol%2C%0A%20%20%20%20pairPIV%2C%0A%20%20%20%20searchAS%2C%0A%20%20%20%20sf%2C%0A%20%20%20%20tPulse%2C%0A)%3A%0A%20%20%20%20print(%22Using%20normalized%202005%20Westerweel's%20median%20filter%20with%20one%20run%3A%5Cn%22)%0A%20%20%20%20PIVanalysis(dataArray%20%3D%20pairPIV%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20imageShape%20%3D%20imageShape%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20searchAreaSize%20%3D%20searchAS%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20overlap%20%3D%20ol%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20scalingFactor%20%3D%20sf%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20deltaT%20%3D%20tPulse%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20maskBubble%20%3D%20bubblesMask%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20nameTXT%20%3D%20fileTXT%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20medianTest%20%3D%202005%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20numRuns%20%3D%201%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20s2nThresh%20%3D%202%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20onImg%20%3D%20bubblesMaskFile%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20PIVanalysis%2C%0A%20%20%20%20bubblesMask%2C%0A%20%20%20%20bubblesMaskFile%2C%0A%20%20%20%20fileTXT%2C%0A%20%20%20%20imageShape%2C%0A%20%20%20%20ol%2C%0A%20%20%20%20pairPIV%2C%0A%20%20%20%20searchAS%2C%0A%20%20%20%20sf%2C%0A%20%20%20%20tPulse%2C%0A)%3A%0A%20%20%20%20print(%22Using%20normalized%202005%20Westerweel's%20median%20filter%20with%20two%20runs%3A%5Cn%22)%0A%20%20%20%20PIVanalysis(dataArray%20%3D%20pairPIV%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20imageShape%20%3D%20imageShape%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20searchAreaSize%20%3D%20searchAS%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20overlap%20%3D%20ol%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20scalingFactor%20%3D%20sf%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20deltaT%20%3D%20tPulse%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20maskBubble%20%3D%20bubblesMask%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20nameTXT%20%3D%20fileTXT%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20medianTest%20%3D%202005%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20numRuns%20%3D%202%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20s2nThresh%20%3D%202%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20onImg%20%3D%20bubblesMaskFile%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20See%20how%20the%20fields%20are%20becoming%20visually%20better%20starting%20from%20the%20classic%20median%20filter%20to%20the%20normalized%20median%20filter%20and%20to%20the%20double-ran%20normalized%20median%20filter.%20If%20one%20has%20several%20velocity%20fields%20in%20a%20row%20(i.e.%2C%20the%20time%20series%20of%20velocity%20fields)%20and%20wants%20to%20average%20them%20(along%20the%20time%20axis)%2C%20outliers%20might%20alter%20the%20average%20and%20one%20might%20need%20a%20bigger%20time%20series%20to%20get%20a%20more%20stable%20average.%20On%20the%20other%20hand%20when%20all%20the%20velocity%20fields%20don't%20have%20outliers%2C%20the%20average%20becomes%20more%20stable%20(doesn't%20need%20as%20big%20of%20a%20time%20series).%20Also%2C%20when%20one%20analyzes%20vortex%20dynamics%20(i.e.%2C%20calculates%20different%20derivatives%20within%20a%20single%20velocity%20field)%2C%20one%20should%20better%20have%20no%20outliers.%20This%20is%20why%20it%20is%20important%20to%20filter%20out%20the%20outliers%20and%20replace%20them%20with%20something%20better%2C%20like%20the%20average%20of%20the%20surrounding%20vectors.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Final%20note.%0A%20%20%20%20The%20parameters%20used%20in%20the%20function%20%60PIVanalysis()%60%20were%20carefully%20selected%20using%20trials%20and%20errors%20method.%20The%20time%20it%20takes%20to%20find%20the%20right%20parameters%20can%20vary%20greatly%3A%20from%20a%20couple%20of%20hours%20to%20a%20day.%0A%0A%20%20%20%20The%20filters%20have%20many%20parameters%2C%20changing%20some%20of%20them%20slightly%20can%20lead%20to%20drastic%20change%20in%20velocity%20fields.%20Therefore%2C%20one%20should%20spend%20time%20playing%20with%20the%20parameters%20in%20order%20got%20get%20the%20necessary%20experience.%20Go%20ahead%20and%20change%20the%20threshold%20parameter%20in%20either%20one%20of%20the%20normalized%20median%20filters%20runs%20from%20the%20current%202.5%20to%2C%20say%2C%201.1%20and%20witness%20with%20your%20own%20eyes%20how%20wrong%20the%20resultant%20velocity%20field%20becomes.%20This%20effect%20is%20explained%20at%20the%20beginning%20of%20this%20notebook.%0A%0A%20%20%20%20Then%20go%20ahead%20and%20play%20with%20the%20rest%20of%20the%20parameters%20of%20all%20the%20filters%20as%20well%20as%20with%20the%20parameters%20of%20the%20outliers%20replacing%20functions.%20Also%2C%20try%20reordering%20the%20validation%20methods%3A%20try%20doing%20signal%20to%20noise%20validatin%20after%20the%20median%20filter%20and%20see%20how%20that%20affects%20the%20resultant%20velocity%20field.%0A%0A%20%20%20%20This%20notebook%20examplifies%20the%20very%20basic%20PIV.%20Of%20course%2C%20for%20flows%20like%20this%20one%20more%20advanced%20PIV%20techniques%20are%20preferable%2C%20such%20as%20OpenPIV's%20%60windef.py%60%20which%20also%20has%20normalized%20median%20filter%20implemented.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
b52ad7b5f07dfaadbac1b3c077332014