Skip to content
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

Open
alexlib opened this issue Nov 17, 2020 · 9 comments
Open

Please consider roi2poly #23

alexlib opened this issue Nov 17, 2020 · 9 comments
Labels
enhancement New feature or request

Comments

@alexlib
Copy link
Member

alexlib commented Nov 17, 2020

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.

@alexlib alexlib added the enhancement New feature or request label Nov 17, 2020
@ErichZimmer
Copy link
Collaborator

ErichZimmer commented Nov 18, 2020

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,
Erich Zimmer

@ErichZimmer
Copy link
Collaborator

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 windef.py and pyprocess.py. This allows me to focus on new features more readily. However, time is running out for me as I am going on a 47 week long military training session starting in 5 days (only can push it back for so long 😝).

@ErichZimmer
Copy link
Collaborator

ErichZimmer commented Nov 18, 2020

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)

masking

This type of masking is alright, but the roi2poly package sure looks nice, so I'll see what I can come up with.

@alexlib
Copy link
Member Author

alexlib commented Nov 26, 2020

see also feature request on openpiv-python

OpenPIV/openpiv-python#169

@ErichZimmer
Copy link
Collaborator

ErichZimmer commented Aug 9, 2021

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.

@ErichZimmer
Copy link
Collaborator

How do we want to handle this issue?

@alexlib
Copy link
Member Author

alexlib commented Oct 28, 2021

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 roi2poly for the further masking.

@alexlib
Copy link
Member Author

alexlib commented Oct 28, 2021

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.

@alexlib
Copy link
Member Author

alexlib commented Nov 2, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants