# OpenPIV examples that you can execute in your browser¶

Thanks to the great service of mybinder.org

## Import what is necessary from OpenPIV¶

In [1]:
from openpiv import tools, 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 [2]:
frame_a  = tools.imread( 'exp1_001_b.bmp' )


## Show them using matplotlib¶

In [3]:
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[3]:
<matplotlib.image.AxesImage at 0x7ff2f817bdf0>

## 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 [4]:
winsize = 32 # pixels, interrogation window size in frame A
searchsize = 38  # pixels, search in image B
overlap = 12 # pixels, 50% overlap
dt = 0.02 # sec, time interval between pulses


## Run the OpenPIV (fast code, precompiled in Cython)¶

In [5]:
u0, v0, sig2noise = pyprocess.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 [6]:
x, y = pyprocess.get_coordinates( image_size=frame_a.shape,
search_area_size=searchsize,
overlap=overlap )


## Clean the peaks that are below a quality threshold¶

In [7]:
u1, v1, mask = validation.sig2noise_val( u0, v0,
sig2noise,
threshold = 1.05 )
# if you need more detailed look, first create a histogram of sig2noise
# plt.hist(sig2noise.flatten())
# to see where is a reasonable limit


## Replace those that are masked as bad vectors with local interpolation¶

In [8]:
# filter out outliers that are very different from the
# neighbours

u2, v2 = filters.replace_outliers( u1, v1,
method='localmean',
max_iter=3,
kernel_size=3)


## Scale the results from pix/dt to mm/sec¶

In [9]:
# convert x,y to mm
# convert u,v to mm/sec

x, y, u3, v3 = scaling.uniform(x, y, u2, v2,
scaling_factor = 96.52 ) # 96.52 microns/pixel

# 0,0 shall be bottom left, positive rotation rate is counterclockwise
x, y, u3, v3 = tools.transform_coordinates(x, y, u3, v3)


## store the result in a text file¶

In [10]:
# save in the simple ASCII table format
tools.save(x, y, u3, v3, mask, 'exp1_001.txt' )


## plot the data stored in the text file¶

In [11]:
fig, ax = plt.subplots(figsize=(8,8))
tools.display_vector_field('exp1_001.txt',
ax=ax, scaling_factor=96.52,
scale=50, # scale defines here the arrow length
width=0.0035, # width is the thickness of the arrow
on_img=True, # overlay on the image
image_name='exp1_001_b.bmp');


# 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 [12]:
frame_a = tools.imread('http://www.pivchallenge.org/pub/B/B001_1.tif')

<matplotlib.image.AxesImage at 0x7ff2f7ecb910>