|
22 | 22 | "source": [
|
23 | 23 | "import matplotlib.pyplot as plt\n",
|
24 | 24 | "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)" |
27 | 27 | ]
|
28 | 28 | },
|
29 | 29 | {
|
|
50 | 50 | {
|
51 | 51 | "cell_type": "code",
|
52 | 52 | "execution_count": null,
|
53 |
| - "metadata": { |
54 |
| - "collapsed": false |
55 |
| - }, |
| 53 | + "metadata": {}, |
56 | 54 | "outputs": [],
|
57 | 55 | "source": [
|
58 |
| - "from scipy.signal import convolve2d\n", |
| 56 | + "from scipy import ndimage as ndi\n", |
59 | 57 | "\n",
|
60 |
| - "img = color.rgb2gray(io.imread('../images/snakes.png'))\n", |
| 58 | + "image = color.rgb2gray(io.imread('../images/snakes.png'))\n", |
61 | 59 | "\n",
|
62 | 60 | "# 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", |
64 | 62 | "\n",
|
65 | 63 | "# 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", |
70 | 70 | "\n",
|
71 | 71 | "# 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", |
73 | 78 | "\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", |
78 | 79 | "plt.show()"
|
79 | 80 | ]
|
80 | 81 | },
|
|
108 | 109 | "source": [
|
109 | 110 | "image = io.imread(\"../images/round_pill.jpg\")\n",
|
110 | 111 | "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", |
112 | 113 | "\n",
|
113 | 114 | "f, (ax0, ax1, ax2) = plt.subplots(1, 3, figsize=(15, 8))\n",
|
114 | 115 | "ax0.imshow(image)\n",
|
|
125 | 126 | "outputs": [],
|
126 | 127 | "source": [
|
127 | 128 | "from skimage import measure\n",
|
| 129 | + "from matplotlib.patches import Circle\n", |
128 | 130 | "\n",
|
129 | 131 | "coords = np.column_stack(np.nonzero(edges))\n",
|
130 | 132 | "\n",
|
|
138 | 140 | "\n",
|
139 | 141 | "f, ax = plt.subplots()\n",
|
140 | 142 | "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", |
142 | 144 | "ax.add_artist(circle);"
|
143 | 145 | ]
|
144 | 146 | },
|
|
172 | 174 | },
|
173 | 175 | "outputs": [],
|
174 | 176 | "source": [
|
175 |
| - "from skimage import restoration, color, io, filter as filters, morphology\n", |
| 177 | + "from skimage import restoration, color, io, feature, morphology\n", |
176 | 178 | "\n",
|
177 | 179 | "image = color.rgb2gray(io.imread('../images/fingers.png'))\n",
|
178 | 180 | "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", |
180 | 182 | "\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", |
185 | 187 | " ax.set_axis_off()"
|
186 | 188 | ]
|
187 | 189 | },
|
|
203 | 205 | "*Hint:*\n",
|
204 | 206 | "\n",
|
205 | 207 | "1. Equalize\n",
|
206 |
| - "2. Threshold (``filter.otsu`` or ``filters.otsu``, depending on version)\n", |
| 208 | + "2. Threshold (``filters.threshold_otsu``)\n", |
207 | 209 | "3. Remove objects touching boundary (``segmentation.clear_border``)\n",
|
208 | 210 | "4. Apply morphological closing (``morphology.closing``)\n",
|
209 | 211 | "5. Remove small objects (``measure.regionprops``)\n",
|
|
219 | 221 | "outputs": [],
|
220 | 222 | "source": [
|
221 | 223 | "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');" |
223 | 226 | ]
|
224 | 227 | },
|
225 | 228 | {
|
|
230 | 233 | },
|
231 | 234 | "outputs": [],
|
232 | 235 | "source": [
|
233 |
| - "from scipy import ndimage\n", |
234 | 236 | "from skimage import segmentation\n",
|
235 | 237 | "\n",
|
236 | 238 | "image = data.coins()\n",
|
237 | 239 | "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", |
241 | 243 | "\n",
|
242 | 244 | "f, (ax0, ax1) = plt.subplots(1, 2)\n",
|
243 | 245 | "ax0.imshow(image, cmap='gray')\n",
|
|
247 | 249 | {
|
248 | 250 | "cell_type": "code",
|
249 | 251 | "execution_count": null,
|
250 |
| - "metadata": { |
251 |
| - "collapsed": false |
252 |
| - }, |
| 252 | + "metadata": {}, |
253 | 253 | "outputs": [],
|
254 | 254 | "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);" |
265 | 262 | ]
|
266 | 263 | },
|
267 | 264 | {
|
|
530 | 527 | "name": "python",
|
531 | 528 | "nbconvert_exporter": "python",
|
532 | 529 | "pygments_lexer": "ipython3",
|
533 |
| - "version": "3.4.3" |
| 530 | + "version": "3.6.3" |
534 | 531 | }
|
535 | 532 | },
|
536 | 533 | "nbformat": 4,
|
537 |
| - "nbformat_minor": 0 |
| 534 | + "nbformat_minor": 1 |
538 | 535 | }
|
0 commit comments