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%20Linear%20vs%20Circular%2C%20Normalized%20vs%20non-normalized%20correlations%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%20%23%20we%20should%20test%20linear%20vs%20circular%20to%20understand%20the%20differences%0A%20%20%20%20%23%20the%20main%20difference%20in%20the%20new%20version%20was%20a%20bug%20as%20%0A%20%20%20%20%23%20the%20zero-padding%20for%20linear%20case%20does%20not%20work%20as%20long%20as%20you%20leave%20values%20on%20the%20%0A%20%20%20%20%23%20borders%20not%20zero.%20%0A%20%20%20%20%23%20see%20new%20normalize_intensity%20function%20that%20should%20take%20care%20of%20uneven%20illumination%0A%20%20%20%20%23%20both%20between%20the%20frames%20and%20at%20different%20regions%20of%20the%20image%20A%20or%20B%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%0A%20%20%20%20from%20skimage.registration%20import%20phase_cross_correlation%0A%20%20%20%20from%20skimage%20import%20feature%0A%20%20%20%20%23%20from%20skimage.feature.phase_cross_correlation%20import%20_upsampled_dft%0A%20%20%20%20from%20scipy.ndimage%20import%20fourier_shift%0A%20%20%20%20from%20skimage.exposure%20import%20match_histograms%0A%0A%20%20%20%20return%20fourier_shift%2C%20match_histograms%2C%20np%2C%20phase_cross_correlation%2C%20plt%0A%0A%0Aapp._unparsable_cell(%0A%20%20%20%20r%22%22%22%0A%20%20%20%20from%20openpiv.tools%20import%20imread%0A%20%20%20%20from%20pylab%20import%20*%0A%20%20%20%20%22%22%22%2C%0A%20%20%20%20name%3D%22_%22%0A)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20from%20scipy.fft%20import%20rfft2%2C%20rfftn%0A%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20from%20openpiv.pyprocess%20import%20find_subpixel_peak_position%0A%20%20%20%20from%20openpiv.pyprocess%20import%20normalize_intensity%2C%20fft_correlate_images%0A%0A%20%20%20%20return%20fft_correlate_images%2C%20find_subpixel_peak_position%0A%0A%0A%40app.cell%0Adef%20_(fourier_shift%2C%20imread%2C%20match_histograms%2C%20np)%3A%0A%20%20%20%20a%20%3D%20imread('..%2Ftest11%2FA001_1.tif')%0A%20%20%20%20%23%20b%20%3D%20imread('..%2Fdata%2FPIVChallenge2001_A%2FA001_2.tif')%0A%20%20%20%20a%20%3D%20a%5B%3A32%2C%3A32%5D.copy()%0A%20%20%20%20a%5B16%3A18%2C16%3A18%5D%20%3D%20255%0A%20%20%20%20%23%20b%20%3D%20b%5B%3A32%2C%3A32%5D%0A%0A%20%20%20%20%23%20should%20be%20in%20the%20order%20of%20y%2Cx%3A%0A%20%20%20%20%23%20so%20it's%20about%205%20pixels%20upwards%20and%20about%203%20pixels%20to%20the%20right%0A%20%20%20%20shift%20%3D%20(-12.035%2C%20-10.92)%0A%20%20%20%20%23%20The%20shift%20corresponds%20to%20the%20pixel%20offset%20relative%20to%20the%20reference%20image%0A%20%20%20%20b%20%3D%20fourier_shift(np.fft.fftn(a)%2C%20shift)%0A%20%20%20%20b%20%3D%20np.fft.ifftn(b).real%0A%20%20%20%20b%20%3D%20match_histograms(b%2Ca).astype('uint8')%0A%20%20%20%20%23%20b%20%3D%20b%20%2B%20np.linspace(10%2C85%2C32)%0A%20%20%20%20return%20a%2C%20b%2C%20shift%0A%0A%0A%40app.cell%0Adef%20_(a%2C%20colorbar%2C%20imshow)%3A%0A%20%20%20%20imshow(a)%0A%20%20%20%20colorbar()%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(b%2C%20colorbar%2C%20imshow)%3A%0A%20%20%20%20imshow(b)%0A%20%20%20%20colorbar()%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(a%2C%20b%2C%20np)%3A%0A%20%20%20%20%23%20like%20moving%20window%20with%20the%200th%20dimension%20the%20IW%20no.%0A%20%20%20%20a_1%20%3D%20a%5Bnp.newaxis%2C%20%3A%2C%20%3A%5D%0A%20%20%20%20b_1%20%3D%20b%5Bnp.newaxis%2C%20%3A%2C%20%3A%5D%0A%20%20%20%20return%20a_1%2C%20b_1%0A%0A%0A%40app.cell%0Adef%20_(a_1%2C%20b_1%2C%20fft_correlate_images)%3A%0A%20%20%20%20%23%20%25%25timeit%20%0A%20%20%20%20c1%20%3D%20fft_correlate_images(a_1%2C%20b_1%2C%20'circular'%2C%20normalized_correlation%3DFalse)%0A%20%20%20%20return%20(c1%2C)%0A%0A%0A%40app.cell%0Adef%20_(a_1%2C%20b_1%2C%20fft_correlate_images)%3A%0A%20%20%20%20%23%20%25%25timeit%0A%20%20%20%20c2%20%3D%20fft_correlate_images(a_1%2C%20b_1%2C%20'linear'%2C%20normalized_correlation%3DFalse)%0A%20%20%20%20return%20(c2%2C)%0A%0A%0A%40app.cell%0Adef%20_(a_1%2C%20b_1%2C%20fft_correlate_images)%3A%0A%20%20%20%20%23%20%25%25timeit%0A%20%20%20%20c3%20%3D%20fft_correlate_images(a_1%2C%20b_1%2C%20'circular'%2C%20normalized_correlation%3DTrue)%0A%20%20%20%20return%20(c3%2C)%0A%0A%0A%40app.cell%0Adef%20_(a_1%2C%20b_1%2C%20fft_correlate_images)%3A%0A%20%20%20%20%23%20%25%25timeit%0A%20%20%20%20c4%20%3D%20fft_correlate_images(a_1%2C%20b_1%2C%20'linear'%2C%20normalized_correlation%3DTrue)%0A%20%20%20%20return%20(c4%2C)%0A%0A%0A%40app.cell%0Adef%20_(c1%2C%20c2%2C%20c3%2C%20c4%2C%20colorbar%2C%20find_subpixel_peak_position%2C%20np%2C%20plt%2C%20shift)%3A%0A%20%20%20%20fig%2C%20ax%20%3D%20plt.subplots(1%2C%204%2C%20figsize%3D(14%2C%202.5))%0A%20%20%20%20counter%20%3D%200%0A%20%20%20%20for%20c%20in%20%5Bc1%2C%20c2%2C%20c3%2C%20c4%5D%3A%0A%20%20%20%20%20%20%20%20s%20%3D%20ax%5Bcounter%5D.contourf(c%5B0%2C%20%3A%2C%20%3A%5D)%0A%20%20%20%20%20%20%20%20ax%5Bcounter%5D.invert_yaxis()%0A%20%20%20%20%20%20%20%20colorbar(s%2C%20ax%3Dax%5Bcounter%5D)%0A%20%20%20%20%20%20%20%20default_peak_position%20%3D%20np.floor(np.array(c%5B0%2C%20%3A%2C%20%3A%5D.shape)%20%2F%202)%0A%20%20%20%20%20%20%20%20i%20%3D%20np.array(find_subpixel_peak_position(c%5B0%2C%20%3A%2C%20%3A%5D))%0A%20%20%20%20%20%20%20%20ax%5Bcounter%5D.plot(i%5B1%5D%2C%20i%5B0%5D%2C%20'rx')%0A%20%20%20%20%20%20%20%20print(np.array(i%20-%20default_peak_position)%2C%20np.sum(np.abs(np.array(i%20-%20default_peak_position)%20-%20np.array(shift))))%0A%20%20%20%20%20%20%20%20counter%20%3D%20counter%20%2B%201%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(a_1%2C%20b_1%2C%20np%2C%20phase_cross_correlation%2C%20plt)%3A%0A%20%20%20%20image%20%3D%20a_1%5B0%2C%20%3A%2C%20%3A%5D%0A%20%20%20%20offset_image%20%3D%20b_1%5B0%2C%20%3A%2C%20%3A%5D%0A%20%20%20%20%23%20pixel%20precision%20first%0A%20%20%20%20shift_1%2C%20error%2C%20diffphase%20%3D%20phase_cross_correlation(image%2C%20offset_image)%0A%20%20%20%20fig_1%20%3D%20plt.figure(figsize%3D(8%2C%203))%0A%20%20%20%20ax1%20%3D%20plt.subplot(1%2C%203%2C%201)%0A%20%20%20%20ax2%20%3D%20plt.subplot(1%2C%203%2C%202%2C%20sharex%3Dax1%2C%20sharey%3Dax1)%0A%20%20%20%20ax3%20%3D%20plt.subplot(1%2C%203%2C%203)%0A%20%20%20%20ax1.imshow(image%2C%20cmap%3D'gray')%0A%20%20%20%20ax1.set_axis_off()%0A%20%20%20%20ax1.set_title('Reference%20image')%0A%20%20%20%20ax2.imshow(offset_image.real%2C%20cmap%3D'gray')%0A%20%20%20%20ax2.set_axis_off()%0A%20%20%20%20ax2.set_title('Offset%20image')%0A%20%20%20%20image_product%20%3D%20np.fft.fft2(image).conj()%20*%20np.fft.fft2(offset_image)%0A%20%20%20%20cc_image%20%3D%20np.fft.fftshift(np.fft.ifft2(image_product))%0A%20%20%20%20ax3.imshow(cc_image.real)%0A%20%20%20%20ax3.set_axis_off()%0A%20%20%20%20%23%20Show%20the%20output%20of%20a%20cross-correlation%20to%20show%20what%20the%20algorithm%20is%0A%20%20%20%20%23%20doing%20behind%20the%20scenes%0A%20%20%20%20ax3.set_title('Cross-correlation')%0A%20%20%20%20plt.show()%0A%20%20%20%20print('Detected%20pixel%20offset%20(y%2C%20x)%3A%20%7B%7D'.format(shift_1))%0A%20%20%20%20shift_1%2C%20error%2C%20diffphase%20%3D%20phase_cross_correlation(offset_image%2C%20image%2C%20upsample_factor%3D1000)%0A%20%20%20%20fig_1%20%3D%20plt.figure(figsize%3D(8%2C%203))%0A%20%20%20%20ax1%20%3D%20plt.subplot(1%2C%203%2C%201)%0A%20%20%20%20ax2%20%3D%20plt.subplot(1%2C%203%2C%202%2C%20sharex%3Dax1%2C%20sharey%3Dax1)%0A%20%20%20%20ax1.imshow(image%2C%20cmap%3D'gray')%0A%20%20%20%20ax1.set_axis_off()%0A%20%20%20%20ax1.set_title('Reference%20image')%0A%20%20%20%20%23%20subpixel%20precision%0A%20%20%20%20ax2.imshow(offset_image.real%2C%20cmap%3D'gray')%0A%20%20%20%20ax2.set_axis_off()%0A%20%20%20%20ax2.set_title('Offset%20image')%0A%20%20%20%20plt.show()%0A%20%20%20%20%23%20ax3%20%3D%20plt.subplot(1%2C%203%2C%203)%0A%20%20%20%20%23%20Calculate%20the%20upsampled%20DFT%2C%20again%20to%20show%20what%20the%20algorithm%20is%20doing%0A%20%20%20%20%23%20behind%20the%20scenes.%20%20Constants%20correspond%20to%20calculated%20values%20in%20routine.%0A%20%20%20%20%23%20See%20source%20code%20for%20details.%0A%20%20%20%20%23%20cc_image%20%3D%20_upsampled_dft(image_product%2C%20150%2C%20100%2C%20(shift*100)%2B75).conj()%0A%20%20%20%20%23%20ax3.imshow(cc_image.real)%0A%20%20%20%20%23%20ax3.set_axis_off()%0A%20%20%20%20%23%20ax3.set_title(%22Supersampled%20XC%20sub-area%22)%0A%20%20%20%20print('Detected%20subpixel%20offset%20(y%2C%20x)%3A%20%7B%7D'.format(shift_1))%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
9ad5d5646398ee3799731ad96b94c4a5