Thanks for the reply.
I have a trial to "impose the mask both as a mask (masked array) and impose it on the image itself as black region".
The code I used is shown below.
================================================================
from openpiv import tools, preprocess, pyprocess, validation, filters, scaling
import numpy as np
import matplotlib.pyplot as plt
#matplotlib inline
import imageio
import importlib_resources
import pathlib
path = importlib_resources.files('openpiv')
frame_a = tools.imread( path / 'data/test1/exp1_001_a.bmp' )
frame_b = tools.imread( path / 'data/test1/exp1_001_b.bmp' )
#fig,ax = plt.subplots(1,2,figsize=(12,10))
#ax[0].imshow(frame_a,cmap=plt.cm.gray);
#ax[1].imshow(frame_b,cmap=plt.cm.gray);
#plt.show()
winsize = 32 # pixels, interrogation window size in frame A
searchsize = 38 # pixels, search area size in frame B
overlap = 17 # pixels, 50% overlap
dt = 0.02 # sec, time interval between the two frames
#==============================
#masking
from skimage.draw import polygon
from skimage.draw import disk
mask = 0*frame_a.copy()+1
rr, cc = polygon(
[150,200,200,150],
[100,100,250,250]
)
mask[rr, cc] = 0
rr, cc = disk((150,450), 50)
#rr, cc = disk((150,450), 50)
#mask[rr, cc] = 0
#"to impose it on the image itself as black region"
frame_a=frame_a*mask
frame_b=frame_b*mask
fig,ax = plt.subplots(1,2,figsize=(12,10))
ax[0].imshow(frame_a,cmap=plt.cm.gray);
ax[1].imshow(frame_b,cmap=plt.cm.gray);
plt.show()
# convert mask to a boolean mask
mask=1-mask
mask = np.where(mask, True, False)
#==============================
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',
)
x, y = pyprocess.get_coordinates(
image_size=frame_a.shape,
search_area_size=searchsize,
overlap=overlap,
)
#plt.hist(sig2noise.flatten())
#plt.show(block=False)
invalid_mask = validation.sig2noise_val(
sig2noise,
threshold = 1.05,
)
u1, v1 = filters.replace_outliers(
u0, v0,
invalid_mask,
method='localmean',
max_iter=3,
kernel_size=3,
)
# convert x,y to mm
# convert u,v to mm/sec
x, y, u2, v2 = scaling.uniform(
x, y, u1, v1,
scaling_factor = 96.52, # 96.52 pixels/millimeter
)
# 0,0 shall be bottom left, positive rotation rate is counterclockwise
x, y, u3, v3 = tools.transform_coordinates(x, y, u2, v2)
# using the grid mask to create masked arrays
grid_mask = preprocess.prepare_mask_on_grid(x,y,mask)
invalid_mask=invalid_mask | grid_mask
# "to impose the mask both as a mask (masked array)"
masked_u = np.ma.masked_array(u3, mask=invalid_mask)
masked_v = np.ma.masked_array(v3, mask=invalid_mask)
tools.save('exp1_001.txt' , x, y, masked_u, masked_v, invalid_mask)
fig, ax = plt.subplots(figsize=(8,8))
tools.display_vector_field(
pathlib.Path('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= str(path / 'data'/'test1'/'exp1_001_a.bmp'),
);
================================================================
The two images to be analyzed are:
A rectangule and a circle regions are masked:
The outcome before the masking should be like this:
The outcome after masking is:
It seems that the red vectors are masked out, but the abnormal horizontal vectors still exist.
I think these are the vectors out of the regions of our masks.
I guess that since some particles disappared (blocked by the masks),
the cross-correlation algorithom cannot correctly match the search window around the masks,
but match to some "fake peak" around.
I do not know whether there is a way to eliminate these abnormal vectors.
What I can think is to eliminate them manually in the final results.
Best regards,
Wenyue Zhang