Skip to content

Commit

Permalink
Merge pull request #343 from danforthcenter/cluster_functions
Browse files Browse the repository at this point in the history
Cluster function show_grid
  • Loading branch information
nfahlgren authored Mar 8, 2019
2 parents d41d46a + f65e65f commit ad35fe5
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 26 deletions.
8 changes: 7 additions & 1 deletion docs/cluster_contours.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This function take a image with multiple contours and clusters them based on user input of rows and columns

**platncv.cluster_contours**(*img, roi_objects, roi_obj_hierarchy, nrow=1,ncol=1*)
**platncv.cluster_contours**(*img, roi_objects, roi_obj_hierarchy, nrow=1,ncol=1, show_grid=False*)

**returns** grouped_contour_indexes, contours, hierarchy

Expand All @@ -12,6 +12,7 @@ This function take a image with multiple contours and clusters them based on use
- roi_obj_hierarchy - object hierarchy
- nrow - approximate number of rows (default nrow=1)
- ncol - approximate number of columns (default ncol=1)
- show_grid - if True then a grid gets displayed in debug mode (default show_grid=False)
- **Context:**
- Cluster contours based on number of approximate rows and columns
- **Example use:**
Expand All @@ -32,9 +33,14 @@ pcv.params.debug = "print"

# clusters them based on user input of rows and columns
clusters_i, contours, hierarchy = pcv.cluster_contours(img, roi_objects, roi_obj_hierarchy, 4, 6)
clusters_i, contours, hierarchy = pcv.cluster_contours(img, roi_objects, roi_obj_hierarchy, 4, 6, show_grid=True)
```

**Cluster Contour Image**

![Screenshot](img/documentation_images/cluster_contour/14_clusters.jpg)

**Cluster Contour Image with Grid**

![Screenshot](img/documentation_images/cluster_contour/show_grid.jpg)

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions docs/multi-plant_tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ roi_objects, roi_obj_hierarchy, kept_mask, obj_area = pcv.roi_objects(img1, 'par
# roi_obj_hierarchy = object hierarchy
# nrow = number of rows to cluster (this should be the approximate number of desired rows in the entire image even if there isn't a literal row of plants)
# ncol = number of columns to cluster (this should be the approximate number of desired columns in the entire image even if there isn't a literal row of plants)
# show_grid = if True then a grid gets displayed in debug mode (default show_grid=False)


clusters_i, contours, hierarchies = pcv.cluster_contours(img1, roi_objects, roi_obj_hierarchy, 4, 6)
```
Expand Down Expand Up @@ -561,6 +563,7 @@ def main():
# roi_obj_hierarchy = object hierarchy
# nrow = number of rows to cluster (this should be the approximate number of desired rows in the entire image even if there isn't a literal row of plants)
# ncol = number of columns to cluster (this should be the approximate number of desired columns in the entire image even if there isn't a literal row of plants)
# show_grid = if True then a grid gets displayed in debug mode (default show_grid=False)

clusters_i, contours, hierarchies = pcv.cluster_contours(img1, roi_objects, roi_obj_hierarchy, 4, 6)

Expand Down
7 changes: 4 additions & 3 deletions docs/updating.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ pages for more details on the input and output variable types.

