diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index 7a7852e..f64c7e5 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -16,7 +16,7 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - python-version: ['3.8', '3.9', '3.10'] + python-version: ['3.9', '3.10', '3.11'] os: [ubuntu-latest] env: OS: ${{ matrix.os }} diff --git a/docs/get_centroids.md b/docs/get_centroids.md new file mode 100644 index 0000000..87dbb08 --- /dev/null +++ b/docs/get_centroids.md @@ -0,0 +1,44 @@ +## Get Centroids + +Extract the centroid coordinate (column,row) from regions in a binary image. + +**plantcv.annotate.get_centroids**(*bin_img*) + +**returns** list containing coordinates of centroids + +- **Parameters:** + - bin_img - Binary image containing the connected regions to consider +- **Context:** + - Given an arbitrary mask of the objects of interest, `get_centroids` + returns a list of coordinates that can the be imported into the annotation class [Points](Points.md). + +- **Example use:** + - Below + +**Binary image** + +![count_img](img/documentation_images/get_centroids/discs_mask.png) + +```python + +from plantcv import plantcv as pcv + +# Set global debug behavior to None (default), "print" (to file), +# or "plot" +pcv.params.debug = "plot" + +# Apply get centroids to the binary image +coords = pcv.annotate.get_centroids(bin_img=binary_img) +print(coords) +# [[1902, 600], [1839, 1363], [1837, 383], [1669, 1977], [1631, 1889], [1590, 1372], [1550, 1525], +# [1538, 1633], [1522, 1131], [1494, 2396], [1482, 1917], [1446, 1808], [1425, 726], [1418, 2392], +# [1389, 198], [1358, 1712], [1288, 522], [1289, 406], [1279, 368], [1262, 1376], [1244, 1795], +# [1224, 1327], [1201, 624], [1181, 725], [1062, 85], [999, 840], [885, 399], [740, 324], [728, 224], +# [697, 860], [660, 650], [638, 2390], [622, 1565], [577, 497], [572, 2179], [550, 2230], [547, 1826], +# [537, 892], [538, 481], [524, 2144], [521, 2336], [497, 201], [385, 1141], [342, 683], [342, 102], +# [332, 1700], [295, 646], [271, 60], [269, 1626], [210, 1694], [189, 878], [178, 1570], [171, 2307], +# [61, 286], [28, 2342]] + +``` + +**Source Code:** [Here](https://github.com/danforthcenter/plantcv-annotate/blob/main/plantcv/annotate/get_centroids.py) diff --git a/docs/img/documentation_images/get_centroids/discs_mask.png b/docs/img/documentation_images/get_centroids/discs_mask.png new file mode 100644 index 0000000..357fc23 Binary files /dev/null and b/docs/img/documentation_images/get_centroids/discs_mask.png differ diff --git a/mkdocs.yml b/mkdocs.yml index ed8c5af..eff0192 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -22,7 +22,7 @@ nav: - Napari Label: napari_label_classes.md - Napari Open: napari_open.md - Points: Points.md - + - Get Centroids: get_centroids.md markdown_extensions: - toc: permalink: True diff --git a/plantcv/annotate/__init__.py b/plantcv/annotate/__init__.py index 7f7214e..395de61 100644 --- a/plantcv/annotate/__init__.py +++ b/plantcv/annotate/__init__.py @@ -1,12 +1,19 @@ +from importlib.metadata import version +from plantcv.annotate.classes import Points +from plantcv.annotate.get_centroids import get_centroids from plantcv.annotate.napari_classes import napari_classes from plantcv.annotate.napari_open import napari_open from plantcv.annotate.napari_label_classes import napari_label_classes from plantcv.annotate.napari_join_labels import napari_join_labels +# Auto versioning +__version__ = version("plantcv-annotate") __all__ = [ + "Points", + "get_centroids", "napari_classes", "napari_open", "napari_label_classes", "napari_join_labels" -] + ] diff --git a/plantcv/annotate/classes.py b/plantcv/annotate/classes.py new file mode 100644 index 0000000..5134742 --- /dev/null +++ b/plantcv/annotate/classes.py @@ -0,0 +1,160 @@ +# Class helpers + +# Imports +import cv2 +import json +from math import floor +import matplotlib.pyplot as plt +from plantcv.plantcv.annotate.points import _find_closest_pt +from plantcv.plantcv import warn + + +class Points: + """Point annotation/collection class to use in Jupyter notebooks. It allows the user to + interactively click to collect coordinates from an image. Left click collects the point and + right click removes the closest collected point. + """ + + def __init__(self, img, figsize=(12, 6), label="default", color="r", view_all=False): + """Points initialization method. + + Parameters + ---------- + img : numpy.ndarray + image to annotate + figsize : tuple, optional + figure plotting size, by default (12, 6) + label : str, optional + class label, by default "default" + """ + self.img = img + self.figsize = figsize + self.label = label # current label + self.color = color # current color + self.view_all = view_all # a flag indicating whether or not view all labels + self.coords = {} # dictionary of all coordinates per group label + self.events = [] # includes right and left click events + self.count = {} # a dictionary that saves the counts of different groups (labels) + self.sample_labels = [] # list of all sample labels, one to one with points collected + self.colors = {} # all used colors + + self.view(label=self.label, color=self.color, view_all=self.view_all) + + def onclick(self, event): + """Handle mouse click events + + Parameters + ---------- + event : matplotlib.backend_bases.MouseEvent + matplotlib MouseEvent object + """ + print(type(event)) + self.events.append(event) + if event.button == 1: + # Add point to the plot + self.ax.plot(event.xdata, event.ydata, marker='x', c=self.color) + self.coords[self.label].append((floor(event.xdata), floor(event.ydata))) + self.count[self.label] += 1 + self.sample_labels.append(self.label) + else: + idx_remove, _ = _find_closest_pt((event.xdata, event.ydata), self.coords[self.label]) + # remove the closest point to the user right clicked one + self.coords[self.label].pop(idx_remove) + self.count[self.label] -= 1 + idx_remove = idx_remove + self.p_not_current + self.ax.lines[idx_remove].remove() + self.sample_labels.pop(idx_remove) + self.fig.canvas.draw() + + def print_coords(self, filename): + """Save collected coordinates to a file. + + Parameters + ---------- + filename : str + output filename + """ + # Open the file for writing + with open(filename, "w") as fp: + # Save the data in JSON format with indentation + json.dump(obj=self.coords, fp=fp, indent=4) + + def import_list(self, coords, label="default"): + """Import coordinates. + + Parameters + ---------- + coords : list + list of coordinates (tuples) + label : str, optional + class label, by default "default" + """ + if label not in self.coords: + self.coords[label] = [] + for (x, y) in coords: + self.coords[label].append((x, y)) + self.count[label] = len(self.coords[label]) + self.view(label=label, color=self.color, view_all=False) + else: + warn(f"{label} already included and counted, nothing is imported!") + + def import_file(self, filename): + """Import coordinates from a file. + + Parameters + ---------- + filename : str + JSON file containing Points annotations + """ + with open(filename, "r") as fp: + coords = json.load(fp) + + keys = list(coords.keys()) + + for key in keys: + keycoor = coords[key] + keycoor = list(map(lambda sub: (sub[1], sub[0]), keycoor)) + self.import_list(keycoor, label=key) + + def view(self, label="default", color="r", view_all=False): + """View coordinates for a specific class label. + + Parameters + ---------- + label : str, optional + class label, by default "default" + color : str, optional + marker color, by default "r" + view_all : bool, optional + view all classes or a single class, by default False + """ + if label not in self.coords and color in self.colors.values(): + warn("The color assigned to the new class label is already used, if proceeding, " + "items from different classes will not be distinguishable in plots!") + self.label = label + self.color = color + self.view_all = view_all + + if self.label not in self.coords: + self.coords[self.label] = [] + self.count[self.label] = 0 + self.colors[self.label] = self.color + + self.fig, self.ax = plt.subplots(1, 1, figsize=self.figsize) + + self.events = [] + self.fig.canvas.mpl_connect('button_press_event', self.onclick) + + self.ax.imshow(cv2.cvtColor(self.img, cv2.COLOR_BGR2RGB)) + self.ax.set_title("Please left click on objects\n Right click to remove") + self.p_not_current = 0 + # if view_all is True, show all already marked markers + if self.view_all: + for k in self.coords: + for (x, y) in self.coords[k]: + self.ax.plot(x, y, marker='x', c=self.colors[k]) + if self.label not in self.coords or len(self.coords[self.label]) == 0: + self.p_not_current += 1 + else: + for (x, y) in self.coords[self.label]: + self.ax.plot(x, y, marker='x', c=self.color) diff --git a/plantcv/annotate/get_centroids.py b/plantcv/annotate/get_centroids.py new file mode 100644 index 0000000..8fbf46b --- /dev/null +++ b/plantcv/annotate/get_centroids.py @@ -0,0 +1,28 @@ +# Get centroids from mask objects + +from skimage.measure import label, regionprops + + +def get_centroids(bin_img): + """Get the coordinates (row,column) of the centroid of each connected region in a binary image. + + Inputs: + bin_img = Binary image containing the connected regions to consider + + Returns: + coords = List of coordinates (row,column) of the centroids of the regions + + :param bin_img: numpy.ndarray + :return coords: list + """ + # find contours in the binary image + labeled_img = label(bin_img) + # measure regions + obj_measures = regionprops(labeled_img) + coords = [] + for obj in obj_measures: + # Convert coord values to int + coord = tuple(map(int, obj.centroid)) + coords.append(coord) + + return coords diff --git a/pyproject.toml b/pyproject.toml index 8a6251d..5abfaf1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,5 +1,5 @@ [build-system] -requires = ["setuptools >= 61.0"] +requires = ["setuptools >= 64.0", "setuptools_scm>=8"] build-backend = "setuptools.build_meta" [tool.setuptools.packages.find] @@ -27,12 +27,17 @@ classifiers = [ "License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)", "Intended Audience :: Science/Research", ] + [project.optional-dependencies] test = [ "pytest", + "pytest-cov", "pytest-qt" ] + [project.urls] Homepage = "https://plantcv.org" Documentation = "https://plantcv.readthedocs.io" -Repository = "https://github.com/danforthcenter/plantcv-annotate" \ No newline at end of file +Repository = "https://github.com/danforthcenter/plantcv-annotate" + +[tool.setuptools_scm] diff --git a/tests/conftest.py b/tests/conftest.py index 2e840e8..4023056 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,6 +1,6 @@ +import os import pytest import matplotlib -import os # Disable plotting @@ -11,17 +11,19 @@ class TestData: def __init__(self): """Initialize simple variables.""" # Test data directory - self.datadir = os.path.join(os.path.dirname(os.path.abspath(__file__)), - "testdata") + self.datadir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "testdata") + # Flat image directory + self.snapshot_dir = os.path.join(self.datadir, "snapshot_dir") # RGB image - filename_rgb = "setaria_small_plant_rgb.png" - self.small_rgb_img = os.path.join(self.datadir, filename_rgb) + self.small_rgb_img = os.path.join(self.datadir, "setaria_small_plant_rgb.png") + # Binary image + self.small_bin_img = os.path.join(self.datadir, "setaria_small_plant_mask.png") + # Text file with tuple coordinates (by group label) + self.pollen_coords = os.path.join(self.datadir, "points_file_import.coords") # Kmeans Clustered Gray image - filename_kmeans = "silphium_seed_labeled_example.png" - self.kmeans_seed_gray_img = os.path.join(self.datadir, filename_kmeans) + self.kmeans_seed_gray_img = os.path.join(self.datadir, "silphium_seed_labeled_example.png") # Small Hyperspectral image - filename_hyper = "corn-kernel-hyperspectral.raw" - self.envi_sample_data = os.path.join(self.datadir, filename_hyper) + self.envi_sample_data = os.path.join(self.datadir, "corn-kernel-hyperspectral.raw") @pytest.fixture(scope="session") diff --git a/tests/test_annotate_get_centroids.py b/tests/test_annotate_get_centroids.py new file mode 100644 index 0000000..54c4db3 --- /dev/null +++ b/tests/test_annotate_get_centroids.py @@ -0,0 +1,12 @@ +import cv2 +from plantcv.annotate.get_centroids import get_centroids + + +def test_get_centroids(test_data): + """Test for PlantCV.""" + # Read in test data + mask = cv2.imread(test_data.small_bin_img, -1) + + coor = get_centroids(bin_img=mask) + + assert coor == [(166, 214)] diff --git a/tests/test_annotate_points.py b/tests/test_annotate_points.py new file mode 100644 index 0000000..f3b91e1 --- /dev/null +++ b/tests/test_annotate_points.py @@ -0,0 +1,152 @@ +"""Tests for annotate.Points.""" +import os +import cv2 +import matplotlib +from plantcv.annotate.classes import Points + + +def test_points(test_data): + """Test for plantcv-annotate.""" + # Read in a test grayscale image + img = cv2.imread(test_data.small_rgb_img) + + # initialize interactive tool + drawer_rgb = Points(img, figsize=(12, 6)) + + # simulate mouse clicks + # event 1, left click to add point + e1 = matplotlib.backend_bases.MouseEvent(name="button_press_event", canvas=drawer_rgb.fig.canvas, + x=0, y=0, button=1) + point1 = (200, 200) + e1.xdata, e1.ydata = point1 + drawer_rgb.onclick(e1) + + # event 2, left click to add point + e2 = matplotlib.backend_bases.MouseEvent(name="button_press_event", canvas=drawer_rgb.fig.canvas, + x=0, y=0, button=1) + e2.xdata, e2.ydata = (300, 200) + drawer_rgb.onclick(e2) + + # event 3, left click to add point + e3 = matplotlib.backend_bases.MouseEvent(name="button_press_event", canvas=drawer_rgb.fig.canvas, + x=0, y=0, button=1) + e3.xdata, e3.ydata = (50, 50) + drawer_rgb.onclick(e3) + + # event 4, right click to remove point with exact coordinates + e4 = matplotlib.backend_bases.MouseEvent(name="button_press_event", canvas=drawer_rgb.fig.canvas, + x=0, y=0, button=3) + e4.xdata, e4.ydata = (50, 50) + drawer_rgb.onclick(e4) + + # event 5, right click to remove point with coordinates close but not equal + e5 = matplotlib.backend_bases.MouseEvent(name="button_press_event", canvas=drawer_rgb.fig.canvas, + x=0, y=0, button=3) + e5.xdata, e5.ydata = (301, 200) + drawer_rgb.onclick(e5) + + assert drawer_rgb.coords["default"][0] == point1 + +def test_points_print_coords(test_data, tmpdir): + """Test for plantcv-annotate.""" + cache_dir = tmpdir.mkdir("cache") + filename = os.path.join(cache_dir, 'plantcv_print_coords.txt') + # Read in a test image + img = cv2.imread(test_data.small_rgb_img) + + # initialize interactive tool + drawer_rgb = Points(img, figsize=(12, 6)) + + # simulate mouse clicks + # event 1, left click to add point + e1 = matplotlib.backend_bases.MouseEvent(name="button_press_event", canvas=drawer_rgb.fig.canvas, + x=0, y=0, button=1) + point1 = (200, 200) + e1.xdata, e1.ydata = point1 + drawer_rgb.onclick(e1) + + # event 2, left click to add point + e2 = matplotlib.backend_bases.MouseEvent(name="button_press_event", canvas=drawer_rgb.fig.canvas, + x=0, y=0, button=1) + e2.xdata, e2.ydata = (300, 200) + drawer_rgb.onclick(e2) + + # Save collected coords out + drawer_rgb.print_coords(filename) + assert os.path.exists(filename) + +def test_points_import_list(test_data): + """Test for plantcv-annotate.""" + # Read in a test image + img = cv2.imread(test_data.small_rgb_img) + # initialize interactive tool + drawer_rgb = Points(img, figsize=(12, 6), label="default") + totalpoints1 = [(158, 531), (361, 112), (500, 418), (269.25303806488864, 385.69839981447126), + (231.21964288863632, 445.995245825603), (293.37177646934134, 448.778177179963), (240.49608073650273, 277.1640769944342), + (279.4571196975417, 240.05832560296852), (77.23077461405376, 165.84682282003712), (420, 364), + (509.5127783246289, 353.2308673469388), (527.1380102355752, 275.3087894248609), (445.50535717435065, 138.94515306122452)] + drawer_rgb.import_list(coords=totalpoints1, label="imported") + + assert len(drawer_rgb.coords["imported"]) == 13 + +def test_points_import_list_warn(test_data): + """Test for plantcv-annotate.""" + # Read in a test image + img = cv2.imread(test_data.small_rgb_img) + # initialize interactive tool + drawer_rgb = Points(img, figsize=(12, 6), label="default") + totalpoints1 = [(158, 531), (361, 112), (500, 418), (445.50535717435065, 138.94515306122452)] + drawer_rgb.import_list(coords=totalpoints1) + + assert len(drawer_rgb.coords["default"]) == 0 + +def test_points_import_file(test_data): + """Test for plantcv-annotate.""" + img = cv2.imread(test_data.small_rgb_img) + counter = Points(img, figsize=(8, 6)) + file = test_data.pollen_coords + counter.import_file(file) + + assert counter.count['total'] == 70 + +def test_points_view(test_data): + """Test for plantcv-annotate.""" + # Read in a test grayscale image + img = cv2.imread(test_data.small_rgb_img) + + # initialize interactive tool + drawer_rgb = Points(img, figsize=(12, 6)) + + # simulate mouse clicks + # event 1, left click to add point + e1 = matplotlib.backend_bases.MouseEvent(name="button_press_event", canvas=drawer_rgb.fig.canvas, + x=0, y=0, button=1) + point1 = (200, 200) + e1.xdata, e1.ydata = point1 + drawer_rgb.onclick(e1) + drawer_rgb.view(label="new", view_all=True) + e2 = matplotlib.backend_bases.MouseEvent(name="button_press_event", canvas=drawer_rgb.fig.canvas, + x=0, y=0, button=1) + e2.xdata, e2.ydata = (300, 200) + drawer_rgb.onclick(e2) + drawer_rgb.view(view_all=False) + + assert str(drawer_rgb.fig) == "Figure(1200x600)" + +def test_points_view_warn(test_data): + """Test for plantcv-annotate.""" + # Read in a test grayscale image + img = cv2.imread(test_data.small_rgb_img) + + # initialize interactive tool, implied default label and "r" color + drawer_rgb = Points(img, figsize=(12, 6)) + + # simulate mouse clicks, event 1=left click to add point + e1 = matplotlib.backend_bases.MouseEvent(name="button_press_event", canvas=drawer_rgb.fig.canvas, + x=0, y=0, button=1) + point1 = (200, 200) + e1.xdata, e1.ydata = point1 + drawer_rgb.onclick(e1) + drawer_rgb.view(label="new", color='r') + + assert str(drawer_rgb.fig) == "Figure(1200x600)" diff --git a/tests/testdata/points_file_import.coords b/tests/testdata/points_file_import.coords new file mode 100644 index 0000000..2be1117 --- /dev/null +++ b/tests/testdata/points_file_import.coords @@ -0,0 +1,306 @@ +{ + "total": [ + [ + 1524, + 59 + ], + [ + 1642, + 78 + ], + [ + 723, + 159 + ], + [ + 1711, + 218 + ], + [ + 736, + 263 + ], + [ + 1431, + 275 + ], + [ + 1573, + 338 + ], + [ + 531, + 358 + ], + [ + 1437, + 365 + ], + [ + 904, + 380 + ], + [ + 1255, + 387 + ], + [ + 1140, + 417 + ], + [ + 112, + 561 + ], + [ + 2056, + 690 + ], + [ + 418, + 700 + ], + [ + 1738, + 703 + ], + [ + 1255, + 959 + ], + [ + 1791, + 1042 + ], + [ + 1310, + 1065 + ], + [ + 902, + 1084 + ], + [ + 1319, + 1131 + ], + [ + 1084, + 1156 + ], + [ + 457, + 1183 + ], + [ + 1763, + 1190 + ], + [ + 1407, + 1232 + ], + [ + 2053, + 1283 + ], + [ + 1140, + 1383 + ], + [ + 566, + 1393 + ], + [ + 240, + 1403 + ], + [ + 1681, + 1421 + ], + [ + 398, + 470 + ], + [ + 438, + 432 + ], + [ + 446, + 502 + ], + [ + 919, + 252 + ], + [ + 947, + 204 + ], + [ + 997, + 219 + ], + [ + 967, + 267 + ], + [ + 1034, + 400 + ], + [ + 1074, + 364 + ], + [ + 1104, + 329 + ], + [ + 1165, + 267 + ], + [ + 1235, + 304 + ], + [ + 1272, + 262 + ], + [ + 1297, + 299 + ], + [ + 1350, + 249 + ], + [ + 1387, + 229 + ], + [ + 1488, + 492 + ], + [ + 1565, + 202 + ], + [ + 1595, + 247 + ], + [ + 1633, + 274 + ], + [ + 839, + 101 + ], + [ + 861, + 162 + ], + [ + 914, + 46 + ], + [ + 1052, + 56 + ], + [ + 1102, + 149 + ], + [ + 1150, + 127 + ], + [ + 280, + 442 + ], + [ + 240, + 477 + ], + [ + 93, + 490 + ], + [ + 270, + 733 + ], + [ + 351, + 713 + ], + [ + 363, + 627 + ], + [ + 215, + 1036 + ], + [ + 498, + 973 + ], + [ + 606, + 1224 + ], + [ + 646, + 1264 + ], + [ + 353, + 1244 + ], + [ + 343, + 1284 + ], + [ + 300, + 1244 + ], + [ + 323, + 1196 + ] + ], + "germinated": [ + [ + 1485, + 494 + ], + [ + 95, + 481 + ], + [ + 1588, + 974 + ], + [ + 1994, + 1039 + ], + [ + 1901, + 198 + ] + ] +} \ No newline at end of file diff --git a/tests/testdata/setaria_small_plant_mask.png b/tests/testdata/setaria_small_plant_mask.png new file mode 100644 index 0000000..2c990da Binary files /dev/null and b/tests/testdata/setaria_small_plant_mask.png differ