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
Official implementation of "An Image is Worth 16x16 Words, What is a Video Worth?" (2021 paper)

An Image is Worth 16x16 Words, What is a Video Worth? paper Official PyTorch Implementation Gilad Sharir, Asaf Noy, Lihi Zelnik-Manor DAMO Academy, Al

213 Nov 12, 2022
Amazing 3D explosion animation using Pygame module.

3D Explosion Animation 💣 💥 🔥 Amazing explosion animation with Pygame. 💣 Explosion physics An Explosion instance is made of a set of Particle objec

Dylan Tintenfich 12 Mar 11, 2022
Primary QPDF source code and documentation

QPDF QPDF is a command-line tool and C++ library that performs content-preserving transformations on PDF files. It supports linearization, encryption,

QPDF 2.2k Jan 04, 2023
[python3.6] 运用tf实现自然场景文字检测,keras/pytorch实现ctpn+crnn+ctc实现不定长场景文字OCR识别

本文基于tensorflow、keras/pytorch实现对自然场景的文字检测及端到端的OCR中文文字识别 update20190706 为解决本项目中对数学公式预测的准确性,做了其他的改进和尝试,效果还不错,https://github.com/xiaofengShi/Image2Katex 希

xiaofeng 2.7k Dec 25, 2022
Can We Find Neurons that Cause Unrealistic Images in Deep Generative Networks?

Can We Find Neurons that Cause Unrealistic Images in Deep Generative Networks? Artifact Detection/Correction - Offcial PyTorch Implementation This rep

CHOI HWAN IL 23 Dec 20, 2022
🔎 Like Chardet. 🚀 Package for encoding & language detection. Charset detection.

Charset Detection, for Everyone 👋 The Real First Universal Charset Detector A library that helps you read text from an unknown charset encoding. Moti

TAHRI Ahmed R. 332 Dec 31, 2022
Deep LearningImage Captcha 2

滑动验证码深度学习识别 本项目使用深度学习 YOLOV3 模型来识别滑动验证码缺口,基于 https://github.com/eriklindernoren/PyTorch-YOLOv3 修改。 只需要几百张缺口标注图片即可训练出精度高的识别模型,识别效果样例: 克隆项目 运行命令: git cl

Python3WebSpider 117 Dec 28, 2022
Character Segmentation using TensorFlow

Character Segmentation Segment characters and spaces in one text line,from this paper Chinese English mixed Character Segmentation as Semantic Segment

26 Aug 25, 2022
Bu uygulamada Python ve Opencv kullanarak bilgisayar kamerasından yüz tespiti yapıyoruz.

opencv_yuz_bulma Bu uygulamada Python ve Opencv kullanarak bilgisayar kamerasından yüz tespiti yapıyoruz. Bilgisarın kendi kamerasını kullanmak için;

Ahmet Haydar Ornek 6 Apr 16, 2022
How to detect objects in real time by using Jupyter Notebook and Neural Networks , by using Yolo3

Real Time Object Recognition From your Screen Desktop . In this post, I will explain how to build a simply program to detect objects from you desktop

Ruslan Magana Vsevolodovna 2 Sep 28, 2022
Programa que viabiliza a OCR (Optical Character Reading - leitura óptica de caracteres) de um PDF.

Este programa tem o intuito de ser um modificador de arquivos PDF. Os arquivos PDFs podem ser 3: PDFs verdadeiros - em que podem ser selecionados o ti

Daniel Soares Saldanha 2 Oct 11, 2021
Framework for the Complete Gaze Tracking Pipeline

Framework for the Complete Gaze Tracking Pipeline The figure below shows a general representation of the camera-to-screen gaze tracking pipeline [1].

Pascal 20 Jan 06, 2023
Code for paper "Role-based network embedding via structural features reconstruction with degree-regularized constraint"

Role-based network embedding via structural features reconstruction with degree-regularized constraint Train python main.py --dataset brazil-flights

wang zhang 1 Jun 28, 2022
deployment of a hybrid model for automatic weapon detection/ anomaly detection for surveillance applications

Automatic Weapon Detection Deployment of a hybrid model for automatic weapon detection/ anomaly detection for surveillance applications. Loved the pro

Janhavi 4 Mar 04, 2022
DouZero is a reinforcement learning framework for DouDizhu - 斗地主AI

[ICML 2021] DouZero: Mastering DouDizhu with Self-Play Deep Reinforcement Learning | 斗地主AI

Kwai 3.1k Jan 05, 2023
Official code for ROCA: Robust CAD Model Retrieval and Alignment from a Single Image (CVPR 2022)

ROCA: Robust CAD Model Alignment and Retrieval from a Single Image (CVPR 2022) Code release of our paper ROCA. Check out our video, paper, and website

123 Dec 25, 2022
OpenCVを用いたカメラキャリブレーションのサンプルです。2021/06/21時点でPython実装のある3種類(通常カメラ向け、魚眼レンズ向け(fisheyeモジュール)、全方位カメラ向け(omnidirモジュール))について用意しています。

OpenCV-CameraCalibration-Example FishEyeCameraCalibration.mp4 OpenCVを用いたカメラキャリブレーションのサンプルです 2021/06/21時点でPython実装のある以下3種類について用意しています。 通常カメラ向け 魚眼レンズ向け(

KazuhitoTakahashi 34 Nov 17, 2022
A simple demo program for using OpenCV on Android

Kivy OpenCV Demo A simple demo program for using OpenCV on Android Build with: buildozer android debug deploy run Run (on desktop) with: python main.p

Andrea Ranieri 13 Dec 29, 2022
Driver Drowsiness Detection with OpenCV & Dlib

In this project, we have built a driver drowsiness detection system that will detect if the eyes of the driver are close for too long and infer if the driver is sleepy or inactive.

Mansi Mishra 4 Oct 26, 2022
OpenCV-Erlang/Elixir bindings

evision [WIP] : OS : arch Build Status Ubuntu 20.04 arm64 Ubuntu 20.04 armv7 Ubuntu 20.04 s390x Ubuntu 20.04 ppc64le Ubuntu 20.04 x86_64 macOS 11 Big

Cocoa 194 Jan 05, 2023