Skip to content

Commit

Permalink
differences for PR #316
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Dec 15, 2023
1 parent 3533c24 commit 02f21d0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
53 changes: 30 additions & 23 deletions 08-connected-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,24 +316,22 @@ labeled_image, count = connected_components(filename="data/shapes-01.jpg", sigma

fig, ax = plt.subplots()
plt.imshow(labeled_image)
plt.axis("off");
plt.axis("off")
```

:::::::::::::::: spoiler

## Color mappings
## Do you see an empty image?

Here you might get a warning
If you are using an old version of Matplotlib you might get a warning
`UserWarning: Low image data range; displaying image with stretched contrast.`
or just see an all black image
(Note: this behavior might change in future versions or
not occur with a different image viewer).
or just see a visually empty image.

What went wrong?
When you hover over the black image,
When you hover over the image,
the pixel values are shown as numbers in the lower corner of the viewer.
You can see that some pixels have values different from `0`,
so they are not actually pure black.
so they are not actually all the same value.
Let's find out more by examining `labeled_image`.
Properties that might be interesting in this context are `dtype`,
the minimum and maximum value.
Expand All @@ -345,29 +343,36 @@ print("min:", np.min(labeled_image))
print("max:", np.max(labeled_image))
```

Examining the output can give us a clue why the image appears black.
Examining the output can give us a clue why the image appears empty.

```output
dtype: int32
min: 0
max: 11
```

The `dtype` of `labeled_image` is `int64`.
This means that values in this image range from `-2 ** 63` to `2 ** 63 - 1`.
The `dtype` of `labeled_image` is `int32`.
This means that values in this image range from `-2 ** 31` to `2 ** 31 - 1`.
Those are really big numbers.
From this available space we only use the range from `0` to `11`.
When showing this image in the viewer,
it squeezes the complete range into 256 gray values.
Therefore, the range of our numbers does not produce any visible change.
it may squeeze the complete range into 256 gray values.
Therefore, the range of our numbers does not produce any visible variation. One way to rectify this
is to explicitly specify the data range we want the colormap to cover:

Fortunately, the scikit-image library has tools to cope with this situation.
```python
fig, ax = plt.subplots()
plt.imshow(labeled_image, vmin=np.min(labeled_image), vmax=np.max(labeled_image))
```

Note this is the default behaviour for newer versions of `matplotlib.pyplot.imshow`.
Alternatively we could convert the image to RGB and then display it.


:::::::::::::::::::::::::

We can use the function `ski.color.label2rgb()`
to convert the colours in the image
to convert the 32-bit grayscale labeled image to standard RGB colour
(recall that we already used the `ski.color.rgb2gray()` function
to convert to grayscale).
With `ski.color.label2rgb()`,
Expand All @@ -380,7 +385,7 @@ colored_label_image = ski.color.label2rgb(labeled_image, bg_label=0)

fig, ax = plt.subplots()
plt.imshow(colored_label_image)
plt.axis("off");
plt.axis("off")
```

![](fig/shapes-01-labeled.png){alt='Labeled objects'}
Expand All @@ -402,7 +407,7 @@ How does changing the `sigma` and `threshold` values influence the result?
## Solution

As you might have guessed, the return value `count` already
contains the number of found images. So it can simply be printed
contains the number of found objects in the image. So it can simply be printed
with

```python
Expand Down Expand Up @@ -685,7 +690,7 @@ set the entries that belong to small objects to `0`.
object_areas = np.array([objf["area"] for objf in object_features])
object_labels = np.array([objf["label"] for objf in object_features])
small_objects = object_labels[object_areas < min_area]
labeled_image[np.isin(labeled_image,small_objects)] = 0
labeled_image[np.isin(labeled_image, small_objects)] = 0
```

An even more elegant way to remove small objects from the image is
Expand All @@ -698,7 +703,7 @@ i.e, their pixel values are set to `False`.
We can then apply `ski.measure.label` to the masked image:

```python
object_mask = ski.morphology.remove_small_objects(binary_mask,min_area)
object_mask = ski.morphology.remove_small_objects(binary_mask, min_size=min_area)
labeled_image, n = ski.measure.label(object_mask,
connectivity=connectivity, return_num=True)
```
Expand All @@ -712,7 +717,7 @@ def enhanced_connected_components(filename, sigma=1.0, t=0.5, connectivity=2, mi
gray_image = ski.color.rgb2gray(image)
blurred_image = ski.filters.gaussian(gray_image, sigma=sigma)
binary_mask = blurred_image < t
object_mask = ski.morphology.remove_small_objects(binary_mask,min_area)
object_mask = ski.morphology.remove_small_objects(binary_mask, min_size=min_area)
labeled_image, count = ski.measure.label(object_mask,
connectivity=connectivity, return_num=True)
return labeled_image, count
Expand All @@ -728,7 +733,7 @@ colored_label_image = ski.color.label2rgb(labeled_image, bg_label=0)

fig, ax = plt.subplots()
plt.imshow(colored_label_image)
plt.axis("off");
plt.axis("off")

print("Found", count, "objects in the image.")
```
Expand Down Expand Up @@ -772,14 +777,16 @@ the area by indexing the `object_areas` with the label values in `labeled_image`

```python
object_areas = np.array([objf["area"] for objf in ski.measure.regionprops(labeled_image)])
object_areas = np.insert(0,1,object_areas)
# prepend zero to object_areas array for background pixels
object_areas = np.insert(0, obj=1, values=object_areas)
# create image where the pixels in each object are equal to that object's area
colored_area_image = object_areas[labeled_image]

fig, ax = plt.subplots()
im = plt.imshow(colored_area_image)
cbar = fig.colorbar(im, ax=ax, shrink=0.85)
cbar.ax.set_title("Area")
plt.axis("off");
plt.axis("off")
```

![](fig/shapes-01-objects-coloured-by-area.png){alt='Objects colored by area'}
Expand Down
2 changes: 1 addition & 1 deletion md5sum.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"episodes/05-creating-histograms.md" "59c07192c0a6217e8a42d3e7365025f9" "site/built/05-creating-histograms.md" "2023-12-08"
"episodes/06-blurring.md" "8d109bb4c49f27f54857f6d35b4c6b9a" "site/built/06-blurring.md" "2023-12-08"
"episodes/07-thresholding.md" "bc0b3a64255ef9dd359d4cf3188aba83" "site/built/07-thresholding.md" "2023-12-15"
"episodes/08-connected-components.md" "59d42797208c5bf569da2fa2e4dd05df" "site/built/08-connected-components.md" "2023-09-09"
"episodes/08-connected-components.md" "ffea5032d5ede5aa12673c83f9c62e97" "site/built/08-connected-components.md" "2023-12-15"
"episodes/09-challenges.md" "655bdca8cab5d28dc6b2c2e9275aaecc" "site/built/09-challenges.md" "2023-12-15"
"instructors/instructor-notes.md" "b1c166a544eb4b9b91f3ac8f2dafd549" "site/built/instructor-notes.md" "2023-05-12"
"learners/discuss.md" "ad762c335f99400dc2cd1a8aad36bdbd" "site/built/discuss.md" "2023-07-26"
Expand Down

0 comments on commit 02f21d0

Please sign in to comment.