Skip to content

Commit

Permalink
differences for PR #317
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Dec 13, 2023
1 parent 456bfd3 commit 8ed520b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
41 changes: 41 additions & 0 deletions 09-challenges.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,48 @@ This could be fixed with more complicated segmentation methods
(outside of the scope of this lesson) like
[watershed](https://scikit-image.org/docs/dev/auto_examples/segmentation/plot_watershed.html).

:::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::::: challenge

## Colony counting with minimum size and automated threshold (optional, not included in timing)

Modify your function from the previous exercise for colony counting to (i) exclude objects smaller
than a specified size and (ii) use an automated thresholding approach, e.g. Otsu, to mask the
colonies.

::::::::::::::::::::::::::::::::::::::: solution

Here is a modified function with the requested features. Note when calculating the Otsu threshold we
don't include the very bright pixels outside the dish.

```python
def count_colonies_enhanced(image_filename, sigma=1.0, min_colony_size=10, connectivity=2):

bacteria_image = iio.imread(image_filename)
gray_bacteria = ski.color.rgb2gray(bacteria_image)
blurred_image = ski.filters.gaussian(gray_bacteria, sigma=sigma)

# create mask excluding the very bright pixels outside the dish
# we dont want to include these when calculating the automated threshold
mask = blurred_image < 0.90
# calculate an automated threshold value within the dish using the Otsu method
t = ski.filters.threshold_otsu(blurred_image[mask])
# update mask to select pixels both within the dish and less than t
mask = np.logical_and(mask, blurred_image < t)
# remove objects smaller than specified area
mask = ski.morphology.remove_small_objects(mask, min_size=min_colony_size)

labeled_image, count = ski.measure.label(mask, return_num=True)
print(f"There are {count} colonies in {image_filename}")
colored_label_image = ski.color.label2rgb(labeled_image, bg_label=0)
summary_image = ski.color.gray2rgb(gray_bacteria)
summary_image[mask] = colored_label_image[mask]
fig, ax = plt.subplots()
plt.imshow(summary_image)
```

:::::::::::::::::::::::::

Expand Down
2 changes: 1 addition & 1 deletion md5sum.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"episodes/06-blurring.md" "8d109bb4c49f27f54857f6d35b4c6b9a" "site/built/06-blurring.md" "2023-12-08"
"episodes/07-thresholding.md" "7ae5260f90e1df8e20a6226cce8ec6b6" "site/built/07-thresholding.md" "2023-09-05"
"episodes/08-connected-components.md" "59d42797208c5bf569da2fa2e4dd05df" "site/built/08-connected-components.md" "2023-09-09"
"episodes/09-challenges.md" "a3ace24af8f5cb0bda1e9379a688ad4c" "site/built/09-challenges.md" "2023-09-05"
"episodes/09-challenges.md" "655bdca8cab5d28dc6b2c2e9275aaecc" "site/built/09-challenges.md" "2023-12-13"
"instructors/instructor-notes.md" "b1c166a544eb4b9b91f3ac8f2dafd549" "site/built/instructor-notes.md" "2023-05-12"
"learners/discuss.md" "ad762c335f99400dc2cd1a8aad36bdbd" "site/built/discuss.md" "2023-07-26"
"learners/edge-detection.md" "fdbcee7436e104e6587e1cb40cd37d18" "site/built/edge-detection.md" "2023-08-16"
Expand Down

0 comments on commit 8ed520b

Please sign in to comment.