Skip to content

Commit c396eeb

Browse files
committed
Modernize stackoverflow examples
1 parent 9bc648e commit c396eeb

File tree

1 file changed

+43
-46
lines changed

1 file changed

+43
-46
lines changed

lectures/stackoverflow_challenges.ipynb

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
"source": [
2323
"import matplotlib.pyplot as plt\n",
2424
"import numpy as np\n",
25-
"from skimage import (filter as filters, io, color,\n",
26-
" exposure, segmentation, morphology, img_as_float)"
25+
"from skimage import (filters, io, color, exposure, feature,\n",
26+
" segmentation, morphology, img_as_float)"
2727
]
2828
},
2929
{
@@ -50,31 +50,32 @@
5050
{
5151
"cell_type": "code",
5252
"execution_count": null,
53-
"metadata": {
54-
"collapsed": false
55-
},
53+
"metadata": {},
5654
"outputs": [],
5755
"source": [
58-
"from scipy.signal import convolve2d\n",
56+
"from scipy import ndimage as ndi\n",
5957
"\n",
60-
"img = color.rgb2gray(io.imread('../images/snakes.png'))\n",
58+
"image = color.rgb2gray(io.imread('../images/snakes.png'))\n",
6159
"\n",
6260
"# Reduce all lines to one pixel thickness\n",
63-
"snakes = morphology.skeletonize(img < 1)\n",
61+
"snakes = morphology.skeletonize(image < 1).astype(np.uint8)\n",
6462
"\n",
6563
"# Find pixels with only one neighbor\n",
66-
"corners = convolve2d(snakes, [[1, 1, 1],\n",
67-
" [1, 0, 1],\n",
68-
" [1, 1, 1]], mode='same') == 1\n",
69-
"corners = corners & snakes\n",
64+
"neighbor_kernel = np.array([[1, 1, 1],\n",
65+
" [1, 0, 1],\n",
66+
" [1, 1, 1]])\n",
67+
"num_neighbors = ndi.convolve(snakes, neighbor_kernel,\n",
68+
" mode='constant')\n",
69+
"corners = (num_neighbors == 1) & snakes\n",
7070
"\n",
7171
"# Those are the start and end positions of the segments\n",
72-
"y, x = np.where(corners)\n",
72+
"rr, cc = np.nonzero(corners)\n",
73+
"\n",
74+
"fig, ax = plt.subplots()\n",
75+
"ax.imshow(img, cmap='gray')\n",
76+
"ax.scatter(cc, rr)\n",
77+
"ax.set_axis_off()\n",
7378
"\n",
74-
"plt.figure(figsize=(10, 5))\n",
75-
"plt.imshow(img, cmap=plt.cm.gray, interpolation='nearest')\n",
76-
"plt.scatter(x, y)\n",
77-
"plt.axis('off')\n",
7879
"plt.show()"
7980
]
8081
},
@@ -108,7 +109,7 @@
108109
"source": [
109110
"image = io.imread(\"../images/round_pill.jpg\")\n",
110111
"image_equalized = exposure.equalize_adapthist(image)\n",
111-
"edges = filters.canny(color.rgb2gray(image_equalized))\n",
112+
"edges = feature.canny(color.rgb2gray(image_equalized))\n",
112113
"\n",
113114
"f, (ax0, ax1, ax2) = plt.subplots(1, 3, figsize=(15, 8))\n",
114115
"ax0.imshow(image)\n",
@@ -125,6 +126,7 @@
125126
"outputs": [],
126127
"source": [
127128
"from skimage import measure\n",
129+
"from matplotlib.patches import Circle\n",
128130
"\n",
129131
"coords = np.column_stack(np.nonzero(edges))\n",
130132
"\n",
@@ -138,7 +140,7 @@
138140
"\n",
139141
"f, ax = plt.subplots()\n",
140142
"ax.imshow(image, cmap='gray');\n",
141-
"circle = plt.Circle((col, row), radius=radius, edgecolor='green', linewidth=2, fill=False)\n",
143+
"circle = Circle((col, row), radius=radius, edgecolor='C9', linewidth=2, fill=False)\n",
142144
"ax.add_artist(circle);"
143145
]
144146
},
@@ -172,16 +174,16 @@
172174
},
173175
"outputs": [],
174176
"source": [
175-
"from skimage import restoration, color, io, filter as filters, morphology\n",
177+
"from skimage import restoration, color, io, feature, morphology\n",
176178
"\n",
177179
"image = color.rgb2gray(io.imread('../images/fingers.png'))\n",
178180
"denoised = restoration.denoise_tv_bregman(image, 1)\n",
179-
"edges = filters.canny(denoised, low_threshold=0.01, high_threshold=0.21)\n",
181+
"edges = feature.canny(denoised, low_threshold=0.01, high_threshold=0.21)\n",
180182
"\n",
181-
"fig, axes = plt.subplots(1, 2, figsize=(15, 10))\n",
182-
"axes[0].imshow(denoised, cmap='gray')\n",
183-
"axes[1].imshow(edges, cmap='gray')\n",
184-
"for ax in axes:\n",
183+
"fig, (ax0, ax1) = plt.subplots(1, 2, figsize=(15, 10))\n",
184+
"ax0.imshow(denoised, cmap='gray')\n",
185+
"ax1.imshow(edges, cmap='gray')\n",
186+
"for ax in (ax0, ax1):\n",
185187
" ax.set_axis_off()"
186188
]
187189
},
@@ -203,7 +205,7 @@
203205
"*Hint:*\n",
204206
"\n",
205207
"1. Equalize\n",
206-
"2. Threshold (``filter.otsu`` or ``filters.otsu``, depending on version)\n",
208+
"2. Threshold (``filters.threshold_otsu``)\n",
207209
"3. Remove objects touching boundary (``segmentation.clear_border``)\n",
208210
"4. Apply morphological closing (``morphology.closing``)\n",
209211
"5. Remove small objects (``measure.regionprops``)\n",
@@ -219,7 +221,8 @@
219221
"outputs": [],
220222
"source": [
221223
"from skimage import data\n",
222-
"plt.imshow(data.coins(), cmap='gray');"
224+
"fig, ax = plt.subplots()\n",
225+
"ax.imshow(data.coins(), cmap='gray');"
223226
]
224227
},
225228
{
@@ -230,14 +233,13 @@
230233
},
231234
"outputs": [],
232235
"source": [
233-
"from scipy import ndimage\n",
234236
"from skimage import segmentation\n",
235237
"\n",
236238
"image = data.coins()\n",
237239
"equalized = exposure.equalize_adapthist(image)\n",
238-
"edges = equalized > filters.threshold_otsu(equalized)\n",
239-
"edges = segmentation.clear_border(edges)\n",
240-
"edges = morphology.closing(edges, morphology.square(3))\n",
240+
"binary0 = equalized > filters.threshold_otsu(equalized)\n",
241+
"binary1 = segmentation.clear_border(binary0)\n",
242+
"binary2 = morphology.closing(binary1, morphology.square(3))\n",
241243
"\n",
242244
"f, (ax0, ax1) = plt.subplots(1, 2)\n",
243245
"ax0.imshow(image, cmap='gray')\n",
@@ -247,21 +249,16 @@
247249
{
248250
"cell_type": "code",
249251
"execution_count": null,
250-
"metadata": {
251-
"collapsed": false
252-
},
252+
"metadata": {},
253253
"outputs": [],
254254
"source": [
255-
"labels = measure.label(edges)\n",
256-
"for region in measure.regionprops(labels):\n",
257-
" if region.area < 200:\n",
258-
" rows, cols = region.coords.T\n",
259-
" labels[rows, cols] = 0\n",
260-
"\n",
261-
"print(\"Number of coins:\", len(np.unique(labels)) - 1)\n",
262-
" \n",
263-
"out = color.label2rgb(labels, image, bg_label=0)\n",
264-
"plt.imshow(out);"
255+
"labels = ndi.label(binary2)[0]\n",
256+
"labels_big = morphology.remove_small_objects(labels)\n",
257+
"print(\"Number of coins:\", len(np.unique(labels_big)[1:]))\n",
258+
"\n",
259+
"out = color.label2rgb(labels_big, image, bg_label=0)\n",
260+
"fig, ax = plt.subplots()\n",
261+
"ax.imshow(out);"
265262
]
266263
},
267264
{
@@ -530,9 +527,9 @@
530527
"name": "python",
531528
"nbconvert_exporter": "python",
532529
"pygments_lexer": "ipython3",
533-
"version": "3.4.3"
530+
"version": "3.6.3"
534531
}
535532
},
536533
"nbformat": 4,
537-
"nbformat_minor": 0
534+
"nbformat_minor": 1
538535
}

0 commit comments

Comments
 (0)