# A-level Physics (Advancing Physics)/Digital Processing

As we have already seen, a digital image consists of pixels, with each pixel having a value which represents its colour. For the purposes of understanding how digital images are manipulated, we are going to consider an 8-bit grayscale image, with pixel values ranging from 0 to 255, giving us 256 (2$^8$) levels of grey. 0 represents white, and 255 represents black. This is the image we are going to consider:
| 000 | 000 | 000 | 000 | 000 | 150 | 150 | 150 | 050 | 150 |
| 000 | 000 | 000 | 000 | 000 | 150 | 150 | 150 | 150 | 150 |
| 000 | 000 | 235 | 000 | 000 | 150 | 150 | 150 | 150 | 150 |
| 000 | 000 | 000 | 000 | 000 | 150 | 205 | 150 | 150 | 150 |
| 000 | 000 | 000 | 000 | 000 | 150 | 150 | 150 | 150 | 150 |
| 000 | 000 | 000 | 000 | 000 | 150 | 150 | 150 | 150 | 150 |
| 255 | 000 | 000 | 000 | 000 | 150 | 150 | 150 | 150 | 150 |
| 000 | 000 | 000 | 000 | 000 | 150 | 150 | 150 | 150 | 150 |
| 000 | 000 | 000 | 000 | 000 | 150 | 150 | 150 | 150 | 095 |
| 000 | 000 | 000 | 000 | 000 | 150 | 150 | 150 | 150 | 150 |
| 000 | 000 | 000 | 185 | 000 | 150 | 150 | 150 | 150 | 150 |
The image consists of an edge, and some random noise. There are two methods of smoothing this image (i.e. removing noise) that you need to know about:

## Mean Smoothing

In order to attempt to remove noise, we can take the mean average of all the pixels surrounding each pixel (and the pixel itself) as the value of the pixel in the smoothed image, as follows:
| 000 | 000 | 000 | 000 | 050 | 100 | 150 | 133 | 133 | 133 |
| 000 | 026 | 026 | 026 | 050 | 100 | 150 | 139 | 139 | 139 |
| 000 | 026 | 026 | 026 | 050 | 106 | 173 | 173 | 150 | 150 |
| 000 | 026 | 026 | 026 | 050 | 106 | 173 | 173 | 150 | 150 |
| 000 | 000 | 000 | 000 | 050 | 106 | 173 | 173 | 150 | 150 |
| 043 | 028 | 000 | 000 | 050 | 100 | 150 | 150 | 150 | 150 |
| 043 | 028 | 000 | 000 | 050 | 100 | 150 | 150 | 150 | 150 |
| 043 | 028 | 000 | 000 | 050 | 100 | 150 | 150 | 144 | 141 |
| 000 | 000 | 000 | 000 | 050 | 100 | 150 | 150 | 144 | 141 |
| 000 | 000 | 021 | 021 | 071 | 100 | 150 | 150 | 144 | 141 |
| 000 | 000 | 31 | 31 | 081 | 100 | 150 | 150 | 150 | 150 |
This does remove the noise, but it blurs the image with means crucial anomalies and points may be missed.

## Median Smoothing

A far better method is, instead of taking the mean, to take the median, as follows:
| 000 | 000 | 000 | 000 | 000 | 150 | 150 | 150 | 150 | 150 |
| 000 | 000 | 000 | 000 | 000 | 150 | 150 | 150 | 150 | 150 |
| 000 | 000 | 000 | 000 | 000 | 150 | 150 | 150 | 150 | 150 |
| 000 | 000 | 000 | 000 | 000 | 150 | 150 | 150 | 150 | 150 |
| 000 | 000 | 000 | 000 | 000 | 150 | 150 | 150 | 150 | 150 |
| 000 | 000 | 000 | 000 | 000 | 150 | 150 | 150 | 150 | 150 |
| 000 | 000 | 000 | 000 | 000 | 150 | 150 | 150 | 150 | 150 |
| 000 | 000 | 000 | 000 | 000 | 150 | 150 | 150 | 150 | 150 |
| 000 | 000 | 000 | 000 | 000 | 150 | 150 | 150 | 150 | 150 |
| 000 | 000 | 000 | 000 | 000 | 150 | 150 | 150 | 150 | 150 |
| 000 | 000 | 000 | 000 | 000 | 150 | 150 | 150 | 150 | 150 |
For this image, this gives a perfect result. In more complicated images, however, data will still be lost, although, in general, less data will be lost by taking the median than by taking the mean.

## Edge Detection

We can detect the positioning of edges in an image using the 'Laplace rule', or 'Laplace kernel'. For each pixel in the image, we multiply its value by 4, and then subtract the values of the pixels above and below it, and on either side of it. If the result is negative, we treat it as 0. So, taking the median-smoothed image above, edge detection gives the following result:
| 000 | 000 | 000 | 000 | 000 | 150 | 000 | 000 | 000 | 000 |
| 000 | 000 | 000 | 000 | 000 | 150 | 000 | 000 | 000 | 000 |
| 000 | 000 | 000 | 000 | 000 | 150 | 000 | 000 | 000 | 000 |
| 000 | 000 | 000 | 000 | 000 | 150 | 000 | 000 | 000 | 000 |
| 000 | 000 | 000 | 000 | 000 | 150 | 000 | 000 | 000 | 000 |
| 000 | 000 | 000 | 000 | 000 | 150 | 000 | 000 | 000 | 000 |
| 000 | 000 | 000 | 000 | 000 | 150 | 000 | 000 | 000 | 000 |
| 000 | 000 | 000 | 000 | 000 | 150 | 000 | 000 | 000 | 000 |
| 000 | 000 | 000 | 000 | 000 | 150 | 000 | 000 | 000 | 000 |
| 000 | 000 | 000 | 000 | 000 | 150 | 000 | 000 | 000 | 000 |

## Questions

1. How could the above methods be applied to a digital sound sample?
2. Which of the above methods would be suitable for smoothing sharp edges? Why?
3. Use median smoothing to remove noise from the following image of a white cat in a snowstorm (the black pixels have a value of 255):
| 000 | 255 | 000 | 000 |
| 000 | 000 | 000 | 255 |
| 255 | 000 | 000 | 000 |
| 000 | 000 | 255 | 000 |
4. Why would mean sampling not be appropriate for smoothing the image given in question 3?
5. Use mean smoothing to remove noise from the following image of a black cat in a coal cellar:
| 255 | 255 | 255 | 255 |
| 255 | 255 | 000 | 255 |
| 255 | 255 | 255 | 255 |
Worked Solutions