diff --git a/notebooks/04_feature_extraction.ipynb b/notebooks/04_feature_extraction.ipynb index b61549a8d5beb3b35c9dcd8e1e1a149a99dc3f88..6a64a54650775b41e2d15d2aac82caa9a7ff0e40 100644 --- a/notebooks/04_feature_extraction.ipynb +++ b/notebooks/04_feature_extraction.ipynb @@ -1043,35 +1043,69 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "jupyter": { - "source_hidden": true - } - }, + "metadata": {}, "outputs": [], "source": [ - "# Solution\n", + "# Solution cell 1\n", "\n", + "# compute distance transform from masks\n", "distance = ndi.distance_transform_edt(mask_blobs)\n", + "# blur helps reduce the noise and minor irregularities in the distance values \n", "blurred_distance = skimage.filters.gaussian(distance, sigma=2)\n", - "fp = np.ones((3,) * mask_blobs.ndim)\n", + "fp = np.ones((3,3))\n", "\n", + "# compute local max of blurred distance map\n", "coords = skimage.feature.peak_local_max(blurred_distance, footprint=fp, labels=mask_blobs)\n", + "\n", + "# create an array with labeled markers \n", "mask = np.zeros(distance.shape, dtype=bool)\n", "mask[tuple(coords.T)] = True\n", "markers = skimage.measure.label(mask)\n", + "# apply the watershed algorithm\n", "labels = skimage.segmentation.watershed(-blurred_distance, markers, mask=mask_blobs)\n", "\n", + "plt.subplots(figsize=(10,10))\n", + "plt.imshow(labels)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Solution cell 2\n", + "\n", + "# now if you really want to \"split\" the objects, you can do the following\n", + "# first detect edges from the labels after watershed\n", "edges = skimage.filters.sobel(labels)\n", + "# then detect edges from the mask_blobs before watershed\n", "edges2 = skimage.filters.sobel(mask_blobs)\n", "\n", - "almost = np.logical_not(np.logical_xor(edges != 0, edges2 != 0)) * mask_blobs\n", - "output = skimage.morphology.binary_opening(almost)\n", + "fig, ax = plt.subplots(1, 2, figsize=(10,10))\n", + "ax[0].imshow(edges)\n", + "ax[1].imshow(edges2)\n", + "ax[0].set_title('Edges of labels after watershed');\n", + "ax[1].set_title('Edges of masks before watershed');" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Solution cell 3\n", + "almost = np.logical_not(np.logical_xor(edges != 0, edges2 != 0)) * mask_blobs # XOR = exclusive OR\n", "\n", + "output = skimage.morphology.binary_opening(almost)\n", "split_labels_ex_1 = skimage.morphology.label(output)\n", "\n", - "plt.subplots(figsize=(10,10))\n", - "plt.imshow(split_labels_ex_1)" + "fig, ax = plt.subplots(2, 2, figsize=(10,10))\n", + "ax[0, 0].imshow(np.logical_xor(edges != 0, edges2 != 0))\n", + "ax[0, 1].imshow(almost)\n", + "ax[1, 0].imshow(output)\n", + "ax[1, 1].imshow(split_labels_ex_1);" ] }, { @@ -1554,7 +1588,7 @@ "chan_3 = skimage.filters.median(chan_3, skimage.morphology.disk(3))\n", "masks = chan_3 > skimage.filters.threshold_otsu(chan_3)\n", "\n", - "plt.subplots(figsize=(10,10))\n", + "plt.subplots(figsize=(8,8))\n", "plt.imshow(masks);" ] }, @@ -1576,24 +1610,15 @@ "\n", "distance = ndi.distance_transform_edt(masks)\n", "blurred_distance = skimage.filters.gaussian(distance, sigma=2)\n", - "fp = np.ones((3,) * masks.ndim)\n", + "fp = np.ones((3,3))\n", "\n", "coords = skimage.feature.peak_local_max(blurred_distance, footprint=fp, labels=masks)\n", - "mask = np.zeros(distance.shape, dtype=bool)\n", + "mask = np.zeros(blurred_distance.shape, dtype=bool)\n", "mask[tuple(coords.T)] = True\n", "markers = skimage.measure.label(mask)\n", - "labels = skimage.segmentation.watershed(-blurred_distance, markers, mask=masks)\n", - "\n", - "edges = skimage.filters.sobel(labels)\n", - "edges2 = skimage.filters.sobel(masks)\n", - "\n", - "almost = np.logical_not(np.logical_xor(edges != 0, edges2 != 0)) * masks\n", - "output = skimage.morphology.binary_opening(almost)\n", + "labels_ex_3 = skimage.segmentation.watershed(-blurred_distance, markers, mask=masks)\n", "\n", - "split_labels_ex_3 = skimage.morphology.label(output)\n", - "\n", - "plt.subplots(figsize=(10,10))\n", - "plt.imshow(split_labels_ex_3);" + "stackview.picker(labels_ex_3)" ] }, { @@ -1631,7 +1656,7 @@ "source": [ "# Solution\n", "\n", - "results = skimage.measure.regionprops_table(split_labels_ex_3, image_ex_3[:,:,3], properties=('label','mean_intensity'))" + "results = skimage.measure.regionprops_table(labels_ex_3, image_ex_3[:,:,3], properties=('label','mean_intensity'))" ] }, {