@@ -316,24 +316,22 @@ labeled_image, count = connected_components(filename="data/shapes-01.jpg", sigma
316
316
317
317
fig, ax = plt.subplots()
318
318
plt.imshow(labeled_image)
319
- plt.axis(" off" );
319
+ plt.axis(" off" )
320
320
```
321
321
322
322
:::::::::::::::: spoiler
323
323
324
324
## Color mappings
325
325
326
- Here you might get a warning
326
+ If you are using an old version of Matplotlib you might get a warning
327
327
` UserWarning: Low image data range; displaying image with stretched contrast. `
328
- or just see an all black image
329
- (Note: this behavior might change in future versions or
330
- not occur with a different image viewer).
328
+ or just see a visually blank image
331
329
332
330
What went wrong?
333
- When you hover over the black image,
331
+ When you hover over the blank image,
334
332
the pixel values are shown as numbers in the lower corner of the viewer.
335
333
You can see that some pixels have values different from ` 0 ` ,
336
- so they are not actually pure black .
334
+ so they are not actually all the same value .
337
335
Let's find out more by examining ` labeled_image ` .
338
336
Properties that might be interesting in this context are ` dtype ` ,
339
337
the minimum and maximum value.
@@ -345,29 +343,36 @@ print("min:", np.min(labeled_image))
345
343
print (" max:" , np.max(labeled_image))
346
344
```
347
345
348
- Examining the output can give us a clue why the image appears black .
346
+ Examining the output can give us a clue why the image appears blank .
349
347
350
348
``` output
351
349
dtype: int32
352
350
min: 0
353
351
max: 11
354
352
```
355
353
356
- The ` dtype ` of ` labeled_image ` is ` int64 ` .
357
- This means that values in this image range from ` -2 ** 63 ` to ` 2 ** 63 - 1 ` .
354
+ The ` dtype ` of ` labeled_image ` is ` int32 ` .
355
+ This means that values in this image range from ` -2 ** 31 ` to ` 2 ** 31 - 1 ` .
358
356
Those are really big numbers.
359
357
From this available space we only use the range from ` 0 ` to ` 11 ` .
360
358
When showing this image in the viewer,
361
- it squeezes the complete range into 256 gray values.
362
- Therefore, the range of our numbers does not produce any visible change.
359
+ it may squeeze the complete range into 256 gray values.
360
+ Therefore, the range of our numbers does not produce any visible variation. One way to rectify this
361
+ is to explicitly specify the data range we want the colormap to cover:
363
362
364
- Fortunately, the scikit-image library has tools to cope with this situation.
363
+ ``` python
364
+ fig, ax = plt.subplots()
365
+ plt.imshow(labeled_image, vmin = np.min(labeled_image), vmax = np.max(labeled_image))
366
+ ```
367
+
368
+ Note this is the default behaviour for newer versions of ` matplotlib.pyplot.imshow ` .
369
+ Alternatively we could convert the image to RGB and then display it.
365
370
366
371
367
372
:::::::::::::::::::::::::
368
373
369
374
We can use the function ` ski.color.label2rgb() `
370
- to convert the colours in the image
375
+ to convert the 32-bit grayscale labeled image to standard RGB colour
371
376
(recall that we already used the ` ski.color.rgb2gray() ` function
372
377
to convert to grayscale).
373
378
With ` ski.color.label2rgb() ` ,
@@ -380,7 +385,7 @@ colored_label_image = ski.color.label2rgb(labeled_image, bg_label=0)
380
385
381
386
fig, ax = plt.subplots()
382
387
plt.imshow(colored_label_image)
383
- plt.axis(" off" );
388
+ plt.axis(" off" )
384
389
```
385
390
386
391
![ ] ( fig/shapes-01-labeled.png ) {alt='Labeled objects'}
@@ -728,7 +733,7 @@ colored_label_image = ski.color.label2rgb(labeled_image, bg_label=0)
728
733
729
734
fig, ax = plt.subplots()
730
735
plt.imshow(colored_label_image)
731
- plt.axis(" off" );
736
+ plt.axis(" off" )
732
737
733
738
print (" Found" , count, " objects in the image." )
734
739
```
@@ -779,7 +784,7 @@ fig, ax = plt.subplots()
779
784
im = plt.imshow(colored_area_image)
780
785
cbar = fig.colorbar(im, ax = ax, shrink = 0.85 )
781
786
cbar.ax.set_title(" Area" )
782
- plt.axis(" off" );
787
+ plt.axis(" off" )
783
788
```
784
789
785
790
![ ] ( fig/shapes-01-objects-coloured-by-area.png ) {alt='Objects colored by area'}
0 commit comments