Guide to AI and Data Courses · Lecture Notes
Chapter 17: Image Processing
When we scan an old document into a computer, the page has turned grey, specks are scattered over it and the writing has faded. Our eyes still read it, but to the computer the page is just a table of numbers. Image processing teaches how to turn that table into another table: erasing the specks, opening up the contrast and separating the writing from the background. These notes introduce the course and show its three core ideas, filtering and convolution, histogram equalization and thresholding, with small numbers you can compute by hand.
Learning outcomes
- An image is a matrix
- Read a grey-level image as a matrix of pixels; compute its memory requirement from its size and bit depth.
- Convolution and filters
- Compute the convolution of one pixel with a 3×3 kernel by hand; explain what the averaging, sharpening and Sobel kernels do.
- Histogram equalization
- Build the equalization table of a small image from its histogram and cumulative distribution.
- Thresholding
- Compute the between-class variance for each threshold in Otsu's method and pick the best one.
- Place of the course
- State what separates image processing from computer vision, signal processing, computer graphics and medical image processing.
17.1What question does the course ask?
How is an image enhanced, transformed and divided into regions?
Image processing studies methods that take an image and turn it into a new image or into regions: reducing noise, boosting contrast, finding edges and object boundaries. Both input and output are images; understanding what is in the image is the job of computer vision.
The question of image processing is: how do we turn an image into another image that is more useful? The input is a matrix of numbers; each number is the brightness of one pixel. In an 8-bit grey-level image the values run from 0 (black) to 255 (white); in a color image every pixel has three numbers (the red, green and blue channels). The output is a matrix too: an image with less noise, more contrast, emphasized edges, or converted to black and white.
For this reason most methods in the course are arithmetic on a table, and they need no learning. The method is defined in advance, and a single image is enough. We can sort the methods into two groups: point operations change each pixel by looking only at its own value (raising brightness, histogram equalization, thresholding); neighborhood operations also look at the pixels around it (filters, edge detection, morphology).
17.1.1Counting memory
Once the size of an image is known, so is its memory requirement: width × height × number of channels × bytes per pixel. A 512×512 grey-level image with 8 bits per pixel takes 262,144 bytes (256 KiB); in color (3 channels) it would take 786,432 bytes (768 KiB). Compression (JPEG, PNG) exists to reduce this number and is one of the last topics of the course.
17.1.2At a glance
| Criterion | Image Processing |
|---|---|
| Approach | pixel-level transformations |
| Input | image |
| Output | processed image or regions |
| Success measure | visual quality, PSNR |
| Math load | medium: convolution, Fourier |
| Data needs | little; learning not required |
17.2A short history
| Year | Milestone |
|---|---|
| 1920s | The Bartlane system sends digitally coded newspaper photos across the Atlantic by cable. |
| 1957 | Russell Kirsch makes the first digitally scanned photo: a 176×176-pixel image of his infant son. |
| 1964 | JPL corrects Ranger 7's lunar images by computer; digital image processing is born. |
| 1979 | Otsu publishes his method for picking a threshold from the histogram; still the most widely used thresholding method. |
| 1986 | The Canny edge detector appears: noise-robust, thin and continuous edges. |
| 1992 | The JPEG standard is adopted; DCT-based lossy compression becomes the common language of photos. |
17.3Core concepts
The rest of the course is built on the concepts below. The following sections work through the most important ones with examples.
| Concept | Meaning |
|---|---|
| Pixels and channels | An image is a matrix of numbers; color has three channels. |
| Histogram | Brightness distribution; contrast via histogram equalization. |
| Spatial filter | Convolution with neighboring pixels: blurring, sharpening. |
| Frequency domain | 2D Fourier transform; filtering periodic noise. |
| Edge detection | Brightness jumps via gradient, Sobel and Canny. |
| Thresholding | Separating foreground from background; Otsu's method. |
| Morphology | Erosion and dilation: cleaning up binary shapes. |
| Segmentation | Splitting an image into regions: region growing, watershed. |
17.4Spatial filters and convolution
The most common neighborhood operation is convolution. A small table of weights, the kernel, is slid over the image. At each position the pixels under the kernel are multiplied by the kernel's weights, the products are added, and the sum becomes the new value at that position. The weights of the kernel decide what result we get.
g(x, y) = Σi=−1..1 Σj=−1..1 h(i, j) · f(x−i, y−j), where f is the input, h the kernel and g the output. In true convolution the kernel is first rotated by 180°; the same sum without the rotation is called correlation. For symmetric kernels such as the averaging kernel the two give the same result.
Two example kernels come first in the course. The averaging kernel has all nine weights equal to 1/9; it blurs the image and reduces noise. The Sobel kernels measure the horizontal and vertical change of brightness and find edges.
In a 5×5 image the left three columns are dark (10) and the right two columns are bright (90); only the second pixel of the third row is 20 (noise). The 3×3 patch at the center of the image is:
| 10 | 10 | 90 |
| 20 | 10 | 90 |
| 10 | 10 | 90 |
- Average. The sum is 10+10+90+20+10+90+10+10+90 = 340; 340/9 ≈ 37.8. The center pixel went from 10 to 38: the edge was softened, blurred.
- Sobel Gx. The kernel is [−1 0 1; −2 0 2; −1 0 1]. Left column: −1·10 − 2·20 − 1·10 = −60; right column: 1·90 + 2·90 + 1·90 = 360. Gx = −60 + 360 = 300.
- Sobel Gy. The kernel is [−1 −2 −1; 0 0 0; 1 2 1]. Top row: −10 − 20 − 90 = −120; bottom row: 10 + 20 + 90 = 120. Gy = 0.
- Edge strength. √(300² + 0²) = 300. The large value says there is a jump in brightness in the horizontal direction; since Gy = 0 the edge is vertical.
- True convolution. If the kernel is rotated by 180° we get Gx = −300; only the sign changes, the strength stays the same.
The same patch gave smoothing with one kernel and an edge with the other. The only difference between the filters is the kernel.
17.4.1Sharpening
The sharpening kernel gives the center pixel a large positive weight and its four neighbors negative weights ([0 −1 0; −1 5 −1; 0 −1 0]): the more a pixel differs from its neighbors, the more that difference is enlarged. Noise is also a "difference", so sharpening enlarges it too. That is why noise is reduced first and sharpening comes after; sharpening does not bring back detail that was never recorded.
Forgetting the 1/9 factor in the averaging kernel. On the patch above the sum is 340; since an 8-bit image can store at most 255, the value overflows and the picture turns white. If the kernel weights add up to 1, brightness is preserved; derivative kernels such as Sobel add up to 0 and the result can be negative, so it is rescaled for display. At the border of the image the kernel hangs outside, so a rule must be chosen, such as replicating the border or padding with zeros.
17.5Histogram equalization
The histogram of an image shows how many pixels there are at each grey level. If a photo is dim, the histogram piles up on the left: only a few of the 256 available levels are used and the picture looks washed out. Histogram equalization redistributes the levels so that the histogram spreads over the whole range. It is a point operation: each grey level is replaced by a new one through a table that depends only on its own value.
For L levels and N pixels, h(v) = round( (CDF(v) − CDFmin) / (N − CDFmin) · (L − 1) ). Here CDF(v) is the number of pixels with value v or less (the cumulative distribution) and CDFmin is the first nonzero CDF value.
In an image of 8×8 = 64 pixels the grey levels run from 0 to 7. The histogram for levels 0..7 is 16, 14, 8, 7, 7, 6, 5, 1 pixels (the values are illustrative). The image is dim: the mean level is 146/64 ≈ 2.28.
- Cumulative distribution. CDF = 16, 30, 38, 45, 52, 58, 63, 64. The smallest nonzero value is 16.
- Transform. h(v) = (CDF − 16)/48 · 7: 0; 2.04; 3.21; 4.23; 5.25; 6.13; 6.85; 7.
- Rounding. The new levels are 0→0, 1→2, 2→3, 3→4, 4→5, 5→6, 6→7, 7→7.
- New histogram. Level 0: 16, level 2: 14, level 3: 8, level 4: 7, level 5: 7, level 6: 6, level 7: 5 + 1 = 6; level 1 is empty. The mean level is 193/64 ≈ 3.02.
Most pixels now sit at the higher levels; an ideal even distribution would have 8 pixels at each level, and the result comes closer to that. The transform is always nondecreasing: a dark pixel never overtakes a brighter one.
Believing that equalization adds information. In the example above levels 6 and 7 were mapped to the same value (7); two levels merged and this cannot be undone. Level 1 is no longer used at all. Equalization opens up the existing differences, but it opens up the noise too; and since it changes brightness, the original image must be kept if measurements will be made on medical or forensic images.
17.6Thresholding and Otsu's method
The simplest way to separate an object from its background is thresholding: a threshold t is chosen; pixels less than or equal to t count as "background" and larger ones as "foreground". The result is a black-and-white mask. The real question is how to choose the threshold. Choosing by hand is easy but breaks when the lighting changes. Otsu's method finds the threshold automatically from the histogram.
The idea is this: the right threshold splits the pixels into two classes that are as far apart as possible. For each candidate t we compute the weights of the two classes ω0, ω1 (the fractions of pixels) and their means μ0, μ1, and choose the t with the largest between-class variance.
σb2(t) = ω0 · ω1 · (μ0 − μ1)2. The farther apart the two groups are, and the more balanced, the larger this value.
The histogram of a 20-pixel image for levels 0..7 is 1, 5, 3, 1, 0, 2, 6, 2 pixels (the values are illustrative). Two peaks show: dark levels around 1–2 and bright levels around 6. The computation for each t:
| t | ω0 | μ0 | μ1 | σb2 |
|---|---|---|---|---|
| 0 | 0.05 | 0.00 | 3.89 | 0.72 |
| 1 | 0.30 | 0.83 | 4.93 | 3.52 |
| 2 | 0.45 | 1.22 | 5.73 | 5.02 |
| 3 | 0.50 | 1.40 | 6.00 | 5.29 |
| 4 | 0.50 | 1.40 | 6.00 | 5.29 |
| 5 | 0.60 | 2.00 | 6.25 | 4.34 |
| 6 | 0.90 | 3.33 | 7.00 | 1.21 |
- Classes for t = 3. The dark class is levels 0–3: 1+5+3+1 = 10 pixels, weight 0.5; mean (0·1 + 1·5 + 2·3 + 3·1)/10 = 1.4. The bright class is levels 4–7: 10 pixels; mean (5·2 + 6·6 + 7·2)/10 = 6.0.
- Variance. σb2 = 0.5 · 0.5 · (1.4 − 6.0)2 = 0.25 · 21.16 = 5.29.
- Decision. The largest value occurs at t = 3 and t = 4. Since level 4 has no pixels, the two thresholds give the same split. With threshold 3, levels 0–3 are background and 4–7 are foreground.
The threshold has landed in the valley between the two peaks. Otsu's method does not work as well on images whose histogram has a single peak or in which one class is very small.
Applying a threshold found once to every image, or to every part of an image. In the example above, if a shadow darkens the image by two levels, the two bright pixels at level 5 drop to 3 and are thrown into the background by t = 3. When the lighting is uneven, the threshold is computed per region (locally), or the image is first passed through an illumination correction.
After thresholding there are often stray noise pixels and small gaps in the letters. Morphological operations (erosion and dilation) repair them, and labeling the connected components lets us count and measure the objects. The course thus ends by building a preprocessing pipeline: filter the noise, open the contrast, threshold, clean the shapes.
17.7How is the course taught?
17.7.1A typical workflow
An old scanned document is first cleaned of specks with a median filter, its contrast is boosted by histogram equalization, it is binarized with Otsu's threshold, and the letters are repaired with morphological operations. The output is a clean image; reading the letters is the job of computer vision.
17.7.2Topics over the semester
- Image formation, sampling and quantization
- Point operations and histogram equalization
- Spatial filtering: smoothing and sharpening
- The 2D Fourier transform and frequency-domain filtering
- Image restoration: noise models, the Wiener filter
- Color spaces and color image processing
- Geometric transforms and interpolation
- Edge detection: gradients, Sobel, Canny
- Thresholding and Otsu's method
- Morphological operations
- Segmentation: region growing, watershed, labeling
- Image compression: the DCT and JPEG
This is a typical semester; the order and coverage vary between universities.
17.7.3Skills you will gain
- building image pipelines with OpenCV
- segmentation with scikit-image
- designing filters and kernels
- interpreting histograms and spectra
- measuring objects in ImageJ
17.7.4Try it yourself
The Try It: Finding Edges in a Noisy Image slide in the presentation lets you solve a typical problem of this course yourself: A quality control camera returned a 32×32 grayscale image of a part; the sensor is noisy. Toggle the steps and slide the threshold: how many of the detected edges lie on a real shape boundary?
17.8Are you ready for this course?
17.8.1Prerequisites
| Course | Why it is needed |
|---|---|
| Digital Signal Processing | 2D signals, convolution, Fourier transform |
| Linear algebra and calculus | matrix operations, derivatives and gradients: the language of filters and edges |
| Programming | loops over 2D arrays in Python or C |
| Data Structures | queues and union-find: region growing and connected-component labeling |
17.8.2Ask yourself
For every "no", the right-hand column shows where to start. If you can say "yes" to most of them, you are ready.
| Question | If not, start with |
|---|---|
| Can you visit every element of a matrix with nested loops? | An intro programming course, then a short NumPy tutorial |
| Can you explain what a partial derivative and a gradient are? | Derivatives and partial derivatives from calculus |
| Can you multiply matrices and take a dot product? | Linear Algebra |
| Can you explain what the frequency components of a signal are? | Digital Signal Processing |
17.9Neighboring courses
17.9.1Where it overlaps
| Relation | Course | Shared ground |
|---|---|---|
| builds on | This course builds on Digital Signal Processing | 2D signals, convolution, Fourier transform |
| builds on | Computer Vision builds on this course | edges, filters, segmentation |
| application | Medical Image Processing applies the methods of this course | denoising, segmentation, registration |
| application | Remote Sensing applies the methods of this course | multispectral images, correction |
| shares methods | Shares methods with Pattern Recognition | feature extraction, template matching |
17.9.2Where it differs
- Computer Vision: In image processing both input and output are images; in computer vision the input is an image and the output is meaning: a label, a box, depth.
- Digital Signal Processing: Signal processing works with a 1D time signal; image processing carries the same convolution and Fourier tools to two dimensions.
- Computer Graphics: Graphics produces an image from a scene description; image processing takes an existing image and transforms it.
- Medical Image Processing: Medical image processing applies this course's methods to medical images such as CT and MRI, under clinical constraints.
17.10In practice
17.10.1Applications
| Area | What is done |
|---|---|
| Photo editing | Brightness, sharpness, noise reduction. |
| Document scanning | Deskewing, converting to black and white. |
| Compression | JPEG, PNG and video coding. |
| Industrial inspection | Isolating scratches and stains on products. |
| Microscopy | Segmenting and counting cells. |
| Preprocessing | Clean input for vision and medical models. |
17.10.2Risks and ethics
| Risk | Explanation |
|---|---|
| Image forgery | The same tools can remove or add objects in a photo, and an edited image can be passed off as evidence. |
| Corrupted evidence | Lossy JPEG and sharpening change detail in forensic or medical images; the original file must be kept. |
| Invented detail | Super-resolution and denoising can produce detail that was never in the scene, and it may be taken as real. |
| Hand-tuned thresholds | Thresholds and parameters picked by looking at one image fail silently when the lighting changes. |
These issues are discussed in depth in Explainable AI and AI Ethics.
17.10.3Tools and datasets
Tools: OpenCV, scikit-image, Pillow, ImageJ / Fiji, MATLAB Image Processing Toolbox, GIMP.
Classic test images and datasets: USC-SIPI test images, Kodak image set, BSDS500 (Berkeley segmentation), DIV2K (super-resolution), Cameraman and Peppers.
In image processing a single image is often enough: the method is defined in advance, not learned from data. Large labeled datasets are what computer vision needs.
17.11Common misconceptions
Image processing and computer vision are the same thing.
Image processing transforms the image; vision extracts meaning from it. One is a step before the other.
Sharpening brings back lost detail.
Sharpening accentuates existing edges; detail that was never recorded does not come back.
Deep learning does everything now.
Classical methods are still essential in calibration, compression, preprocessing and fast industrial inspection.
Enlarging an image increases its resolution.
Interpolation creates new pixels but no new information; the image just gets bigger.
17.12Summary
- An image is a matrix of numbers; image processing turns one matrix into another. The methods are defined in advance, not learned from data.
- Point operations change each pixel by its own value, neighborhood operations also look at the surrounding pixels; the most common neighborhood operation is convolution with a kernel.
- On the same patch the averaging kernel blurs and the Sobel kernel finds edges; the kernel weights decide the result.
- Histogram equalization spreads the grey levels over the whole range using the cumulative distribution; it raises contrast but adds no information.
- Otsu's method finds, from the histogram, the threshold that maximizes the between-class variance; uneven lighting calls for a local threshold.
17.13Test Yourself
Apply the sharpening kernel [0 −1 0; −1 5 −1; 0 −1 0] to the center of a 3×3 patch whose center is 40 and whose four sides (top, bottom, left, right) and corners are all 10. What is the result?
Answer
The corners have weight 0. Center: 5·40 = 200; four neighbors: −4·10 = −40. Total 160. The center went from 40 to 160: the difference was enlarged.
A 16-pixel image has four levels (0–3) and the histogram 4, 4, 0, 8. Work out the histogram equalization table.
Answer
CDF = 4, 8, 8, 16; minimum 4. h(v) = (CDF − 4)/12 · 3: 0; 1; 1; 3. So 0→0, 1→1, 2→1, 3→3. Level 2 has no pixels, so where it maps does not affect the new histogram.
An image has the histogram 3, 1, 1, 0, 0, 1, 1, 3 for levels 0..7. Compare the thresholds t = 1 and t = 2 with the Otsu criterion.
Answer
t = 1: ω0 = 0.4, μ0 = 1/4 = 0.25, ω1 = 0.6, μ1 = 34/6 ≈ 5.67; σb2 = 0.4·0.6·5.422 ≈ 7.04. t = 2: ω = 0.5 and 0.5, μ0 = 3/5 = 0.6, μ1 = 32/5 = 6.4; σb2 = 0.25·5.82 = 8.41. 2 is better (3 and 4 give the same split and the same value).
In a 3×3 patch eight pixels are 10 and the center pixel is 255 (salt noise). Compute the center output of the mean and the median filter; which suits this noise?
Answer
Mean: (8·10 + 255)/9 = 335/9 ≈ 37.2; the noise is not removed, it spreads as a 37 blotch. Median: the median of the nine sorted values (the 5th) is 10; the noise is removed completely. For isolated outlier pixels the median filter is the right choice.
How many bytes does an 8-bit grey-level image of 1024×768 pixels take? And a 24-bit color image of the same size?
Answer
Grey: 1024·768 = 786,432 bytes (768 KiB). Color: 3 bytes per pixel, 786,432·3 = 2,359,296 bytes (2.25 MiB).
A scanned invoice is first cleaned of specks, its contrast is raised, it is converted to black and white, and then its letters are read to extract the total. Which steps belong to image processing and which to computer vision?
Answer
Speck removal (median filter), contrast (histogram equalization) and conversion to black and white (Otsu) are image processing: input and output are both images. Reading the letters and extracting the total means getting meaning out of the image; that is computer vision.
17.14After this course
Image processing is the common first step for every course that works with images. To extract meaning from images, move on to computer vision; for applications, to medical image processing or remote sensing. In the opposite direction, computer graphics studies how to produce images.
Courses that build on or extend this one: Computer Vision, Medical Image Processing, Remote Sensing.
Related careers: Image processing engineer, Embedded vision engineer, Quality control systems engineer, Biomedical imaging specialist.
17.15Further reading
- R. C. Gonzalez and R. E. Woods, Digital Image Processing (4th ed., 2018): the classic textbook for the course; covers every topic from point operations, filters and thresholding to compression.
- W. Burger and M. J. Burge, Digital Image Processing: An Algorithmic Introduction (3rd ed., 2022): gives every method as an algorithm with code; good for moving hand calculations like those in these notes onto the computer.
- A. K. Jain, Fundamentals of Digital Image Processing (1989): a theoretical framework from the side of linear systems and transforms.
17.16References
- Gonzalez and Woods, Digital Image Processing, 4th ed., Pearson, 2018.
- Jain, Fundamentals of Digital Image Processing, Prentice Hall, 1989.
- Burger and Burge, Digital Image Processing: An Algorithmic Introduction, 3rd ed., Springer, 2022.
- Canny, "A Computational Approach to Edge Detection", IEEE Transactions on Pattern Analysis and Machine Intelligence 8(6), 1986.
Related pages: Slides · Slides (PDF) · Course page