Skip to content

Commit b32977f

Browse files
committed
display range text
1 parent 49112f9 commit b32977f

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

episodes/08-connected-components.md

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -316,24 +316,22 @@ labeled_image, count = connected_components(filename="data/shapes-01.jpg", sigma
316316

317317
fig, ax = plt.subplots()
318318
plt.imshow(labeled_image)
319-
plt.axis("off");
319+
plt.axis("off")
320320
```
321321

322322
:::::::::::::::: spoiler
323323

324324
## Color mappings
325325

326-
Here you might get a warning
326+
If you are using an old version of Matplotlib you might get a warning
327327
`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
331329

332330
What went wrong?
333-
When you hover over the black image,
331+
When you hover over the blank image,
334332
the pixel values are shown as numbers in the lower corner of the viewer.
335333
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.
337335
Let's find out more by examining `labeled_image`.
338336
Properties that might be interesting in this context are `dtype`,
339337
the minimum and maximum value.
@@ -345,29 +343,36 @@ print("min:", np.min(labeled_image))
345343
print("max:", np.max(labeled_image))
346344
```
347345

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.
349347

350348
```output
351349
dtype: int32
352350
min: 0
353351
max: 11
354352
```
355353

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`.
358356
Those are really big numbers.
359357
From this available space we only use the range from `0` to `11`.
360358
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:
363362

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.
365370

366371

367372
:::::::::::::::::::::::::
368373

369374
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
371376
(recall that we already used the `ski.color.rgb2gray()` function
372377
to convert to grayscale).
373378
With `ski.color.label2rgb()`,
@@ -380,7 +385,7 @@ colored_label_image = ski.color.label2rgb(labeled_image, bg_label=0)
380385

381386
fig, ax = plt.subplots()
382387
plt.imshow(colored_label_image)
383-
plt.axis("off");
388+
plt.axis("off")
384389
```
385390

386391
![](fig/shapes-01-labeled.png){alt='Labeled objects'}
@@ -728,7 +733,7 @@ colored_label_image = ski.color.label2rgb(labeled_image, bg_label=0)
728733

729734
fig, ax = plt.subplots()
730735
plt.imshow(colored_label_image)
731-
plt.axis("off");
736+
plt.axis("off")
732737

733738
print("Found", count, "objects in the image.")
734739
```
@@ -779,7 +784,7 @@ fig, ax = plt.subplots()
779784
im = plt.imshow(colored_area_image)
780785
cbar = fig.colorbar(im, ax=ax, shrink=0.85)
781786
cbar.ax.set_title("Area")
782-
plt.axis("off");
787+
plt.axis("off")
783788
```
784789

785790
![](fig/shapes-01-objects-coloured-by-area.png){alt='Objects colored by area'}

0 commit comments

Comments
 (0)