def check_windows(img_a,img_b,no): win_a = pyprocess.moving_window_array(img_a,20,8) win_b = pyprocess.moving_window_array(img_b,20,8) fig,ax = plt.subplots(2,figsize=(10,10)) ax[0].imshow(win_a[no,:,:]) ax[1].imshow(win_b[no,:,:])
def multipass_img_deform(frame_a, frame_b, window_size, overlap, iterations, current_iteration, x_old, y_old, u_old, v_old, correlation_method='circular', subpixel_method='gaussian', do_sig2noise=False, sig2noise_method='peak2peak', sig2noise_mask=2, MinMaxU=(-100, 50), MinMaxV=(-50, 50), std_threshold=5, median_threshold=2, median_size=1, filter_method='localmean', max_filter_iteration=10, filter_kernel_size=2, interpolation_order=3): """ First pass of the PIV evaluation. This function does the PIV evaluation of the first pass. It returns the coordinates of the interrogation window centres, the displacment u and v for each interrogation window as well as the mask which indicates wether the displacement vector was interpolated or not. Parameters ---------- frame_a : 2d np.ndarray the first image frame_b : 2d np.ndarray the second image window_size : tuple of ints the size of the interrogation window overlap : tuple of ints the overlap of the interrogation window normal for example window_size/2 x_old : 2d np.ndarray the x coordinates of the vector field of the previous pass y_old : 2d np.ndarray the y coordinates of the vector field of the previous pass u_old : 2d np.ndarray the u displacement of the vector field of the previous pass v_old : 2d np.ndarray the v displacement of the vector field of the previous pass subpixel_method: string the method used for the subpixel interpolation. one of the following methods to estimate subpixel location of the peak: 'centroid' [replaces default if correlation map is negative], 'gaussian' [default if correlation map is positive], 'parabolic' MinMaxU : two elements tuple sets the limits of the u displacment component Used for validation. MinMaxV : two elements tuple sets the limits of the v displacment component Used for validation. std_threshold : float sets the threshold for the std validation median_threshold : float sets the threshold for the median validation filter_method : string the method used to replace the non-valid vectors Methods: 'localmean', 'disk', 'distance', max_filter_iteration : int maximum of filter iterations to replace nans filter_kernel_size : int size of the kernel used for the filtering interpolation_order : int the order of the spline interpolation used for the image deformation Returns ------- x : 2d np.array array containg the x coordinates of the interrogation window centres y : 2d np.array array containg the y coordinates of the interrogation window centres u : 2d np.array array containing the u displacement for every interrogation window u : 2d np.array array containing the u displacement for every interrogation window mask : 2d np.array array containg the mask values (bool) which contains information if the vector was filtered """ x, y = get_coordinates(np.shape(frame_a), window_size, overlap) 'calculate the y and y coordinates of the interrogation window centres' y_old = y_old[:, 0] # y_old = y_old[::-1] x_old = x_old[0, :] y_int = y[:, 0] # y_int = y_int[::-1] x_int = x[0, :] '''The interpolation function dont like meshgrids as input. Hence, the the edges must be extracted to provide the sufficient input. x_old and y_old are the are the coordinates of the old grid. x_int and y_int are the coordiantes of the new grid''' ip = RectBivariateSpline(y_old, x_old, u_old) u_pre = ip(y_int, x_int) ip2 = RectBivariateSpline(y_old, x_old, v_old) v_pre = ip2(y_int, x_int) ''' interpolating the displacements from the old grid onto the new grid y befor x because of numpy works row major ''' frame_b_deform = frame_interpolation( frame_b, x, y, u_pre, -v_pre, interpolation_order=interpolation_order) '''this one is doing the image deformation (see above)''' cor_win_1 = pyprocess.moving_window_array(frame_a, window_size, overlap) cor_win_2 = pyprocess.moving_window_array(frame_b_deform, window_size, overlap) '''Filling the interrogation window. They windows are arranged in a 3d array with number of interrogation window *window_size*window_size this way is much faster then using a loop''' correlation = correlation_func(cor_win_1, cor_win_2, correlation_method=correlation_method, normalized_correlation=False) 'do the correlation' disp = np.zeros((np.size(correlation, 0), 2)) for i in range(0, np.size(correlation, 0)): ''' determine the displacment on subpixel level ''' disp[i, :] = find_subpixel_peak_position( correlation[i, :, :], subpixel_method=subpixel_method) 'this loop is doing the displacment evaluation for each window ' disp = np.array(disp) - np.floor(np.array(correlation[0, :, :].shape) / 2) 'reshaping the interrogation window to vector field shape' shapes = np.array( pyprocess.get_field_shape(np.shape(frame_a), window_size, overlap)) u = disp[:, 1].reshape(shapes) v = -disp[:, 0].reshape(shapes) 'adding the recent displacment on to the displacment of the previous pass' u = u + u_pre v = v + v_pre 'validation using gloabl limits and local median' u, v, mask_g = validation.global_val(u, v, MinMaxU, MinMaxV) u, v, mask_s = validation.global_std(u, v, std_threshold=std_threshold) u, v, mask_m = validation.local_median_val(u, v, u_threshold=median_threshold, v_threshold=median_threshold, size=median_size) mask = mask_g + mask_m + mask_s 'adding masks to add the effect of alle the validations' #mask=np.zeros_like(u) 'filter to replace the values that where marked by the validation' if current_iteration != iterations: 'filter to replace the values that where marked by the validation' u, v = filters.replace_outliers(u, v, method=filter_method, max_iter=max_filter_iteration, kernel_size=filter_kernel_size) if do_sig2noise == True and current_iteration == iterations and iterations != 1: sig2noise_ratio = sig2noise_ratio_function( correlation, sig2noise_method=sig2noise_method, width=sig2noise_mask) sig2noise_ratio = sig2noise_ratio.reshape(shapes) else: sig2noise_ratio = np.full_like(u, np.nan) return x, y, u, v, sig2noise_ratio, mask
def first_pass(frame_a, frame_b, window_size, overlap, iterations, correlation_method='circular', subpixel_method='gaussian', do_sig2noise=False, sig2noise_method='peak2peak', sig2noise_mask=2): """ First pass of the PIV evaluation. This function does the PIV evaluation of the first pass. It returns the coordinates of the interrogation window centres, the displacment u and v for each interrogation window as well as the mask which indicates wether the displacement vector was interpolated or not. Parameters ---------- frame_a : 2d np.ndarray the first image frame_b : 2d np.ndarray the second image window_size : int the size of the interrogation window overlap : int the overlap of the interrogation window normal for example window_size/2 subpixel_method: string the method used for the subpixel interpolation. one of the following methods to estimate subpixel location of the peak: 'centroid' [replaces default if correlation map is negative], 'gaussian' [default if correlation map is positive], 'parabolic' Returns ------- x : 2d np.array array containg the x coordinates of the interrogation window centres y : 2d np.array array containg the y coordinates of the interrogation window centres u : 2d np.array array containing the u displacement for every interrogation window u : 2d np.array array containing the u displacement for every interrogation window """ cor_win_1 = pyprocess.moving_window_array(frame_a, window_size, overlap) cor_win_2 = pyprocess.moving_window_array(frame_b, window_size, overlap) '''Filling the interrogation window. They windows are arranged in a 3d array with number of interrogation window *window_size*window_size this way is much faster then using a loop''' correlation = correlation_func(cor_win_1, cor_win_2, correlation_method=correlation_method, normalized_correlation=False) 'do the correlation' disp = np.zeros((np.size(correlation, 0), 2)) #create a dummy for the loop to fill for i in range(0, np.size(correlation, 0)): ''' determine the displacment on subpixel level ''' disp[i, :] = find_subpixel_peak_position( correlation[i, :, :], subpixel_method=subpixel_method) 'this loop is doing the displacment evaluation for each window ' disp = np.array(disp) - np.floor(np.array(correlation[0, :, :].shape) / 2) shapes = np.array( pyprocess.get_field_shape(frame_a.shape, window_size, overlap)) u = disp[:, 1].reshape(shapes) v = -disp[:, 0].reshape(shapes) 'reshaping the interrogation window to vector field shape' x, y = get_coordinates(frame_a.shape, window_size, overlap) 'get coordinates for to map the displacement' if do_sig2noise == True and iterations == 1: sig2noise_ratio = sig2noise_ratio_function( correlation, sig2noise_method=sig2noise_method, width=sig2noise_mask) sig2noise_ratio = sig2noise_ratio.reshape(shapes) else: sig2noise_ratio = np.full_like(u, np.nan) return x, y, u, v, sig2noise_ratio
# In[5]: # let's make two images of 32 x 32 pixels a = np.random.rand(64, 64) b = np.roll(a, (-3, 2)) # In[6]: # parameters for the test window_size = 8 overlap = 4 # In[7]: # for the regular square windows case: aa = moving_window_array(normalize_intensity(a), window_size, overlap) bb = moving_window_array(normalize_intensity(b), window_size, overlap) # In[8]: c = fft_correlate_strided_images(aa, bb) # In[9]: # let's assume we want the extended search type of PIV analysis # with search_area_size in image B > window_size in image A window_size = 4 overlap = 2 search_size = 8 # In[10]:
img_a = img_a[:,415:] img_b = img_b[:,415:] piv.run_piv(img_a,img_b, winsize = 16, # pixels, interrogation window size in frame A searchsize = 20, # pixels, search in image B overlap = 8, u_bounds = [-100,100], v_bounds = [-2000,0], scale_factor=1e4), # %% from openpiv import pyprocess from matplotlib import pyplot as plt win_a = pyprocess.moving_window_array(img_a,20,8) win_b = pyprocess.moving_window_array(img_b,20,8) fig,ax = plt.subplots(2,figsize=(10,10)) ax[0].imshow(win_a[50,:,:]) ax[1].imshow(win_b[50,:,:]) # %% def check_windows(img_a,img_b,no): win_a = pyprocess.moving_window_array(img_a,20,8) win_b = pyprocess.moving_window_array(img_b,20,8) fig,ax = plt.subplots(2,figsize=(10,10)) ax[0].imshow(win_a[no,:,:]) ax[1].imshow(win_b[no,:,:])