-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Please consider roi2poly #23
Comments
Dear alex, This is already in the making, but improvements are much needed. Additionally, I have a proof-of-concept version that allows the user to import mask images and the python function measure.find_contour() does all the work for us. To input a mask manually, a list of image coordinates are inputted into a masking function that contains a version of poly2mask. An array is then created to create a binary image (black is masked, white is not) which is then multiplied onto the image to effectively mask areas to not be correlated. To apply the mask after the image is correlated, the correlated values is simply set to np.nan. After replacing vectors (if validation is used), the mask is applied again. One issue I am getting is that the masked pixels are set to zero. This causes edge artifacts in the correlation planes where masked objects are located. A better solution would be to set the masked objects to zero and then interpolate data to fill in the asked pixels to minimize said issue. Furthermore, masked vectors could be set to None or integer value 2 for the mask/typevector so the masked vectors could have a different visual effect (like x's for masked vectors). Regards, |
One issue that is slowing down progress on the masking function is that I can't figure a way to vectorize a loop that determines if there is more than 50% of the mask in the interrogation window. Right now, a function simply loops over all the interrogation windows (up to 20,000 in some experiments) and crops out a region of the mask image to see if the mean is less than 0.5 (mask image is normalized to [0,1]. This process is quite slow and is unacceptable in my "standards". Additionally, applying the polygon to the mask image is very inefficient and confusing (using PIL). I will post a working demonstration code soon. Luckily, I finished all the enhancements and bug fixes on the GUI and also created a new GUI for the refactored branch that utilizes the modularity of |
Here is a simple object masking function I made today. It takes an image and a nested list as input and creates a mask image that can then be multiplied to the image being evaluated to set the masked pixels to zero. """creates an mask image that can be multiplied to the
original image to set masked pixels to zero
Parameters
----------
image: 2d np.ndarray
a two dimensional array of integers containing grey levels of
the first frame.
mask_list: nested list [[]]
a nested list that contain the coordinates of vertices of a polygon
invert: bool
invert the object masking so that masked pixels are set to zero
Returns
-------
mask: 2d np.ndarray
a two dimensional array of integers either 0 or 1 with the same shape
as the image used to generate the mask
Example:
-------
import numpy as np
import matplotlib.pyplot as plt
from openpiv import tools
from PIL import Image, ImageDraw, ImageOps
frame_a = tools.imread( '../test1/exp1_001_a.bmp' )
mask_input = [[(50,50), (60, 10), (300,300), (100,400), (10, 80)],
[(300, 10), (300, 200), (150, 200)]]
mask_image = gen_mask_image(frame_a, mask_input, invert = True)
frame_A = frame_a * mask_image
fig,ax = plt.subplots(1,2)
ax[0].imshow(frame_a,cmap=plt.cm.gray)
ax[1].imshow(frame_A,cmap=plt.cm.gray)
"""
height = image.shape[0]
width = image.shape[1]
img = Image.new('L', (width, height))
for i in range(len(mask_list)):
ImageDraw.Draw(img).polygon(mask_list[i], outline=1, fill=1)
if invert:
img = ImageOps.invert(img)
mask = np.array(img)
mask = mask / mask.max()
mask[mask < 0.999] = 0
else:
mask = np.array(img)
return(mask) This type of masking is alright, but the roi2poly package sure looks nice, so I'll see what I can come up with. |
see also feature request on openpiv-python |
I think matplotlib widgets such as PolygonSelector and RectangleSelector are good starting grounds for interactive masking. They are a little slow, but with a little more effort, can be speed up with the use of blitting. Furthermore, there won't be a need to add extra dependencies. |
How do we want to handle this issue? |
we already have a prototype of masking in openpiv-python. What is missing is the graphical interface from the GUI - mouse event recording, conversion to polygon and use of |
I recently implemented it all in napari and it's very simple. However, this is not valid for our present GUI - you need to get it from Tk I guess. |
here is another option for mask using matplotlib |
https://github.com/GRSEB9S/roipoly.py
could be useful for the polygon masking on the images. If so, we'll consider adding some part of it to openpiv-python to handle complex masks.
The text was updated successfully, but these errors were encountered: