Crop regions in napari manually

Overview

napari-crop

License PyPI Python Version tests codecov

Crop regions in napari manually

Usage

Create a new shapes layer to annotate the region you would like to crop:

Use the rectangle tool to annotate a region. Start the crop tool from the Tools > Utilities > Crop region menu. Click the Run button to crop the region.

You can also use the Select shapes tool to move the rectangle to a new place and crop another region by clicking on Run.

Hint: You can also use the napari-tabu plugin to send all your cropped images to a new napari window.


This napari plugin was generated with Cookiecutter using with @napari's cookiecutter-napari-plugin template.

Installation

You can install napari-crop via pip:

pip install napari-crop

Contributing

Contributions are very welcome.

License

Distributed under the terms of the BSD-3 license, "napari-crop" is free and open source software

Issues

If you encounter any problems, please create a thread on image.sc along with a detailed description and tag @haesleinhuepf.

Comments
  • Return list of LayerDataTuple instead of single layers

    Return list of LayerDataTuple instead of single layers

    I implemented these changes to try to solve #6, returning several layers for several drawn shapes, but it is still not working.

    The motivation for this is explained here and there is indication that this approach has been implemented here.

    Does any of you guys have ideas to make this work? @haesleinhuepf @tdmorello

    opened by zoccoler 16
  • Make cropper RGB friendly, N-dimensional, and sliceable from any orthogonal viewpoint

    Make cropper RGB friendly, N-dimensional, and sliceable from any orthogonal viewpoint

    Hi Robert,

    First of all, consider me a big fan of your napari plugins -- I really appreciate the number of tools you are creating and publishing for napari users!

    Second, I was testing out this plugin and found it wasn't working on RGB images, so I dug into the code a little bit and thought of some ways to (hopefully) make it applicable to more scenarios. If you get some time to test it out, I'd really appreciate it!

    I think the key features are:

    1. it works with any number of dimensions
    2. you can draw a cropping region in an orthogonal view (e.g. XZ plane) and it'll give you the expected results
    3. the slice indices aren't hard-coded, so it should be a little easier to maintain and adapt

    I also wrote some tests to help my dev'ing (and hope they will be useful to the repo as well).

    Finally, I see @zoccoler 's PR and am planning on trying to fit my changes in with his (polygon cropping). The code I'm sharing can be easily modified to output more than 1 new layer when multiple shapes are draw.

    Let me know what you think.

    Best, Tim

    opened by tdmorello 7
  • removed python 3.7 and opencl from github CI

    removed python 3.7 and opencl from github CI

    I think this should fix #28 but I can't know for sure until I've started a run of the github CI.

    Edit: Tests are passing, switching to non-draft mode.

    Fixes #28

    bug enhancement 
    opened by jo-mueller 5
  • Cropped ellipses sometimes show a black line of pixels

    Cropped ellipses sometimes show a black line of pixels

    I have noticed that, for certain ellipses, the following cropped image is returned. This behavior is inconsistent: the line appears or not depending on where the ellipse is drawn.

    ellipse_bug

    opened by zoccoler 4
  • Tests are failing due to OpenGL installation in `test_and_deploy.yaml`

    Tests are failing due to OpenGL installation in `test_and_deploy.yaml`

    I'm not sure whether this installation is necessary for packages that do not rely on GPU-functionality. I'll start a PR to see if tests still pass after removing this part from test_and_deploy.yaml.

    opened by jo-mueller 3
  • Update README with new example

    Update README with new example

    Hey Robert @haesleinhuepf

    Here's a new example. I didn't match your style with the highlight circle around the cursor -- I hope that's ok. The url to the animation will have to change before merge.

    opened by tdmorello 3
  • Crop all shapes

    Crop all shapes

    Hi Robert @haesleinhuepf

    I modified the function adding 2 new functionalities:

    1. if more than one shape is drawn in the layer shape, it crops all shapes and adds them as new layers;
    2. if the shapes have irregular shapes (like ellipses or polygons), it crops according to that shape, clearing pixels outside the shape;
    • I also changed a little the extent of the shapes position and size (from .astype(int) to np.ceil or np.around) to ensure the irregular drawn shape would be entirely captured;

    Please let me know if it works and if you have suggestions/improvements to the code 😃

    opened by zoccoler 3
  • Give cropped layers unique names

    Give cropped layers unique names

    Hi guys,

    This Draft PR is intended to fix #20 . The problem happened because after #7 because here we always assigned the same name for drawn shapes from the same Shapes layer, thus, napari replaces previous output Image layer data instead of creating a new layer if the function was executed again.

    The proposed solution is to always provide a new unique name by checking layer names in the viewer (or layer names about to be added to the viewer in case of multiple shapes).

    It works here. I will write a test before turning this into regular PR.

    Best, Marcelo

    opened by zoccoler 2
  • Shape error for irregular shapes drawn close to image edge

    Shape error for irregular shapes drawn close to image edge

    Applying crop to shapes like these:

    import numpy as np
    arr_2d = np.arange(0, 25).reshape((5, 5))
    shapes = [
        np.array([[1, 1], [1, 3], [5, 3], [5, 1]]),
        np.array([[0.5, 0.5], [0.5, 3.5], [4.51, 3.5], [4.51, 0.5]]),
        np.array([[0, 2], [5, 5], [5, 2], [2, 0]]),
        
    ]
    shape_types = ["rectangle", "ellipse", "polygon"]
    
    viewer.add_image(arr_2d)
    shapes_layer = viewer.add_shapes(shapes, shape_type=shape_types, edge_width=0)
    

    shapes

    Only works with the rectangle. For irregular shapes drawn close or over image edge, it gives an error like this:

    ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (6,6)  and requested shape (5,5)
    
    opened by zoccoler 2
  • make crop_region part of the public API

    make crop_region part of the public API

    Hi all,

    does anybody see issues if we make crop_region part of the public API?

    E.g. like this:

    from napari_crop import crop_region
    

    We could then use it from scripts as discssed in this thread.

    Let me know what you think! If there are no concerns, I would just open the API.

    Best, Robert

    opened by haesleinhuepf 1
  • Bug/labels

    Bug/labels

    Fixes a problem where Labels layers could not be cropped.

    The crop_region function was looking for layer_props["rgb"] raising a KeyError with Labels layers.

    Also, adding a test for cropping Labels.

    opened by tdmorello 1
  • Support if image layers to Crop from have an .affine transform property

    Support if image layers to Crop from have an .affine transform property

    Both the image layer to crop from and the shape layer defining the crop regions could have an .affine property. I don't think these are currently taken into account.

    enhancement 
    opened by VolkerH 4
  • Dependency napari_workflows not specified in requirements or setup.cfg

    Dependency napari_workflows not specified in requirements or setup.cfg

    This seems to depend on https://github.com/haesleinhuepf/napari-workflows by @haesleinhuepf, but when installing the plugin, this dependency is not automatically installed.

    opened by VolkerH 3
  • Allow irregular nD crop

    Allow irregular nD crop

    Proposed enhancement

    napari-crop can crop in nD based on 2D shapes. It would be a good new feature to cut in irregular 3D shapes with the user providing points/shapes representing polygons at different z-slices.

    Example of the current behavior

    The code below is an attempt to reproduce it in the current state.

    import napari
    from napari_crop._function import crop_region
    from skimage.data import cells3d
    import numpy as np
    from magicgui import magicgui
    
    viewer = napari.Viewer()
    image = cells3d()
    image = image[:,1]
    
    polygon1 = np.array([[ 24., 141., 100.],
                        [ 24., 135., 115.],
                        [ 24., 142., 130.],
                        [ 24., 155., 134.],
                        [ 24., 167., 129.],
                        [ 24., 173., 117.],
                        [ 24., 163., 103.],
                        [ 24., 156.,  95.]])
    
    polygon2 = np.array([[ 33. , 136., 102.],
                        [ 33., 132., 115.7],
                        [ 33., 152., 135.],
                        [ 33., 165., 134.],
                        [ 33., 176., 122.],
                        [ 33., 180., 112.],
                        [ 33., 160.,  89.],
                        [ 33., 140.,  95.]])
    
    polygon3 = np.array([[ 45., 146., 94.],
                        [ 45., 143., 109.],
                        [ 45., 156., 122.],
                        [ 45., 170., 126.],
                        [ 45., 179., 120.],
                        [ 45., 182., 108.],
                        [ 45., 177.,  99.],
                        [ 45., 154.,  89.]])
    
    # This is how the shapes layer data would be if the user draw polygons along a z-stack
    polygon_list = [polygon1, polygon2, polygon3]
    
    # Transforms a list of polygons into a single 3D array of vertices
    shapes = [polygon[np.newaxis,:] for polygon in polygon_list]
    shape_3D = np.concatenate(shapes, axis=0)
    
    viewer.add_image(image)
    viewer.add_shapes(np.array(shape_3D), shape_type='polygon')
    
    widget = magicgui(crop_region)
    viewer.window.add_dock_widget(widget)
    

    Two errors happen:

    1. If the number of vertices per slice is not the same, there is a ValueError because the polygons cannot be concatenated into a 3D array with the same shape.
    2. Even if the number of vertices match, it gives an interpolation error: ValueError: 'linear' is not a valid Interpolation

    From this, it seems the shapes layer may not be the best choice for that. Turning it into a surface and cropping it seems more appropriate. Suggestions and feedback welcome :)

    enhancement 
    opened by zoccoler 0
  • Crop in time

    Crop in time

    I believe napari-crop should be able to handle time crops as well. It is just a matter of properly slicing, right? For that case, maybe relying on the shapes layer wouldn't be so intuitive from the user point of view.

    My first thought would be to add 2 spinboxes for start end end of time slice. It could be a rangeslider as well. Enabling this by means of a checkbox, or even better if the type of data can be auto-detected.

    What do you guys think?

    enhancement 
    opened by zoccoler 0
  • Cropping on large images does not give expected results

    Cropping on large images does not give expected results

    I have an 10888 x 6451 px image. When I draw small crops, it works as expected. When I draw large crops, the output shapes are not as expected.

    E.g. Crop shape = 9917 x 5423 output shape = 9025 x 4164

    Maybe it has something to do with how the image data is tiled in memory?

    When I crop a region that extends beyond the bounds of the image to get the whole image, it works as expected.

    opened by tdmorello 0
Releases(0.1.6)
  • 0.1.6(Jul 25, 2022)

    What's Changed

    • make crop_region public by @haesleinhuepf in https://github.com/BiAPoL/napari-crop/pull/23
    • Give cropped layers unique names by @zoccoler in https://github.com/BiAPoL/napari-crop/pull/24
    • fix axes order from viewer by @zoccoler in https://github.com/BiAPoL/napari-crop/pull/26
    • Unique layer names by @zoccoler in https://github.com/BiAPoL/napari-crop/pull/31 (actually brings PR #26 into main)

    Full Changelog: https://github.com/BiAPoL/napari-crop/compare/0.1.5...0.1.6

    Source code(tar.gz)
    Source code(zip)
  • 0.1.5(Jan 5, 2022)

    New features

    • Crop multiple shapes (Thanks to @zoccoler and @tdmorello for implementing this)

    Note: The repository location changed. It's now https://github.com/BiAPoL/napari-crop

    Source code(tar.gz)
    Source code(zip)
  • 0.1.4(Dec 27, 2021)

    New features

    • Supports more image shapes and RGB data
    • Supports cropping polygons

    Big thanks to Tim Morello @tdmorello and Marcelo Leomil Zoccoler @zoccoler for working on this!

    Source code(tar.gz)
    Source code(zip)
  • 0.1.3(Oct 26, 2021)

  • 0.1.2(Oct 21, 2021)

  • 0.1.1(Oct 21, 2021)

  • 0.1.0(Oct 21, 2021)

Owner
Robert Haase
Computational Microscopist, BioImage Analyst, Code Jockey
Robert Haase
missing-pixel-filler is a python package that, given images that may contain missing data regions (like satellite imagery with swath gaps), returns these images with the regions filled.

Missing Pixel Filler This is the official code repository for the Missing Pixel Filler by SpaceML. missing-pixel-filler is a python package that, give

SpaceML 11 Jul 19, 2022
Napari 3D Ortho Viewer - an ortho viewer for napari for 3D images

napari-3d-ortho-viewer Napari 3D Ortho Viewer - an ortho viewer for napari for 3D images This napari plugin was generated with Cookiecutter using @nap

niklas netter 5 Nov 28, 2022
Simple python implementation with matplotlib to manually fit MIST isochrones to Gaia DR2 color-magnitude diagrams

Simple python implementation with matplotlib to manually fit MIST isochrones to Gaia DR2 color-magnitude diagrams

Karl Jaehnig 7 Oct 22, 2022
Tool to add main subject to items on Wikidata using a WMFs CirrusSearch for named entity recognition or a manually supplied list of QIDs

ItemSubjector Tool made to add main subject statements to items based on the title using a home-brewed CirrusSearch-based Named Entity Recognition alg

Dennis Priskorn 9 Nov 17, 2022
This code renames subtitle file names to your video files names, so you don't need to rename them manually.

Rename Subtitle This code renames your subtitle file names to your video file names so you don't need to do it manually Note: It only works for series

Mostafa Kazemi 4 Sep 12, 2021
Manually Install Python 2.7 pip without any problem !

Python2.7_install_pip Manually Install Python 2.7 pip without any problem ! Download installPip.py to your system and Run the code using this Command

Ali Jafari 1 Dec 9, 2021
Django helper application to easily and non-destructively crop arbitrarily large images in admin and frontend.

django-image-cropping django-image-cropping is an app for cropping uploaded images via Django's admin backend using Jcrop. Screenshot: django-image-cr

Jonas und der Wolf GmbH 546 Jan 3, 2023
Automatically download and crop key information from the arxiv daily paper.

Arxiv daily 速览 功能:按关键词筛选arxiv每日最新paper,自动获取摘要,自动截取文中表格和图片。 1 测试环境 Ubuntu 16+ Python3.7 torch 1.9 Colab GPU 2 使用演示 首先下载权重baiduyun 提取码:il87,放置于code/Pars

HeoLis 20 Jul 30, 2022
Small python-gtk application, which helps the user to merge or split pdf documents and rotate, crop and rearrange their pages using an interactive and intuitive graphical interface

Small python-gtk application, which helps the user to merge or split pdf documents and rotate, crop and rearrange their pages using an interactive and intuitive graphical interface

null 1.8k Dec 29, 2022
Face recognize and crop them

Face Recognize Cropping Module Source 아이디어 Face Alignment with OpenCV and Python Requirement 필요 라이브러리 imutil dlib python-opence (cv2) Usage 사용 방법 open

Cho Moon Gi 1 Feb 15, 2022
✂️ EyeLipCropper is a Python tool to crop eyes and mouth ROIs of the given video.

EyeLipCropper EyeLipCropper is a Python tool to crop eyes and mouth ROIs of the given video. The whole process consists of three parts: frame extracti

Zi-Han Liu 9 Oct 25, 2022
A discord bot to crop an NFT image living on the Solana blockchain.

NFT Discord Cropper This discord bot crops an NFT in your set measures by getting it through the .cache file which has been used to make a candy machi

Rude Golems 7 Mar 21, 2022
Automatically download and crop key information from the arxiv daily paper. (cpu version)

Automatically download and crop key information from the arxiv daily paper. (cpu version)

HeoLis 4 Jul 30, 2022
~1000 book pages + OpenCV + python = page regions identified as paragraphs, lines, images, captions, etc.

cosc428-structor I had an open-ended Computer Vision assignment to complete, and an out-of-copyright book that I wanted to turn into an ebook. Convent

Chad Oliver 45 Dec 6, 2022
Platform for building statistical models of cities and regions

UrbanSim UrbanSim is a platform for building statistical models of cities and regions. These models help forecast long-range patterns in real estate d

Urban Data Science Toolkit 419 Dec 30, 2022
Collection of useful (to me) python scripts for interacting with napari

Napari scripts A collection of napari related tools in various state of disrepair/functionality. Browse_LIF_widget.py This module can be imported, for

null 5 Aug 15, 2022
Modeling Category-Selective Cortical Regions with Topographic Variational Autoencoders

Modeling Category-Selective Cortical Regions with Topographic Variational Autoencoders

null 1 Oct 11, 2021
Napari plugin for loading Bitplane Imaris files .ims

napari-imaris-loader Napari plugin for loading Bitplane Imaris files '.ims'. Notes: For this plugin to work "File/Preferences/Experimental/Render Imag

Alan Watson 4 Dec 1, 2022
A napari plugin for visualising and interacting with electron cryotomograms.

napari-tomoslice A napari plugin for visualising and interacting with electron cryotomograms. Installation You can install napari-tomoslice via pip: p

null 3 Jan 3, 2023
Modeling Category-Selective Cortical Regions with Topographic Variational Autoencoders

Modeling Category-Selective Cortical Regions with Topographic Variational Autoencoders Getting Started Install requirements with Anaconda: conda env c

T. Andy Keller 4 Aug 22, 2022