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%20Phase%20separation%20tutorial%0A%20%20%20%20In%20the%20first%20section%2C%20we%20load%20an%20artificial%20two-phase%20PIV%20image%20and%20then%20use%20different%20phase%20separation%20methods%20to%20discriminate%20the%20two%20phases%20and%20save%20them%20as%20separate%20images.%20This%20section%20aims%20to%20introduce%20different%20methods%20that%20are%20available.%0A%0A%20%20%20%20In%20the%20second%20section%2C%20we%20demonstrate%20phase%20separation%20on%20real%20images%20followed%20by%20actual%20PIV%20process%20as%20a%20more%20realistic%20example.%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%20Section%201%20-%20Introducing%20different%20methods%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%20Loading%20image%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%20from%20openpiv%20import%20tools%2C%20phase_separation%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20plt.gray()%0A%20%20%20%20return%20phase_separation%2C%20plt%2C%20tools%0A%0A%0A%40app.cell%0Adef%20_(plt%2C%20tools)%3A%0A%20%20%20%20two_phase_image%20%3D%20tools.imread('..%2Ftwo_phase_piv%2Fartificial_A.tif')%0A%20%20%20%20plt.figure(figsize%3D(10%2C10))%0A%20%20%20%20plt.imshow(two_phase_image)%0A%20%20%20%20plt.show()%0A%20%20%20%20return%20(two_phase_image%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%23%20Opening%20method%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(phase_separation%2C%20plt%2C%20two_phase_image)%3A%0A%20%20%20%20_big_particles_image%2C%20_small_particles_image%20%3D%20phase_separation.opening_method(two_phase_image%2C%2011%2C%20thresh_factor%3D1.05)%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(1%2C%202%2C%20figsize%3D(16%2C%2010))%0A%20%20%20%20_ax%5B0%5D.imshow(_big_particles_image%2C%20vmin%3D0%2C%20vmax%3D255)%0A%20%20%20%20_ax%5B1%5D.imshow(_small_particles_image%2C%20interpolation%3D'bicubic'%2C%20vmin%3D0%2C%20vmax%3D255)%0A%20%20%20%20plt.show()%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%20Median%20filter%20method%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(phase_separation%2C%20plt%2C%20two_phase_image)%3A%0A%20%20%20%20_big_particles_image%2C%20_small_particles_image%20%3D%20phase_separation.median_filter_method(two_phase_image%2C%2011)%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(1%2C%202%2C%20figsize%3D(16%2C%2010))%0A%20%20%20%20_ax%5B0%5D.imshow(_big_particles_image)%0A%20%20%20%20_ax%5B1%5D.imshow(_small_particles_image%2C%20vmin%3D0%2C%20vmax%3D255)%0A%20%20%20%20plt.show()%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%20Khalitov-Longmire%20method%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%20Using%20a%20simple%20size%20limit%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(phase_separation%2C%20plt%2C%20two_phase_image)%3A%0A%20%20%20%20%23%20Plot%20size%20distribution%0A%20%20%20%20plt.hist(%20phase_separation.get_particles_size_array(two_phase_image)%2C%2030)%0A%20%20%20%20plt.yscale('log')%0A%20%20%20%20plt.title('Particle%20size%20distribution')%0A%20%20%20%20plt.show()%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(phase_separation%2C%20plt%2C%20two_phase_image)%3A%0A%20%20%20%20%23%20Choose%20size%20limit%20%3D%20100%20%0A%20%20%20%20_big_particles_criteria%20%3D%20%7B'min_size'%3A%2075%7D%0A%20%20%20%20_small_particles_criteria%20%3D%20%7B'max_size'%3A%2075%7D%0A%20%20%20%20_big_particles_image%2C%20_small_particles_image%20%3D%20phase_separation.khalitov_longmire(two_phase_image%2C%20_big_particles_criteria%2C%20_small_particles_criteria)%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(1%2C%202%2C%20figsize%3D(16%2C%2010))%0A%20%20%20%20_ax%5B0%5D.imshow(_big_particles_image)%0A%20%20%20%20_ax%5B1%5D.imshow(_small_particles_image)%0A%20%20%20%20plt.show()%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%20Using%20size%20and%20brightness%20limits%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(phase_separation%2C%20plt%2C%20two_phase_image)%3A%0A%20%20%20%20%23%20First%20plot%20size-brightness%20map%0A%20%20%20%20sb_map%20%3D%20phase_separation.get_size_brightness_map(%20two_phase_image%20)%20%23%20It%20is%20possible%20to%20sum%20over%20maps%20of%20multiple%20images%0A%20%20%20%20plt.figure(figsize%3D(10%2C8))%0A%20%20%20%20plt.imshow(%20sb_map%2C%20interpolation%3D'nearest'%2C%20aspect%3D'auto'%2C%20origin%3D'lower'%2C%20cmap%3D'jet'%20)%0A%20%20%20%20plt.colorbar()%0A%20%20%20%20plt.xlabel('Brightness')%0A%20%20%20%20plt.ylabel('Size%20(px)')%0A%20%20%20%20plt.title('Signal%20density')%0A%20%20%20%20plt.show()%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(phase_separation%2C%20plt%2C%20two_phase_image)%3A%0A%20%20%20%20%23%20size-brightness%20rectangle%20regions%20%0A%20%20%20%20_big_particles_criteria%20%3D%20%7B'min_size'%3A%20100%2C%20'max_size'%3A%20350%2C%20'min_brightness'%3A%20100%2C%20'max_brightness'%3A%20180%7D%0A%20%20%20%20_small_particles_criteria%20%3D%20%7B'min_size'%3A%2025%2C%20'max_size'%3A%20100%2C%20'min_brightness'%3A%2030%2C%20'max_brightness'%3A%20100%7D%0A%20%20%20%20_big_particles_image%2C%20_small_particles_image%20%3D%20phase_separation.khalitov_longmire(two_phase_image%2C%20_big_particles_criteria%2C%20_small_particles_criteria)%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(1%2C%202%2C%20figsize%3D(16%2C%2010))%0A%20%20%20%20_ax%5B0%5D.imshow(_big_particles_image)%0A%20%20%20%20_ax%5B1%5D.imshow(_small_particles_image)%0A%20%20%20%20plt.show()%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%20Section%202%20-%20Practical%20example%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%20Loading%20image%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(plt%2C%20tools)%3A%0A%20%20%20%20raw_A%20%3D%20tools.imread('..%2Ftwo_phase_piv%2Freal_A.tif')%0A%20%20%20%20raw_B%20%3D%20tools.imread('..%2Ftwo_phase_piv%2Freal_B.tif')%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(1%2C%202%2C%20figsize%3D(16%2C%2010))%0A%20%20%20%20_ax%5B0%5D.imshow(raw_A)%0A%20%20%20%20_ax%5B1%5D.imshow(raw_B)%0A%20%20%20%20plt.show()%0A%20%20%20%20return%20raw_A%2C%20raw_B%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%20Separating%20phases%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(phase_separation%2C%20plt%2C%20raw_A%2C%20raw_B)%3A%0A%20%20%20%20solid_A%2C%20carrier_A%20%3D%20phase_separation.opening_method(raw_A%2C%207%2C%20thresh_factor%3D1.05)%0A%20%20%20%20solid_B%2C%20carrier_B%20%3D%20phase_separation.opening_method(raw_B%2C%207%2C%20thresh_factor%3D1.05)%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(2%2C%202%2C%20figsize%3D(16%2C%2010))%0A%20%20%20%20_ax%5B0%2C%200%5D.imshow(solid_A%2C%20vmin%3D0%2C%20vmax%3D255)%0A%20%20%20%20_ax%5B0%2C%201%5D.imshow(carrier_A%2C%20vmin%3D0%2C%20vmax%3D255)%0A%20%20%20%20_ax%5B1%2C%200%5D.imshow(solid_B%2C%20vmin%3D0%2C%20vmax%3D255)%0A%20%20%20%20_ax%5B1%2C%201%5D.imshow(carrier_A%2C%20vmin%3D0%2C%20vmax%3D255)%0A%20%20%20%20plt.show()%0A%20%20%20%20return%20carrier_A%2C%20carrier_B%2C%20solid_A%2C%20solid_B%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%20Carrier%20phase%20PIV%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%20from%20openpiv%20import%20validation%2C%20filters%2C%20scaling%2C%20pyprocess%0A%20%20%20%20import%20numpy%20as%20np%0A%0A%20%20%20%20return%20filters%2C%20np%2C%20pyprocess%2C%20scaling%2C%20validation%0A%0A%0A%40app.cell%0Adef%20_(carrier_A%2C%20carrier_B%2C%20np%2C%20pyprocess)%3A%0A%20%20%20%20winsize%20%3D%2048%20%23%20pixels%0A%20%20%20%20searchsize%20%3D%20128%20%20%23%20pixels%2C%20search%20in%20image%20B%0A%20%20%20%20overlap%20%3D%2024%20%23%20pixels%0A%20%20%20%20dt%20%3D%2020e-6%20%23%20sec%0A%20%20%20%20scaling_factor%20%3D%202500%20%23%20pixels%2Fmeter%0A%0A%20%20%20%20u0%2C%20v0%2C%20sig2noise%20%3D%20pyprocess.extended_search_area_piv(%20carrier_A.astype(np.int32)%2C%20carrier_B.astype(np.int32)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20subpixel_method%20%3D'parabolic'%2C%20window_size%3Dwinsize%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20overlap%3Doverlap%2C%20dt%3Ddt%2C%20search_area_size%3Dsearchsize%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sig2noise_method%3D'peak2peak'%20)%0A%20%20%20%20x%2C%20y%20%3D%20pyprocess.get_coordinates(carrier_A.shape%2Csearchsize%2Coverlap)%0A%20%20%20%20return%20dt%2C%20scaling_factor%2C%20sig2noise%2C%20u0%2C%20v0%2C%20x%2C%20y%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20filters%2C%0A%20%20%20%20scaling%2C%0A%20%20%20%20scaling_factor%2C%0A%20%20%20%20sig2noise%2C%0A%20%20%20%20tools%2C%0A%20%20%20%20u0%2C%0A%20%20%20%20v0%2C%0A%20%20%20%20validation%2C%0A%20%20%20%20x%2C%0A%20%20%20%20y%2C%0A)%3A%0A%20%20%20%20mask%20%3D%20validation.sig2noise_val(sig2noise%2C%20threshold%3D3.2)%0A%20%20%20%20u2%2C%20v2%20%3D%20filters.replace_outliers(u0%2C%20v0%2C%20mask%2C%20method%3D'localmean'%2C%20max_iter%3D5%2C%20kernel_size%3D2)%0A%20%20%20%20x_1%2C%20y_1%2C%20u3%2C%20v3%20%3D%20scaling.uniform(x%2C%20y%2C%20u2%2C%20v2%2C%20scaling_factor%3Dscaling_factor)%0A%20%20%20%20x_1%2C%20y_1%2C%20u3%2C%20v3%20%3D%20tools.transform_coordinates(x_1%2C%20y_1%2C%20u3%2C%20v3)%0A%20%20%20%20tools.save('data.txt'%2C%20x_1%2C%20y_1%2C%20u3%2C%20v3%2C%20mask)%0A%20%20%20%20tools.save('preview.txt'%2C%20x_1%5B%3A%3A5%2C%20%3A%3A5%5D%2C%20y_1%5B%3A%3A5%2C%20%3A%3A5%5D%2C%20u3%5B%3A%3A5%2C%20%3A%3A5%5D%2C%20v3%5B%3A%3A5%2C%20%3A%3A5%5D%2C%20mask%5B%3A%3A5%2C%20%3A%3A5%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(plt%2C%20scaling_factor%2C%20tools)%3A%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(16%2C%2010))%0A%20%20%20%20tools.display_vector_field('preview.txt'%2C%20on_img%3DTrue%2C%20image_name%3D'..%2Ftwo_phase_piv%2Freal_A.tif'%2C%20scaling_factor%3Dscaling_factor%2C%20ax%3D_ax)%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%20Solid%20phase%20PTV%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(dt%2C%20np%2C%20plt%2C%20scaling_factor%2C%20solid_A%2C%20solid_B%2C%20tools)%3A%0A%20%20%20%20%23%20Note%20that%20this%20is%20an%20over-simplified%20PTV%20code%2C%20just%20for%20demonstration.%0A%20%20%20%20%23%20Please%20use%20OpenPTV%20or%20other%20PTV%20software%20for%20meaningful%20results.%0A%20%20%20%20from%20skimage.measure%20import%20label%2C%20regionprops%0A%20%20%20%20%23%20'%25matplotlib%20inline'%20command%20supported%20automatically%20in%20marimo%0A%20%20%20%20%23%20magic%20command%20not%20supported%20in%20marimo%3B%20please%20file%20an%20issue%20to%20add%20support%0A%20%20%20%20%23%20%25config%20InlineBackend.close_figures%3DFalse%0A%20%20%20%20from%20scipy.spatial.distance%20import%20cdist%0A%20%20%20%20from%20IPython.display%20import%20clear_output%0A%20%20%20%20centers_A%20%3D%20np.asarray(%5Bp.centroid%20for%20p%20in%20regionprops(label(solid_A%20%3E%202%20*%20np.mean(solid_A)))%5D)%0A%20%20%20%20centers_B%20%3D%20np.asarray(%5Bp.centroid%20for%20p%20in%20regionprops(label(solid_B%20%3E%202%20*%20np.mean(solid_B)))%5D)%0A%20%20%20%20cross_distance%20%3D%20cdist(centers_B%2C%20centers_A)%0A%20%20%20%20PTV_X%20%3D%20%5B%5D%0A%20%20%20%20PTV_Y%20%3D%20%5B%5D%0A%20%20%20%20PTV_U%20%3D%20%5B%5D%0A%20%20%20%20PTV_V%20%3D%20%5B%5D%0A%20%20%20%20for%20i%20in%20range(1%2C%20len(centers_A))%3A%0A%20%20%20%20%20%20%20%20if%20np.min(cross_distance%2C%200)%5Bi%5D%20%3C%205%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20j%20%3D%20np.argmin(cross_distance%2C%200)%5Bi%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20PTV_X.append(centers_A%5Bi%2C%201%5D%20%2F%20scaling_factor)%0A%20%20%20%20%20%20%20%20%20%20%20%20PTV_Y.append(centers_A%5Bi%2C%200%5D%20%2F%20scaling_factor)%0A%20%20%20%20%20%20%20%20%20%20%20%20PTV_U.append((centers_B%5Bj%2C%201%5D%20-%20centers_A%5Bi%2C%201%5D)%20%2F%20(scaling_factor%20*%20dt))%0A%20%20%20%20%20%20%20%20%20%20%20%20PTV_V.append(-(centers_B%5Bj%2C%200%5D%20-%20centers_A%5Bi%2C%200%5D)%20%2F%20(scaling_factor%20*%20dt))%0A%20%20%20%20PTV_X%20%3D%20np.asarray(PTV_X)%0A%20%20%20%20PTV_Y%20%3D%20np.asarray(PTV_Y)%0A%20%20%20%20PTV_U%20%3D%20np.asarray(PTV_U)%0A%20%20%20%20PTV_V%20%3D%20np.asarray(PTV_V)%0A%20%20%20%20width%20%3D%20solid_A.shape%5B1%5D%20%2F%20scaling_factor%0A%20%20%20%20height%20%3D%20solid_A.shape%5B0%5D%20%2F%20scaling_factor%0A%20%20%20%20fig2%2C%20ax2%20%3D%20plt.subplots(figsize%3D(16%2C%2010))%0A%20%20%20%20ax2.imshow(solid_A%2C%20extent%3D%5B0%2C%20width%2C%200%2C%20height%5D%2C%20origin%3D'lower')%0A%20%20%20%20tools.display_vector_field('preview.txt'%2C%20scaling_factor%3Dscaling_factor%2C%20ax%3Dax2)%0A%20%20%20%20ax2.invert_yaxis()%0A%20%20%20%20ax2.quiver(PTV_X%2C%20PTV_Y%2C%20PTV_U%2C%20PTV_V%2C%20color%3D'yellow'%2C%20width%3D0.003)%0A%20%20%20%20ax2.invert_yaxis()%0A%20%20%20%20clear_output(wait%3DTrue)%0A%20%20%20%20plt.show()%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
d0cd92b1c6b168cb62a6bebccadab2c2