#!/usr/bin/env python # coding: utf-8 # ## OpenPIV tutorial of various correlation types # # In this notebook we compare the time to run the same analysis using Cython (precompiled) version # with the Python process using FFT and/or direct cross-correlation method # In[1]: from openpiv import tools, scaling, pyprocess, validation, filters import numpy as np import pylab get_ipython().run_line_magic('matplotlib', 'inline') # In[2]: frame_a = tools.imread( '../test1/exp1_001_a.bmp' ) frame_b = tools.imread( '../test1/exp1_001_b.bmp' ) pylab.imshow(np.c_[frame_a,np.ones((frame_a.shape[0],20)),frame_b], cmap=pylab.cm.gray) # In[3]: # Typical set of parameters window_size = 32 # pixels, IW size in frame A overlap = 16 # 50% overlap search_area_size = 32 # pixels, IW in frame B, helps for large displacements dt = 0.02 # second, 50 Hz, just an example scaling_factor = 96.52 # micron/pixel # In[4]: get_ipython().run_cell_magic('time', '', "\n# default correlation is FFT circular type (faster, less robust)\n# default type of correlation is not normalized, faster\n# we do not know the values of signal to noise ratio a priori\n# therefore we decide that we remove lowest 5%\n# the signal to noise ratio is defined here as 1st to 2nd peak ratio\n# All the parameters need to be checked. \n\n# get the values of displacements in pixel/sec units\nu, v, sig2noise = pyprocess.extended_search_area_piv( \n frame_a, frame_b, \n window_size=window_size, \n overlap=overlap, \n dt=dt, \n search_area_size=search_area_size, \n sig2noise_method='peak2peak' )\n\n\n# prepare centers of the IWs to know where locate the vectors\nx, y = pyprocess.get_coordinates(frame_a.shape, \n search_area_size=search_area_size, \n overlap=overlap)\n\nu, v, mask = validation.sig2noise_val( u, v, \n sig2noise, \n threshold = np.percentile(sig2noise,5))\n\n# removing and filling in the outlier vectors\nu, v = filters.replace_outliers( u, v, method='localmean', \n max_iter=10, \n kernel_size=2)\n\n# rescale the results to millimeters and mm/sec\nx, y, u, v = scaling.uniform(x, y, u, v, \n scaling_factor=scaling_factor )\n\n# save the data\ntools.save(x, y, u, v, sig2noise, mask, 'circular_default.txt' )\n") # In[5]: get_ipython().run_cell_magic('time', '', "\n# use normalized_correlation\n# both image intensity is normalized before correlation\n# and the correlation map has peaks between 0..1\n\n# get the values of displacements in pixel/sec units\nu, v, sig2noise = pyprocess.extended_search_area_piv( \n frame_a, frame_b, \n window_size=window_size, \n overlap=overlap, \n dt=dt, \n search_area_size=search_area_size, \n sig2noise_method='peak2peak',\n normalized_correlation = True)\n\n\n# prepare centers of the IWs to know where locate the vectors\nx, y = pyprocess.get_coordinates(frame_a.shape, \n search_area_size=search_area_size, \n overlap=overlap)\n\nu, v, mask = validation.sig2noise_val( u, v, \n sig2noise, \n threshold = np.percentile(sig2noise,5))\n\n# removing and filling in the outlier vectors\nu, v = filters.replace_outliers( u, v, method='localmean', \n max_iter=10, \n kernel_size=2)\n\n# rescale the results to millimeters and mm/sec\nx, y, u, v = scaling.uniform(x, y, u, v, \n scaling_factor=scaling_factor )\n\n# save the data\ntools.save(x, y, u, v, sig2noise, mask, 'circular_normalized.txt' )\n") # In[6]: get_ipython().run_cell_magic('time', '', "\n# change to another type of correlation 'linear' - uses\n# zero padding prior to the correlation\n# it requires uniform background and therefore \n# we need to normalize intensity of the images\n\nu, v, sig2noise = pyprocess.extended_search_area_piv( \n pyprocess.normalize_intensity(frame_a), \n pyprocess.normalize_intensity(frame_b), \n window_size=window_size, \n overlap=overlap, \n dt=dt, \n search_area_size=search_area_size, \n sig2noise_method='peak2peak',\n correlation_method = 'linear') \n\n\n# prepare centers of the IWs to know where locate the vectors\nx, y = pyprocess.get_coordinates(frame_a.shape, \n search_area_size=search_area_size, \n overlap=overlap)\n\nu, v, mask = validation.sig2noise_val( u, v, \n sig2noise, \n threshold = np.percentile(sig2noise,5))\n\n# removing and filling in the outlier vectors\nu, v = filters.replace_outliers( u, v, method='localmean', \n max_iter=10, \n kernel_size=2)\n\n# rescale the results to millimeters and mm/sec\nx, y, u, v = scaling.uniform(x, y, u, v, \n scaling_factor=scaling_factor )\n\n# save the data\ntools.save(x, y, u, v, sig2noise, mask, 'linear_intensity.txt' )\n") # In[7]: get_ipython().run_cell_magic('time', '', "\n# add normalized correlation to linear\n\nu, v, sig2noise = pyprocess.extended_search_area_piv( \n pyprocess.normalize_intensity(frame_a), \n pyprocess.normalize_intensity(frame_b), \n window_size=window_size, \n overlap=overlap, \n dt=dt, \n search_area_size=search_area_size, \n sig2noise_method='peak2peak',\n correlation_method='linear',\n normalized_correlation=True) \n\n\n# prepare centers of the IWs to know where locate the vectors\nx, y = pyprocess.get_coordinates(frame_a.shape, \n search_area_size=search_area_size, \n overlap=overlap)\n\nu, v, mask = validation.sig2noise_val( u, v, \n sig2noise, \n threshold = np.percentile(sig2noise,5))\n\n# removing and filling in the outlier vectors\nu, v = filters.replace_outliers( u, v, method='localmean', \n max_iter=10, \n kernel_size=2)\n\n# rescale the results to millimeters and mm/sec\nx, y, u, v = scaling.uniform(x, y, u, v, \n scaling_factor=scaling_factor )\n\n# save the data\ntools.save(x, y, u, v, sig2noise, mask, 'linear_normalized.txt' )\n") # In[8]: get_ipython().run_cell_magic('time', '', "\n\n# change to the extended search_type when window size in B is \n# larger to capture large displacements\n\nsearch_area_size = 40\n\nu, v, sig2noise = pyprocess.extended_search_area_piv( \n frame_a, \n frame_b, \n window_size=window_size, \n overlap=overlap, \n dt=dt, \n search_area_size=search_area_size, \n sig2noise_method='peak2peak',\n correlation_method='linear',\n normalized_correlation=True) \n\n\n# prepare centers of the IWs to know where locate the vectors\nx, y = pyprocess.get_coordinates(frame_a.shape, \n search_area_size=search_area_size, \n overlap=overlap)\n\nu, v, mask = validation.sig2noise_val( u, v, \n sig2noise, \n threshold = np.percentile(sig2noise,5))\n\n# removing and filling in the outlier vectors\nu, v = filters.replace_outliers( u, v, method='localmean', \n max_iter=10, \n kernel_size=2)\n\n# rescale the results to millimeters and mm/sec\nx, y, u, v = scaling.uniform(x, y, u, v, \n scaling_factor=scaling_factor )\n\n# save the data\ntools.save(x, y, u, v, sig2noise, mask, 'linear_normalized_extended.txt' )\n") # In[9]: get_ipython().run_cell_magic('time', '', "\n# default correlation is FFT circular type (faster, less robust)\n# default type of correlation is not normalized, faster\n# we do not know the values of signal to noise ratio a priori\n# therefore we decide that we remove lowest 5%\n# the signal to noise ratio is defined here as 1st to 2nd peak ratio\n# All the parameters need to be checked. \n\n# get the values of displacements in pixel/sec units\nu, v, sig2noise = pyprocess.extended_search_area_piv( \n frame_a, frame_b, \n window_size=window_size, \n overlap=overlap, \n dt=dt, \n search_area_size=search_area_size, \n sig2noise_method='peak2peak' )\n\n\n# prepare centers of the IWs to know where locate the vectors\nx, y = pyprocess.get_coordinates(frame_a.shape, \n search_area_size=search_area_size, \n overlap=overlap)\n\nu, v, mask = validation.sig2noise_val( u, v, \n sig2noise, \n threshold = np.percentile(sig2noise,5))\n\n# removing and filling in the outlier vectors\nu, v = filters.replace_outliers( u, v, method='localmean', \n max_iter=10, \n kernel_size=2)\n\n# rescale the results to millimeters and mm/sec\nx, y, u, v = scaling.uniform(x, y, u, v, \n scaling_factor=scaling_factor )\n\n# save the data\ntools.save(x, y, u, v, sig2noise, mask, 'circular_extended.txt' )\n") # In[10]: tools.display_vector_field('linear_normalized_extended.txt', scale=30) tools.display_vector_field('linear_normalized.txt', scale=30) tools.display_vector_field('linear_intensity.txt', scale=30) tools.display_vector_field('circular_default.txt', scale=30); tools.display_vector_field('circular_normalized.txt', scale=30); tools.display_vector_field('circular_extended.txt', scale=30); # In[ ]: # In[ ]: # In[ ]: