-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from danforthcenter/30-napari-draw-mask
napari draw mask & remove "shape" param from napari_label_classes
- Loading branch information
Showing
20 changed files
with
171 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
## Make Mask of Napari Points | ||
|
||
This function is to generate a mask from Napari point information. | ||
This application of this function could be to get information about image | ||
at particular points (e.g. color or intensity information) | ||
|
||
**plantcv.annotate.napari_points_mask**(*img, viewer*) | ||
|
||
**returns** dictionary of masks (one for each class where the class label is the key to access) | ||
|
||
- **Parameters:** | ||
- img - image data (compatible with gray, RGB, and hyperspectral data. If data is hyperspecral it should be the array e.g. hyperspectral.array_data) | ||
- viewer = Napari Viewer with point classes labeled (likely created with [`napari_label_classes`](napari_label_classes.md)). The size of the points in the mask will be determined from the viewer parameters. | ||
|
||
- **Context:** | ||
- This function can be used to generate a mask from Napari points in order to get information about point data. | ||
|
||
- **Example use:** | ||
- An application of this function might be collection of color data for the [Naive Bayes module](https://plantcv.readthedocs.io/en/latest/tutorials/machine_learning_tutorial/). | ||
|
||
|
||
```python | ||
import plantcv.plantcv as pcv | ||
import plantcv.annotate as pcvan | ||
import napari | ||
|
||
# Create an instance of the Points class | ||
img, path, name = pcv.readimage("./wheat.png") | ||
|
||
# Should open interactive napari viewer | ||
viewer = pcvan.napari_label_classes(img=img, classes=['background','healthy', 'rust', 'chlorosis'], size=4) | ||
|
||
maskdict = pcvan.napari_points_mask(img, viewer) | ||
|
||
pcv.plot_image(maskdict['background']) | ||
pcv.plot_image(maskdict['healthy']) | ||
pcv.plot_image(maskdict['rust']) | ||
pcv.plot_image(maskdict['chlorosis']) | ||
|
||
``` | ||
|
||
![Screenshot](img/documentation_images/napari_points_mask/viewer_labeled.png) | ||
|
||
***Background Mask*** | ||
|
||
![Screenshot](img/documentation_images/napari_points_mask/background.png) | ||
|
||
***Healthy Mask*** | ||
|
||
![Screenshot](img/documentation_images/napari_points_mask/healthy.png) | ||
|
||
***Rust Mask*** | ||
|
||
![Screenshot](img/documentation_images/napari_points_mask/rust.png) | ||
|
||
***Chlorosis Mask*** | ||
|
||
![Screenshot](img/documentation_images/napari_points_mask/chlorosis.png) | ||
|
||
**Source Code:** [Here](https://github.com/danforthcenter/plantcv-annotate/blob/main/plantcv/annotate/napari_points_mask.py) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Make Masks of Labelled Napari Points | ||
|
||
import numpy as np | ||
import os | ||
import cv2 | ||
from plantcv.annotate import napari_classes | ||
from plantcv.plantcv import params | ||
from plantcv.plantcv._debug import _debug | ||
|
||
|
||
def napari_points_mask(img, viewer): | ||
""" | ||
draw points mask based on Napari viewer annotations | ||
Inputs: | ||
img = img (grayimg, rgbimg, or hyperspectral image array data | ||
e.g. hyperspectraldata.array_data). This is used to find the x,y size | ||
of the image | ||
viewer = Napari Viewer with classes labeled. The size of the masked points | ||
will be from the viewer parameters | ||
Returns: | ||
mask_dict = dictionary of masks; mask for each labelled class | ||
:param img: numpy.ndarray | ||
:param viewer: Napari Viewer object | ||
:param shape: str | ||
:return mask_dict: dict of numpy.ndarray | ||
""" | ||
# get shape of image | ||
size = np.shape(img) | ||
keys = napari_classes(viewer) | ||
maskdict = {} | ||
|
||
for key in keys: | ||
maskname = str(key) | ||
mask = np.zeros((size[0], size[1])) | ||
data = list(viewer.layers[key].data) | ||
shapesize = viewer.layers[key]._current_size | ||
shapesizehalf = int(shapesize/2) | ||
|
||
for y, x in data: | ||
startpoint = (int(x-shapesizehalf), int(y-shapesizehalf)) | ||
endpoint = (int(x+shapesizehalf-1), int(y+shapesizehalf-1)) | ||
mask = cv2.rectangle(mask, startpoint, endpoint, (255), -1) | ||
|
||
maskdict[maskname] = mask | ||
|
||
_debug(visual=mask, filename=os.path.join(params.debug_outdir, | ||
str(params.device) + | ||
str(maskname) + | ||
'_labeled_mask.png')) | ||
|
||
return maskdict |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from plantcv.annotate import napari_points_mask | ||
from plantcv.plantcv import readimage | ||
from plantcv.annotate import napari_label_classes | ||
import numpy as np | ||
|
||
|
||
def test_napari_points_mask(test_data): | ||
"""Test for PlantCV.Annotate""" | ||
# Read in test data | ||
img, _, _ = readimage(test_data.small_rgb_img) | ||
data = {'total': [(25, 25)], 'background': [(50, 50)]} | ||
viewer = napari_label_classes(img, ['total'], size=50, importdata=data, | ||
show=False) | ||
maskdict = napari_points_mask(img, viewer) | ||
|
||
summask = int((np.sum(maskdict['total']))/255) | ||
|
||
assert summask == 2500 | ||
viewer.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters