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