* pre v3.0dev2: device, cropped = **plantcv.auto_crop**(*device, img, objects, padding_x=0, padding_y=0, color='black', debug=None*)
* post v3.0dev2: cropped = **plantcv.auto_crop**(*img, objects, padding_x=0, padding_y=0, color='black'*)
* post v3.2: cropped = **plantcv.auto_crop**(*img, obj, padding_x=0, padding_y=0, color='black*)
* post v3.2: cropped = **plantcv.auto_crop**(*img, obj, padding_x=0, padding_y=0, color='black'*)

#### plantcv.background_subtraction

Expand All @@ -202,7 +202,7 @@ pages for more details on the input and output variable types.
#### plantcv.canny_edge_detect

** pre v3.2: NA
** post v3.2: bin_img = **plantcv.canny_edge_detect**(*img, mask = None, sigma=1.0, low_thresh=None, high_thresh=None, thickness=1, mask_color=None, use_quantiles=False*)
** post v3.2: bin_img = **plantcv.canny_edge_detect**(*img, mask=None, sigma=1.0, low_thresh=None, high_thresh=None, thickness=1, mask_color=None, use_quantiles=False*)

#### plantcv.cluster_contour_splitimg

Expand All @@ -213,6 +213,7 @@ pages for more details on the input and output variable types.

* pre v3.0dev2: device, grouped_contour_indexes, contours, roi_obj_hierarchy = **plantcv.cluster_contours**(*device, img, roi_objects,roi_obj_hierarchy, nrow=1, ncol=1, debug=None*)
* post v3.0dev2: grouped_contour_indexes, contours, roi_obj_hierarchy = **plantcv.cluster_contours**(*img, roi_objects, roi_obj_hierarchy, nrow=1, ncol=1*)
* post v3.2: grouped_contour_indexes, contours, roi_obj_hierarchy = **plantcv.cluster_contours**(*img, roi_objects, roi_object_hierarchy, nrow=1, ncol=1, show_grid=False*)

#### plantcv.color_palette

Expand Down Expand Up @@ -539,7 +540,7 @@ post v3.0: new_img = **plantcv.image_subtract**(*gray_img1, gray_img2*)
#### plantcv.transform.find_color_card

* pre v3.0: NA
* post v3.0: df, start_coord, spacing = **plantcv.transofrm.find_color_card**(*rgb_img, threshold='adaptgauss', threshvalue=125, blurry=False, background='dark'*)
* post v3.0: df, start_coord, spacing = **plantcv.transform.find_color_card**(*rgb_img, threshold='adaptgauss', threshvalue=125, blurry=False, background='dark'*)

#### plantcv.transform.get_color_matrix

Expand Down
34 changes: 13 additions & 21 deletions plantcv/plantcv/cluster_contours.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from plantcv.plantcv import params


def cluster_contours(img, roi_objects, roi_obj_hierarchy, nrow=1, ncol=1):
def cluster_contours(img, roi_objects, roi_obj_hierarchy, nrow=1, ncol=1, show_grid=False):

"""
This function take a image with multiple contours and clusters them based on user input of rows and columns
Expand All @@ -20,8 +20,7 @@ def cluster_contours(img, roi_objects, roi_obj_hierarchy, nrow=1, ncol=1):
in the entire image (even if there isn't a literal row of plants)
ncol = number of columns to cluster (this should be the approximate number of desired columns
in the entire image (even if there isn't a literal row of plants)
file = output of filename from read_image function
filenames = input txt file with list of filenames in order from top to bottom left to right
show_grid = if True then the grid will get plot to show how plants are being clustered
Returns:
grouped_contour_indexes = contours grouped
Expand All @@ -31,6 +30,7 @@ def cluster_contours(img, roi_objects, roi_obj_hierarchy, nrow=1, ncol=1):
:param roi_objects: list
:param nrow: int
:param ncol: int
:param show_grid: bool
:return grouped_contour_indexes: list
:return contours: list
:return roi_obj_hierarchy: list
Expand Down Expand Up @@ -123,7 +123,7 @@ def digitize(a, step):

# Debug image is rainbow printed contours

if params.debug == 'print':
if params.debug is not None:
if len(np.shape(img)) == 3:
img_copy = np.copy(img)
else:
Expand All @@ -137,22 +137,14 @@ def digitize(a, step):
pass
else:
cv2.drawContours(img_copy, roi_objects, a, rand_color[i], -1, hierarchy=roi_obj_hierarchy)
print_image(img_copy, os.path.join(params.debug_outdir, str(params.device) + '_clusters.png'))

elif params.debug == 'plot':
if len(np.shape(img)) == 3:
img_copy = np.copy(img)
else:
iy, ix = np.shape(img)
img_copy = np.zeros((iy, ix, 3), dtype=np.uint8)

rand_color = color_palette(len(coordlist))
for i, x in enumerate(coordlist):
for a in x:
if roi_obj_hierarchy[0][a][3] > -1:
pass
else:
cv2.drawContours(img_copy, roi_objects, a, rand_color[i], -1, hierarchy=roi_obj_hierarchy)
plot_image(img_copy)
if show_grid:
for y in rbreaks:
cv2.line(img_copy, (0, y), (ix, y), (255, 0, 0), params.line_thickness)
for x in cbreaks:
cv2.line(img_copy, (x, 0), (x, iy), (255, 0, 0), params.line_thickness)
if params.debug=='print':
print_image(img_copy, os.path.join(params.debug_outdir, str(params.device) + '_clusters.png'))
elif params.debug=='plot':
plot_image(img_copy)

return grouped_contour_indexes, contours, roi_obj_hierarchy
2 changes: 1 addition & 1 deletion tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ def test_plantcv_cluster_contours():
# Test with debug = "print"
pcv.params.debug = "print"
_ = pcv.cluster_contours(img=img1, roi_objects=objs, roi_obj_hierarchy=obj_hierarchy, nrow=4, ncol=6)
_ = pcv.cluster_contours(img=img1, roi_objects=objs, roi_obj_hierarchy=obj_hierarchy)
_ = pcv.cluster_contours(img=img1, roi_objects=objs, roi_obj_hierarchy=obj_hierarchy, show_grid=True)
# Test with debug = "plot"
pcv.params.debug = "plot"
_ = pcv.cluster_contours(img=img1, roi_objects=objs, roi_obj_hierarchy=obj_hierarchy, nrow=4, ncol=6)
Expand Down

0 comments on commit ad35fe5

Please sign in to comment.