Project 7: Measuring worm length

In this project, the length of california blackworms (Lumbriculus variegatus) is measured in 6 well plates, which are detected automatically with the Hough Transform algorithm in detect_mask.

Before

Input - Worms placed in 6 well plates

After

Results - wells and worm body length are detected automatically

Background

Body length is a commonly measured trait. However, live animals are typically not well behaved in front of camera: typically the body is not aligned straight, which makes length measure difficult and sometimes time consuming. Some computer vision can help here, such as skeletonization for an acurate semi automatic approach, and polyline drawing for a less acurate, manual aproach. phenopype’s detect_mask can automatically detect geometric objects using Hough transformation. It takes a while to specific the right parameters for the circle detection - this configuration proved to be useful for the 6-well plates in the images (already preset in gallery_project_7.yaml in the template repo):

- detect_mask:
    ANNOTATION: {type: mask, id: a, edit: overwrite}
    shape: circle
    circle_args: {max_radius: 300, min_radius: 220, param2: 90, param1: 100, min_dist: 400}
    resize: 0.5

Preparation

[2]:
import phenopype as pp
import os

## change working dir - will contain project folder
os.chdir(r"D:\git-repos\phenopype\phenopype-gallery\_temp")

## set project name
project_name = "project_7"

## fetch template from downloaded template repo (https://github.com/phenopype/phenopype-templates)
template_path = r"D:\git-repos\phenopype\phenopype-templates\templates\gallery\project_7.yaml"

## set directory with images
image_dir = r"D:\git-repos\phenopype\phenopype-gallery\gallery\data"
phenopype successfully imported the following plugin dependencies:
phenomorph, keras
[3]:
proj = pp.Project(project_name)
--------------------------------------------
Creating a new phenopype project directory at:
D:\git-repos\phenopype\phenopype-gallery\_temp\project_7

Proceed? (y/n)
y

Project "project_7" successfully created.
--------------------------------------------
[5]:
proj.add_files(image_dir = image_dir, include="worms")
--------------------------------------------
phenopype will search for image files at

D:\git-repos\phenopype\phenopype-gallery\gallery\data

using the following settings:

filetypes: ['jpg', 'JPG', 'jpeg', 'JPEG', 'tif', 'png', 'bmp'], include: worms, exclude: [], mode: copy, recursive: False, resize: False, unique: path

Found image worms1.jpg - phenopype-project folder 0__worms1 created
Found image worms2.jpg - phenopype-project folder 0__worms2 created
Found image worms3.jpg - phenopype-project folder 0__worms3 created

Found 3 files - using all
--------------------------------------------
[6]:
## add the config template; provide a tag
proj.add_config(template_path=template_path, tag="v1")
- template saved under D:\git-repos\phenopype\phenopype-gallery\_temp\project_7\data\0__worms1\pype_config_v1.yaml
- template saved under D:\git-repos\phenopype\phenopype-gallery\_temp\project_7\data\0__worms2\pype_config_v1.yaml
- template saved under D:\git-repos\phenopype\phenopype-gallery\_temp\project_7\data\0__worms3\pype_config_v1.yaml
[7]:
## run image processing
for path in proj.dir_paths:
    pp.Pype(path, tag="v1")


AUTOLOAD
 - nothing to autoload
Stage: add annotation control args
Stage: add annotation control args
Stage: add annotation control args
Stage: add annotation control args
Stage: add annotation control args
Updating pype config: applying staged changes


------------+++ new pype iteration 2022-07-29 11:46:39 +++--------------




PREPROCESSING
detect_mask
- decompose image: using gray channel
- found 1 contours that match criteria
- found 1 contours that match criteria
- found 1 contours that match criteria
- found 1 contours that match criteria
- found 1 contours that match criteria
- found 1 contours that match criteria
Found 6 circles
blur


SEGMENTATION
threshold
- multichannel image supplied, converting to grayscale
- decompose image: using gray channel
- including pixels from 6 drawn masks
detect_contour
- found 92 contours that match criteria
edit_contour
detect_contour
- found 17 contours that match criteria


MEASUREMENT
detect_skeleton


VISUALIZATION
select_canvas
- raw image
draw_contour
draw_mask
draw_polyline


EXPORT
save_annotation
- creating new annotation file
- no annotation_type selected - exporting all annotations
- writing annotations of type "mask" with id "a" to "annotations_v1.json"
- writing annotations of type "contour" with id "a" to "annotations_v1.json"
- writing annotations of type "contour" with id "b" to "annotations_v1.json"
- writing annotations of type "drawing" with id "a" to "annotations_v1.json"
- writing annotations of type "line" with id "a" to "annotations_v1.json"


------------+++ finished pype iteration +++--------------
-------(End with Ctrl+Enter or re-run with Enter)--------


AUTOSHOW
An exception has occurred, use %tb to see the full traceback.

SystemExit:

TERMINATE (by user)

C:\Miniconda3\envs\pp-dev\lib\site-packages\IPython\core\interactiveshell.py:3560: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
[7]:
## this collects all annotations
proj.collect_results(tag="v1", files="annotations",  folder="annotations", overwrite=True)
Created D:\workspace\git-repos\phenopype\phenopype-gallery\_temp\project_7\project\results\annotations
Search string: ['annotations_v1']
Collected annotations_v1.json from 0__worms1
0__worms1_annotations_v1.json saved under D:\workspace\git-repos\phenopype\phenopype-gallery\_temp\project_7\project\results\annotations\0__worms1_annotations_v1.json.
Collected annotations_v1.json from 0__worms2
0__worms2_annotations_v1.json saved under D:\workspace\git-repos\phenopype\phenopype-gallery\_temp\project_7\project\results\annotations\0__worms2_annotations_v1.json.
Collected annotations_v1.json from 0__worms3
0__worms3_annotations_v1.json saved under D:\workspace\git-repos\phenopype\phenopype-gallery\_temp\project_7\project\results\annotations\0__worms3_annotations_v1.json.
[8]:
## display results
import ipyplot ## install with `pip install ipyplot`

canvas_list = []
for path in proj.dir_paths:
    canvas_list.append(pp.load_image(os.path.join(path, "canvas_v1.jpg"), mode="rgb"))

ipyplot.plot_images(canvas_list, img_width=300)
[ ]: