Skip to content

Commit b9bc009

Browse files
authored
Merge pull request #310 from bear-rsg/keeping-low-intensity-pixels
Keeping low intensity pixels
2 parents 49112f9 + a396db9 commit b9bc009

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

episodes/03-skimage-images.md

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -379,39 +379,46 @@ The file `data/sudoku.png` is an RGB image of a sudoku puzzle:
379379

380380
![](data/sudoku.png){alt='Su-Do-Ku puzzle'}
381381

382-
Your task is to turn all of the bright pixels in the image to a
382+
Your task is to load the image in grayscale format and turn all of
383+
the bright pixels in the image to a
383384
light gray colour. In other words, mask the bright pixels that have
384385
a pixel value greater than, say, 192 and set their value to 192 (the
385386
value 192 is chosen here because it corresponds to 75% of the
386387
range 0-255 of an 8-bit pixel). The results should look like this:
387388

388389
![](fig/sudoku-gray.png){alt='Modified Su-Do-Ku puzzle'}
389390

390-
*Hint: this is an instance where it is helpful to load the image in grayscale format.*
391+
*Hint: the `cmap`, `vmin`, and `vmax` parameters of `matplotlib.pyplot.imshow`
392+
will be needed to display the modified image as desired. See the [Matplotlib
393+
documentation](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.imshow.html)
394+
for more details on `cmap`, `vmin`, and `vmax`.*
391395

392396
::::::::::::::: solution
393397

394398
## Solution
395399

396-
First, load the image file `data/sudoku.png` as a grayscale image. Remember that we use `image = np.array(image)` to create a copy of the image array because `imageio.imread` returns a non-writeable image.
400+
First, load the image file `data/sudoku.png` as a grayscale image.
401+
Note we may want to create a copy of the image array to avoid modifying our original variable and
402+
also because `imageio.v3.imread` sometimes returns a non-writeable image.
397403

398404
```python
399-
400-
sudoku = iio.imread(uri="data/sudoku.png")
405+
sudoku = iio.imread(uri="data/sudoku.png", mode="L")
406+
sudoku_gray_background = np.array(sudoku)
401407
```
402408

403409
Then change all bright pixel values greater than 192 to 192:
404410

405411
```python
406-
sudoku = sudoku.copy()
407-
sudoku[sudoku > 125] = 125
412+
sudoku_gray_background[sudoku_gray_background > 192] = 192
408413
```
409414

410-
Finally, display the modified image. Note that we have to specify `vmin=0` and `vmax=255` as the range of the colorscale because it would otherwise automatically adjust to the new range 0-192.
415+
Finally, display the original and modified images side by side. Note that we have to specify `vmin=0` and `vmax=255` as the range of the colorscale because it would otherwise automatically adjust to the new range 0-192.
411416

412417
```python
413418
fig, ax = plt.subplots()
414-
plt.imshow(sudoku, cmap="gray", vmin=0, vmax=1)
419+
fig, ax = plt.subplots(ncols=2)
420+
ax[0].imshow(sudoku, cmap="gray", vmin=0, vmax=255)
421+
ax[1].imshow(sudoku_gray_background, cmap="gray", vmin=0, vmax=255)
415422
```
416423

417424
:::::::::::::::::::::::::

0 commit comments

Comments
 (0)