OpenPIV examples that you can execute in your browser

Thanks to the great service of mybinder.org

Import what is necessary from OpenPIV

In [26]:
from openpiv import tools, process, validation, filters, scaling, pyprocess
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from IPython.display import display
from ipywidgets import interact_manual, interactive, fixed, IntSlider, HBox, VBox, Layout

Read a pair of PIV images

In [4]:
frame_a  = tools.imread( 'exp1_001_b.bmp' )
frame_b  = tools.imread( 'exp1_001_c.bmp' )

Show them using matplotlib

In [5]:
fig,ax = plt.subplots(1,2,figsize=(10,8))
ax[0].imshow(frame_a,cmap=plt.cm.gray)
ax[1].imshow(frame_b,cmap=plt.cm.gray)
Out[5]:
<matplotlib.image.AxesImage at 0x1c15c6dcd0>

Define the PIV analysis parameters

  1. Size of the interrogation window in frame A (winsize),
  2. Size of the search window in frame B (searchsize is larger or equal to winsize),
  3. overlap between the neighbouring windows (overlap),
  4. time interval of the PIV recording ($\Delta t$)
  5. type of the peak quality (signal-to-noise ratio)
In [6]:
winsize = 24 # pixels
searchsize = 64  # pixels, search in image B
overlap = 12 # pixels
dt = 0.02 # sec

Run the OpenPIV (fast code, precompiled in Cython)

In [7]:
u, v, sig2noise = process.extended_search_area_piv( frame_a.astype(np.int32), frame_b.astype(np.int32), 
                                                     window_size=winsize, overlap=overlap, dt=dt, 
                                                     search_area_size=searchsize, sig2noise_method='peak2peak' )

Get a list of coordinates for the vector field

In [8]:
x, y = process.get_coordinates( image_size=frame_a.shape, window_size=winsize, overlap=overlap )

Clean the peaks that are below a quality threshold

In [9]:
u, v, mask = validation.sig2noise_val( u, v, sig2noise, threshold = 1.3 )

Replace those that are masked as bad vectors with local interpolation

In [10]:
u, v = filters.replace_outliers( u, v, method='localmean', max_iter=10, kernel_size=2)

Scale the results from pix/dt to mm/sec

In [11]:
x, y, u, v = scaling.uniform(x, y, u, v, scaling_factor = 96.52 )

store the result in a text file

In [12]:
tools.save(x, y, u, v, mask, 'exp1_001.txt' )

plot the data stored in the text file

In [13]:
tools.display_vector_field('exp1_001.txt', scale=100, width=0.0025)

Another example

Use any pair of images that you can access via URL

For instance we can use images from PIV Challenge http://www.pivchallenge.org/

In [14]:
frame_a = tools.imread('http://www.pivchallenge.org/pub/B/B001_1.tif')
frame_b = tools.imread('http://www.pivchallenge.org/pub/B/B001_2.tif')
fig,ax = plt.subplots(1,2,figsize=(10,8))
ax[0].imshow(frame_a,cmap=plt.cm.gray)
ax[1].imshow(frame_b,cmap=plt.cm.gray)
Out[14]:
<matplotlib.image.AxesImage at 0x10e3a5fd0>
In [15]:
winsize = 32 # pixels
searchsize = 64  # pixels, search in image B
overlap = 16 # pixels
dt = 1.0 # sec
u0, v0, sig2noise = process.extended_search_area_piv( frame_a.astype(np.int32), frame_b.astype(np.int32), window_size=winsize, overlap=overlap, dt=dt, search_area_size=searchsize, sig2noise_method='peak2peak' )
x, y = process.get_coordinates( image_size=frame_a.shape, window_size=winsize, overlap=overlap )
u, v, mask = validation.sig2noise_val( u0, v0, sig2noise, threshold = 1.1 )
u, v = filters.replace_outliers( u, v, method='localmean', max_iter=10, kernel_size=2)
# x, y, u, v = scaling.uniform(x, y, u, v, scaling_factor = 96.52 )

plt.figure(figsize=(10,8))
plt.quiver(x,y,u,v,color='b')
plt.quiver(x[mask],y[mask],u[mask],v[mask],color='r')
Out[15]:
<matplotlib.quiver.Quiver at 0x10e530a10>

How to use IpyWidgets to get an interactive GUI

Import ipywidgets

read more about those on https://ipywidgets.readthedocs.io/en/stable/index.html

In [16]:
from ipywidgets import interact, interactive, fixed, interact_manual
In [17]:
def func(winsize,overlap,searchsize,s2n_method,s2n_threshold):
    if overlap >= winsize:
        overlap -= 1
        
    u, v, sig2noise = process.extended_search_area_piv(frame_a.astype(np.int32), frame_b.astype(np.int32), window_size=winsize, overlap=overlap, dt=dt, search_area_size=searchsize, sig2noise_method=s2n_method )
    x, y = process.get_coordinates( image_size=frame_a.shape, window_size=winsize, overlap=overlap )
    u, v, mask = validation.sig2noise_val( u, v, sig2noise, threshold = s2n_threshold )
    u, v = filters.replace_outliers( u, v, method='localmean', max_iter=10, kernel_size=2)
    plt.figure(figsize=(10,8))
    plt.quiver(x,y,u,v,color='b')
    plt.quiver(x[mask],y[mask],u[mask],v[mask],color='r');

Press Run interact to get PIV output for the values in the list

In [18]:
def run():
    interact_manual(func,winsize=[32,8,16,64,128],
                overlap=[16,8,32,64], 
                searchsize=[64,16,32,128,256],
                s2n_method=['peak2peak','peak2mean'],
                s2n_threshold=(1,10,.1));
In [19]:
run()

Let's say you want this GIF motion from the excellent JPIV website

In [20]:
from IPython.display import Image
Image(url='http://www.jpiv.vennemann-online.de/fig/bode.gif')
Out[20]:
In [21]:
# read it:
gif = tools.imread('http://www.jpiv.vennemann-online.de/fig/bode.gif')

# convert to gray scale
from skimage import img_as_uint
frame_a = img_as_uint(gif[0])
frame_b = img_as_uint(gif[1])
/Users/alex/anaconda3/envs/py27/lib/python2.7/site-packages/skimage/util/dtype.py:122: UserWarning: Possible precision loss when converting from float64 to uint16
  .format(dtypeobj_in, dtypeobj_out))
In [22]:
# see if this is what you want
plt.figure()
plt.imshow(frame_a,cmap=plt.cm.gray)
Out[22]:
<matplotlib.image.AxesImage at 0x1c16256f10>