Skip to content

Commit

Permalink
Merge pull request #292 from chbrandt/chbrandt-patch-1
Browse files Browse the repository at this point in the history
Add visualizations to better explain image blurring in episode 6
  • Loading branch information
tobyhodges authored Aug 16, 2023
2 parents 0852382 + adc16d3 commit 1a34167
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 1 deletion.
144 changes: 143 additions & 1 deletion episodes/06-blurring.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,149 @@ fig, ax = plt.subplots()
plt.imshow(blurred)
```

![](fig/gaussian-blurred.png){alt='Original image'}
![](fig/gaussian-blurred.png){alt='Blurred image'}


## Visualising Blurring

Somebody said once "an image is worth a thousand words".
What is actually happening to the image pixels when we apply blurring may be
difficult to grasp, let's now visualise the effects of blurring from a different
perspective.

Let's use the petri-dish image from previous episodes:

![
Graysacle version of the Petri dish image
](fig/petri-dish.png){alt='Bacteria colony'}

What we want to see here is the pixel intensities from a lateral perspective,
we want to see the profile of intensities .
For instance, let's look for the intensities of the pixels along the horizontal
at `Y=150`:

```python
import matplotlib.pyplot as plt
import imageio.v3 as iio
import skimage.color

# read colonies color image and convert to grayscale:
#
image = iio.imread('data/colonies-01.tif')
image_gray = skimage.color.rgb2gray(image)

# define the pixels we want to view the intensities (profile)
#
xmin, xmax = (0, image_gray.shape[1])
ymin = ymax = 150

# view the image indicating the profile pixels position
#
fig,ax = plt.subplots()
ax.imshow(image_gray, cmap='gray')
ax.plot([xmin,xmax], [ymin,ymax], color='red')
```

![
Grayscale Petri dish image marking selected pixels for profiling
](fig/petri-selected-pixels-marker.png){
alt='Bacteria colony image with selected pixels marker'
}

The intensity of those pixels we can see with a simple line plot:

```python
# Just rename our "Y" variables for a better reading
#
Y = ymin = ymax

# Select the vector of pixels along "Y"
#
image_gray_pixels_slice = image_gray[Y, :]

# Guarantee the intensity values are in the [0:255] range (unsigned integers)
#
image_gray_pixels_slice = img_as_ubyte(image_gray_pixels_slice)

fig = plt.figure()
ax = fig.add_subplot()

ax.plot(image_gray_pixels_slice, color='red')
ax.set_ylim(255, 0)
ax.set_ylabel('L')
ax.set_xlabel('X')
```

![
Intensities profile line plot of pixels along Y=150 in original image
](fig/petri-original-intensities-plot.png){
alt='Pixel intensities profile in original image'
}

And now, how does the same set of pixels look in the corresponding *blurred* image:

```python
# First, let's create a blurred version of (grayscale) image
#
from skimage.filters import gaussian

image_blur = gaussian(image_gray, sigma=3)

# Like before, plot the pixels profile along "Y"
#
image_blur_pixels_slice = image_blur[Y,:]
image_blur_pixels_slice = img_as_ubyte(image_blur_pixels_slice)

fig = plt.figure()
ax = fig.add_subplot()

ax.plot(image_blur_pixels_slice, 'red')
ax.set_ylim(255, 0)
ax.set_ylabel('L')
ax.set_xlabel('X')
```

![
Intensities profile of pixels along Y=150 in *blurred* image
](fig/petri-blurred-intensities-plot.png){
alt='Pixel intensities profile in blurred image'
}

And that is why *blurring* is also called *smoothing*.
This is how low-pass filters affect neighbouring pixels.

Now that we saw the effects of blurring an image from
two different perspectives, front and lateral, let's take
yet another look using a 3D visualisation.

:::::::::::::::::::::::::::::::::::::::::: callout

### 3D Plots with matplotlib
The code to generate these 3D plots is outside the scope of this lesson
but can be viewed by following the links in the captions.

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


![
A 3D plot of pixel intensities across the whole Petri dish image before blurring.
[Explore how this plot was created with matplotlib](https://gist.github.com/chbrandt/63ba38142630a0586ba2a13eabedf94b).
Image credit: [Carlos H Brandt](https://github.com/chbrandt/).
](fig/3D_petri_before_blurring.png){
alt='3D surface plot showing pixel intensities across the whole example Petri dish image before blurring'
}

![
A 3D plot of pixel intensities after Gaussian blurring of the Petri dish image.
Note the 'smoothing' effect on the pixel intensities of the colonies in the image,
and the 'flattening' of the background noise at relatively low pixel intensities throughout the image.
[Explore how this plot was created with matplotlib](https://gist.github.com/chbrandt/63ba38142630a0586ba2a13eabedf94b).
Image credit: [Carlos H Brandt](https://github.com/chbrandt/).
](fig/3D_petri_after_blurring.png){
alt='3D surface plot illustrating the smoothing effect on pixel intensities across the whole example Petri dish image after blurring'
}



::::::::::::::::::::::::::::::::::::::: challenge

Expand Down
Binary file added episodes/fig/3D_petri_after_blurring.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added episodes/fig/3D_petri_before_blurring.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added episodes/fig/petri-blurred-intensities-plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added episodes/fig/petri-dish.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added episodes/fig/petri-original-intensities-plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added episodes/fig/petri-selected-pixels-marker.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 1a34167

Please sign in to comment.