User:Inconspicuum/Physics (A Level)/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 (28) 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
editIn 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.
Median Smoothing
editA 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
editWe 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 |