Skip to content

Commit

Permalink
Merge pull request #10 from danforthcenter/get_centroid
Browse files Browse the repository at this point in the history
Get centroids
  • Loading branch information
nfahlgren authored Apr 22, 2024
2 parents 3a0d074 + 48fd848 commit 9fe576f
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 1 deletion.
44 changes: 44 additions & 0 deletions docs/get_centroids.md
Original file line number Diff line number Diff line change
@@ -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)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ nav:
- 'PlantCV Namespace':
- 'Annotation Tools':
- Points: Points.md
- Get Centroids: get_centroids.md
markdown_extensions:
- toc:
permalink: True
Expand Down
4 changes: 3 additions & 1 deletion plantcv/annotate/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from importlib.metadata import version
from plantcv.annotate.classes import Points
from plantcv.annotate.get_centroids import get_centroids

# Auto versioning
__version__ = version("plantcv-annotate")

__all__ = [
"Points"
"Points",
"get_centroids"
]
28 changes: 28 additions & 0 deletions plantcv/annotate/get_centroids.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ def __init__(self):
self.snapshot_dir = os.path.join(self.datadir, "snapshot_dir")
# RGB image
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")

Expand Down
12 changes: 12 additions & 0 deletions tests/test_annotate_get_centroids.py
Original file line number Diff line number Diff line change
@@ -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)]
Binary file added tests/testdata/setaria_small_plant_mask.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 9fe576f

Please sign in to comment.