-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiv.py
98 lines (71 loc) · 2.78 KB
/
piv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# -*- coding: utf-8 -*-
from matplotlib.pyplot import *
import numpy as np
from scipy import signal
import os
def loaddata(file1, file2):
img1 = imread(os.path.realpath(file1))
img2 = imread(os.path.realpath(file2))
if img1.shape != img2.shape:
raise ValueError('Dimensions of input images do not match!')
if img1.ndim > 2:
img1 = np.average(img1, axis=2)
img2 = np.average(img2, axis=2)
return img1, img2
x, y = loaddata('B005_1.tif', 'B005_2.tif')
imshow(x)
def piv(file1, file2, siw=20, ssw=None):
#Init
if np.size(ssw) < np.size(siw):
raise ValueError('size_search_window zu klein')
if ssw is None:
ssw = siw
#Load Images
img1, img2 = loaddata(file1, file2)
sim1 = np.shape(img1) #size img
#Grid with x and y vectors, x and y contain centers of each window
x = np.arange(ssw//2, sim1[1], siw)
y = np.arange(ssw//2, sim1[0], siw)
X, Y = np.meshgrid(x, y)
#Plot Grid
#figure(3, figsize=(20,20))
#plot(X, Y, marker='.', color='k', linestyle='none')
#axis('equal')
u = np.zeros((len(y), len(x)))
v = np.zeros((len(y), len(x)))
border_diffy = sim1[0] - y[-1]
border_diffx = sim1[1] - x[-1]
#Check border overflowing
if border_diffy < ssw//2 and border_diffy != 0:
img1 = np.pad(img1, ((0, 0),(0, ssw//2 - border_diffy)))
if border_diffx < ssw//2 and border_diffx != 0:
img1 = np.pad(img1, ((0, ssw//2 - border_diffx),(0, 0)))
for n,i in enumerate(x):
for m,j in enumerate(y):
#Window definition
interr_window = np.array(img1[j-siw//2:j+siw//2, i-siw//2:i+siw//2])
search_window = np.array(img2[j-ssw//2:j+ssw//2, i-ssw//2:i+ssw//2])
#get correlation and index of spike
interr_window = interr_window - np.mean(interr_window)
search_window = search_window - np.mean(search_window)
corr = signal.correlate2d(search_window, interr_window, mode='full')
ind = np.unravel_index(corr.argmax(), corr.shape)
#define u and v
u[m, n] = ind[1] - ((ssw + siw)//2-1)
v[m, n] = ind[0] - ((ssw + siw)//2-1)
return X,Y,u,v
#"B005_1.tif", "B005_2.tif", "B_010.TIF","B_014.TIF" , "B038a.bmp", "B038b.bmp", "A001_1.tif", "A001_2.tif", 'A045a.tif', 'A045b.tif'
file1, file2 = "A001_1.tif", "A001_2.tif"
X, Y, U, V = piv(file1, file2, ssw=30)
M = np.sqrt(pow(U, 2) + pow(V, 2))
# Plot Results
#figure(figsize=(15,15))
#subplot(1,3,1)
#imshow(plt.imread(file2), alpha=.4)
#streamplot(X, Y, U, V)
#subplot(1,3,2)
#imshow(plt.imread(file2), alpha=.4)
#contourf(X,Y,M)
#subplot(1,3,3)
#imshow(plt.imread(file2), alpha=.4)
#quiver(X, Y, U, V, angles='xy')