@@ -193,7 +193,44 @@ This could be fixed with more complicated segmentation methods
193
193
(outside of the scope of this lesson) like
194
194
[ watershed] ( https://scikit-image.org/docs/dev/auto_examples/segmentation/plot_watershed.html ) .
195
195
196
+ ::::::::::::::::::::::::::::::::::::::: challenge
197
+
198
+ ## Colony counting with minimum size and automated threshold (optional, not included in timing)
199
+
200
+ Modify your function from the previous exercise for colony counting to (i) exclude objects smaller
201
+ than a specified size and (ii) use an automated thresholding approach, e.g. Otsu, to mask the
202
+ colonies.
203
+
204
+ ::::::::::::::::::::::::::::::::::::::: solution
196
205
206
+ Here is a modified function with the requested features. Note when calculating the Otsu threshold we
207
+ don't include the very bright pixels outside the dish.
208
+
209
+ ``` python
210
+ def count_colonies_enhanced (image_filename , sigma = 1.0 , min_colony_size = 10 , connectivity = 2 ):
211
+
212
+ bacteria_image = iio.imread(image_filename)
213
+ gray_bacteria = ski.color.rgb2gray(bacteria_image)
214
+ blurred_image = ski.filters.gaussian(gray_bacteria, sigma = sigma)
215
+
216
+ # create mask excluding the very bright pixels outside the dish
217
+ # we dont want to include these when calculating the automated threshold
218
+ mask = blurred_image < 0.90
219
+ # calculate an automated threshold value within the dish using the Otsu method
220
+ t = ski.filters.threshold_otsu(blurred_image[mask])
221
+ # update mask to select pixels both within the dish and less than t
222
+ mask = np.logical_and(mask, blurred_image < t)
223
+ # remove objects smaller than specified area
224
+ mask = ski.morphology.remove_small_objects(mask, min_size = min_colony_size)
225
+
226
+ labeled_image, count = ski.measure.label(mask, return_num = True )
227
+ print (f " There are { count} colonies in { image_filename} " )
228
+ colored_label_image = ski.color.label2rgb(labeled_image, bg_label = 0 )
229
+ summary_image = ski.color.gray2rgb(gray_bacteria)
230
+ summary_image[mask] = colored_label_image[mask]
231
+ fig, ax = plt.subplots()
232
+ plt.imshow(summary_image)
233
+ ```
197
234
198
235
:::::::::::::::::::::::::
199
236
0 commit comments