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

Refactor and improve image resizing #634

Closed
nfahlgren opened this issue Oct 16, 2020 · 6 comments
Closed

Refactor and improve image resizing #634

nfahlgren opened this issue Oct 16, 2020 · 6 comments
Labels
new feature New feature ideas and solutions
Projects

Comments

@nfahlgren
Copy link
Member

Is your feature request related to a problem? Please describe.
The function plantcv.resize could be improved in a few ways. It currently allows for resizing the x and y dimensions using scaling factors, but resizing to an exact size may be desirable also. We also use a single, fixed interpolation method that may not yield optimal results in all cases.

Describe the solution you'd like
I propose a few modifications to plantcv.resize and a new function.

  1. Move plantcv.resize to the transform subpackage: plantcv.transform.resize. This is both because it is a transformation of the input image and it's consistent with scikit-image.
  2. Refactor plantcv.transform.resize to resize images to an exact size.
  3. In plantcv.transform.resize, if the new size is smaller than the input size, use the OpenCV interpolation method cv2.INTER_AREA. If the new size is larger than the input size, use the interpolation method cv2.INTER_CUBIC. (These are the generally recommended options, but we could point a user to the OpenCV function if they want more control than what we provide).
  4. Make a new function called something like plantcv.transform.resize_factor that combines the new features proposed for plantcv.transform.resize but uses the current feature of scaling the sizes by factors rather than exact sizes.
  5. To avoid redundancy, it may be useful to have a helper function set the interpolation method.

Describe alternatives you've considered
These functions could be wrapped into one like the OpenCV function, but I think it's simpler to have two smaller functions rather than twice the inputs to one.

@nfahlgren nfahlgren added the new feature New feature ideas and solutions label Oct 16, 2020
@nfahlgren nfahlgren added this to the PlantCV v4.x milestone Oct 16, 2020
@nfahlgren nfahlgren added this to New Issues in PlantCV4 via automation Oct 16, 2020
@DannieSheng
Copy link
Contributor

Not sure if it is related, I will also be interested in adding resizing functions that resize images to exact sizes, either by cropping or padding. Currently, I have this function defined inside the function generating time_lapse_video.

@nfahlgren
Copy link
Member Author

Hi @DannieSheng, it did come to my mind while I was reviewing your PR! I realized that the resize function you proposed there does not use interpolation though, it either crops or pads as you said. Maybe now that I think about it, these could potentially be one function?

def resize(img, size, interpolation=True):
    # img = numpy.ndarray
    # size = tuple
    # interpolation = bool

    img_area = img.shape[0] * img.shape[1]
    new_area = size[0] * size[1]

    if interpolation:
        if new_area >= img_area:
            resized = cv2.resize(img, dsize=size, interpolation=cv2.INTER_CUBIC)
        else:
            resized = cv2.resize(img, dsize=size, interpolation=cv2.INTER_AREA)
    else:
        if new_area >= img_area:
            # pad
        else:
            # crop
    return resized

@DannieSheng
Copy link
Contributor

Hi @DannieSheng, it did come to my mind while I was reviewing your PR! I realized that the resize function you proposed there does not use interpolation though, it either crops or pads as you said. Maybe now that I think about it, these could potentially be one function?

def resize(img, size, interpolation=True):
    # img = numpy.ndarray
    # size = tuple
    # interpolation = bool

    img_area = img.shape[0] * img.shape[1]
    new_area = size[0] * size[1]

    if interpolation:
        if new_area >= img_area:
            resized = cv2.resize(img, dsize=size, interpolation=cv2.INTER_CUBIC)
        else:
            resized = cv2.resize(img, dsize=size, interpolation=cv2.INTER_AREA)
    else:
        if new_area >= img_area:
            # pad
        else:
            # crop
    return resized

Yes, this makes sense! :)

@dschneiderch
Copy link
Collaborator

I wrote up my results for data fusion / image overlays mentioned #615 (comment)

also, since you are refactoring:

  1. i think it would make sense to move rotate to transform.rotate() (maybe keep an alias in pcv.rotate -> pcv.transform.rotate())
  2. crop_position_mask() could likewise be moved and perhaps simplified. its an impressive function(!) but wouldn't cv2.warpAffine() accomplish the same thing? https://docs.opencv.org/3.4/da/d6e/tutorial_py_geometric_transformations.html
  3. we could also include a transform.shear() but i think it would be redundant to transform.skew(), transform.perspective(), transform.skew_perspective(),....or some other name for a function that uses a 3x3 transformation matrix. the perspective transformation requires 4 common ground control points instead of 3 for a shear/affine transformation.

links I found useful:
scikit-image on geometric transformations and homographies link therein
opencv geometric transformations

@nfahlgren nfahlgren added this to New Issues in PlantCV3 via automation Dec 7, 2020
@nfahlgren nfahlgren removed this from New Issues in PlantCV4 Dec 7, 2020
@nfahlgren
Copy link
Member Author

I opened a new issue to migrate rotate to transform.rotate.

Warp is added and we can soft-deprecate crop_position_mask in #648.

Should we open any new issue(s) for point 3 above @dschneiderch?

PlantCV3 automation moved this from New Issues to Done Dec 7, 2020
@dschneiderch
Copy link
Collaborator

@nfahlgren probably not necessary. i can't think of any applications for shear or skew over warp. it can always be revisited.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature New feature ideas and solutions
Projects
No open projects
PlantCV3
  
Done
Development

No branches or pull requests

3 participants