MCS4304 · Topic 3Arithmetic, logic & geometric operations
3
Arithmetic, logic & geometric operationspixel arithmetic · masks · transforms · homogeneous · interpolation Practise · 16 Qs MCQ · 14

Exam card

  • Arithmetic works pixel by pixel on images of the same size: add min(255, A+B) · subtract max(0, A−B), or |A−B| for change · multiply A·B for masking · divide A/(B+ε) · average (1/N)ΣIi for noise.
  • Logical (AND, OR, NOT, XOR) works on binary images and masks: region selection and segmentation. Arithmetic risks overflow and clipping; logical loses the intensity range.
  • Geometric: p′ = T(p). Scale [Sx 0; 0 Sy] · rotate [cos −sin; sin cos] · shear [1 kx; ky 1] · reflect Fx, Fy · translate x+tx, y+ty.
  • Homogeneous coordinates (x, y, 1) turn translation into a matrix multiply, so a chain of transforms becomes one product (affine).
  • Forward mapping leaves holes. Inverse mapping asks where each output pixel came from, then interpolates: nearest neighbour (fast, jagged) or bilinear (4 neighbours, smooth).
  • Aliasing = sampling too coarsely gives jagged edges and moiré. Anti-aliasing reduces it.

Key diagram

frame 1−frame 2=|A − B| = motion

Key terms

pixel-by-pixelsaturation / clippingimage additionimage subtraction|A − B| change detectionbackground subtractionmaskingimage divisionimage averagingAND · OR · NOT · XORbinary maskp′ = T(p)scalingrotationshearingreflectiontranslationhomogeneous coordinatesaffine transformationforward / inverse mappingnearest-neighbour interpolationbilinear interpolationaliasing · moiréanti-aliasing

What this topic covers

  1. Arithmetic operations
  2. Logical operations
  3. Geometric operations
  4. Resampling and interpolation
  5. Aliasing and anti-aliasing

Deep summary

Everything here follows the Topic 3 slides; each part shows its slide numbers. Parts marked added are study aids we wrote, not lecture content.

1 · Arithmetic, pixel by pixel, with numbers Topic 3 · slides 3–13

operationAB (or mask M)Cwhat happenedAdd20090+10040=255130min(255, A+B): 300 → 255Subtract9040−6070=300max(0, A−B): −30 → 0|A − B|9040−6070=3030both changes keptMultiply90200×10=900mask: 1 keeps, 0 removesDivide10050÷200100=0.50.5A/(B+ε): light evened outAverage N=41049710198+=100(1/N)ΣI: 400 ÷ 4 = 100one pixel position (x, y) at a time; every row uses 8-bit values 0…255
swipe to see the whole diagram

Slide 3: arithmetic works pixel by pixel on two images A and B of equal size, so each row above is one or two pixel positions.

Add (slide 4) clips at 255: 200 + 100 = 300 → 255. Subtract (slide 5) clips at 0: 40 − 70 = −30 → 0, so that change is lost; |A − B| keeps it (30). Slide 7 subtracts a smooth bright-to-dark background from a microscope image to flatten the lighting.

Multiply by a 0/1 mask M (slides 8–9: a white circle keeps the region of interest). Divide A/(B + ε) (slides 10–11): the same surface lit 200 or 100 gives 100/200 = 50/100 = 0.5, so uneven light cancels; ε stops a divide by zero. Average N frames of a still scene (slides 12–13, 500 images): noise of +4, −3, +1, −2 cancels to the true 100.

2 · Logical operations as truth tables on pixels Topic 3 · slides 14–16

four pixels of two binary masksA1100B1010A AND B1000A OR B1110A XOR B0110NOT A0011A: squareB: circleAND keeps only the overlap(the slide 15 picture)

Slide 14: logical operations are for binary images and masks; 1 = foreground (true), 0 = background (false). Read each column of the grid as one pixel.

AND is 1 only where both are 1 · OR where either is 1 · XOR where they differ · NOT flips. Slide 15 does this with a square A and a circle B: AND leaves the small overlap, OR the union, XOR the union minus the overlap, NOT turns each mask inside out.

Slide 16 sums up the trade-off: arithmetic changes intensities by an amount but can overflow or clip; logical selects regions precisely but keeps only two states and needs a good threshold or mask.

3 · One point through the four 2 × 2 transforms Topic 3 · slides 18–22

xyp (2,1)scale (4,3)rotate (−1,2)shear (3,1)Fx (−2,1)Fy (2,−1)F45° (1,2)T × (2, 1)Scaling[2 0; 0 3](4, 3)Rotation 90°[0 −1; 1 0](−1, 2)Shear kx=1[1 1; 0 1](3, 1)Reflection Fx[−1 0; 0 1](−2, 1)Reflection Fy[1 0; 0 −1](2, −1)F, θ = 45°[0 1; 1 0](1, 2)cos 90° = 0, sin 90° = 1; for F: cos 2θ = 0, sin 2θ = 1
swipe to see the whole diagram

Slide 18: a geometric operation moves the coordinates: p′ = T p with p = (x, y). Take one point p = (2, 1) and apply each 2 × 2 matrix from slides 19–22.

Scale Sx=2, Sy=3: (2·2, 3·1) = (4, 3); Sx ≠ Sy, so the aspect ratio changes. Rotate θ = 90°: (cos·2 − sin·1, sin·2 + cos·1) = (−1, 2). Shear kx=1, ky=0: (2 + 1·1, 1) = (3, 1).

Reflect: Fx negates x → (−2, 1); Fy negates y → (2, −1). The general F with θ = 45° becomes [0 1; 1 0], which swaps x and y → (1, 2): a mirror in the line y = x.

4 · Translation and homogeneous coordinates Topic 3 · slides 23–24

① translate (2, 1) by tx = 3, ty = 4103014001×211=551→ (5, 5)2+3, 1+4② rotate 90°, then translate: one matrixT · R =0−13104001×211=261check by hand: R(2, 1) = (−1, 2), then + (3, 4) = (2, 6) ✓the other order, R · T: (2, 1) → (5, 5) → rotate → (−5, 5), a different point
swipe to see the whole diagram

Slide 23: translation x′ = x + tx, y′ = y + ty adds a constant, and a 2 × 2 matrix cannot do that. Slide 24’s fix: write the point with a third coordinate equal to one, (x, y, 1), and use a 3 × 3 matrix.

① The last column holds tx, ty, so (2, 1, 1) → (5, 5, 1). ② Because every transform is now a matrix multiply, a chain collapses into one matrix (slide 24: affine transformations). The 2 × 2 rotation sits in the top-left corner.

added The order of multiplication matters: rotate-then-translate (T · R) and translate-then-rotate (R · T) land on different points.

5 · Inverse mapping, one row at a time Topic 3 · slides 25–29

source I (slide 26)10203040x = 0x = 1inverse map one row: x = (x′ + 0.5)/2 − 0.5x′xroundI0−0.2501010.2501020.7512031.251*20* clamped to the last column (x ≤ 1)output row 0 (NN)10102020forward mapping row 0:1020(x, y) → (2x, 2y): gaps at x′ = 1, 3
swipe to see the whole diagram

Slides 25–27: after a transform the new coordinates rarely fall on whole pixels. Forward mapping pushes each source pixel to (2x, 2y), so in a 4 × 4 output only 4 pixels are hit and the rest are holes. Inverse mapping asks, for each output pixel, “where did this pixel come from?” (T−1, slide 27).

For scale 2 the inverse is “divide by 2”. added We measure from pixel centres, x = (x′ + 0.5)/2 − 0.5, a common convention the slides don’t specify. Then nearest neighbour (slide 29) rounds: I′(x, y) = I(round x, round y). Row 0 becomes 10 10 20 20, matching the 4 × 4 result in the Discussion.

Slide 28: because x = 0.25 or 0.75 is between pixels, the value must be estimated by interpolation; NN and bilinear are the two main ways.

6 · Bilinear interpolation: weights worked out Topic 3 · slides 30–31

one output pixel: a = 0.25, b = 0.2510203040×0.5625×0.1875×0.1875×0.062517.5abbilinear 4 × 4nearest neighbour 4 × 41012.517.5201517.522.5252527.532.5353032.537.54010102020101020203030404030304040same output pixel (1, 1): bilinear 17.5 · NN 10
swipe to see the whole diagram

Slide 30: bilinear interpolation estimates a pixel lying between four neighbours, weighting each by how close it is:

I(x, y) = (1−a)(1−b)·I(x₀,y₀) + a(1−b)·I(x₀+1,y₀) + (1−a)b·I(x₀,y₀+1) + ab·I(x₀+1,y₀+1).

Output pixel (1, 1) maps back to (0.25, 0.25), so a = b = 0.25. Weights: 0.75·0.75 = 0.5625, 0.25·0.75 = 0.1875 (twice), 0.25·0.25 = 0.0625 (they add to 1). Value = 5.625 + 3.75 + 5.625 + 2.5 = 17.5. Nearest neighbour just copies 10.

Bilinear makes new in-between values (12.5, 22.5 …) and a smooth ramp; NN keeps only 10/20/30/40 in blocks. Slide 31 compares the two on a photo (and also shows bicubic). Note: slide 30 prints all four terms as I(x₀, y₀); the four different neighbours are meant.

7 · Aliasing: when sampling invents a pattern Topic 3 · slides 29, 32–34

fine stripes (period 2 px), sampled every 3rd pixelsamples: 1, 0, 1, 0 → each sample stands for 3 pixelsresult: wide stripes (period 6 px) that are not in the scene = a false pattern (moiré)
swipe to see the whole diagram

Slide 32: aliasing happens when an image is sampled at too low a resolution for its fine detail. In images it shows as jagged edges on diagonals and curves, false patterns (moiré), and lost or distorted detail.

added The diagram is a 1-D example: stripes that repeat every 2 pixels, kept at only every 3rd pixel, come out as stripes 6 pixels wide. Slide 33 shows the same thing on a striped shirt: the left copy has coloured wavy bands that aren’t on the shirt.

Slide 29 links this to resampling: nearest neighbour “can cause noticeable aliasing”. Anti-aliasing (slide 34) is the process of reducing these artefacts; the slides define it without naming a method.

8 · Exam traps Topic 3 · slides 5, 12, 14, 22–24, 26–30 added

  • max(0, A−B) is not change detection: where B is brighter the change clips to 0. Use |A − B| (slide 5).
  • Averaging needs a still scene (slide 12): anything that moves between frames blurs.
  • Logical ops are for binary masks (slide 14), not grey-level intensities.
  • Fx negates x (mirror across the y-axis); Fy negates y (slide 22). Don’t swap them.
  • Translation is not a 2 × 2 matrix (slide 23): it needs (x, y, 1) and a 3 × 3 matrix (slide 24). When combining, the order of the matrices matters.
  • Forward mapping leaves holes; inverse mapping + interpolation fills every pixel (slides 26–28).
  • NN keeps the original values, bilinear creates new ones (slides 29–30).

9 · Quick check Topic 3 · slides 5, 23–24, 30 added

Q1. A pixel is 100 in frame A and 180 in frame B. What do max(0, A − B) and |A − B| give?
max(0, −80) = 0, so the change is hidden; |−80| = 80 shows it (slide 5).
Q2. Translate (1, 2) by tx = 4, ty = −1 using homogeneous coordinates.
[1 0 4; 0 1 −1; 0 0 1] × (1, 2, 1) = (5, 1, 1), so the point moves to (5, 1) (slides 23–24).
Q3. A point sits exactly in the middle of four pixels 0, 100, 100, 200. What does bilinear interpolation give?
a = b = 0.5, so every weight is 0.25: 0.25 × (0 + 100 + 100 + 200) = 100 (slide 30).

Exam questions · 16 Practise these 16

Q1Define image addition and subtraction for 8-bit images and give two applications of each.
Addition: C(x,y) = min(255, A(x,y) + B(x,y)). Uses: combining several exposures, adding a constant to brighten, averaging frames to reduce noise. Subtraction: C(x,y) = max(0, A(x,y) − B(x,y)); for change detection use |A − B|. Uses: motion detection between video frames, defect detection in industrial inspection, background subtraction. The min/max keep the result inside the valid 0–255 range.
Q2How does image averaging reduce noise, and when does it work?
Given N images of the same scene, Ī(x,y) = (1/N) Σ Ii(x,y). The true signal is the same in every frame, while random noise varies and averages towards zero, so the noise falls as N grows (its standard deviation drops by about 1/√N). It works only for still scenes, where the content doesn’t move between frames. Examples: astronomical and medical imaging.
Q3Compare arithmetic and logical operations on images.
Arithmetic changes intensities quantitatively: brightness, contrast, blending, differencing. Its drawback is overflow, underflow and information loss from clipping and rounding, so it often needs normalisation. Logical (AND, OR, NOT, XOR) works on binary images and masks to select, extract and combine regions precisely. Its drawback is that it keeps only binary states, not the intensity range, and it depends on a good threshold or mask. Arithmetic is best for enhancement and differencing; logical for masking, segmentation and selecting a region of interest.
Q4Why can’t translation be written as a 2 × 2 matrix? How do homogeneous coordinates help?
Translation adds a constant (x′ = x + tx, y′ = y + ty). A 2 × 2 matrix can only form linear combinations of x and y, and it always maps the origin to itself, so it can’t add an offset. Homogeneous coordinates write the point as (x, y, 1), and translation becomes the 3 × 3 matrix [1 0 tx; 0 1 ty; 0 0 1]. All transforms then use the same multiply, so a sequence of them can be combined into one matrix (an affine transformation).
Q5Scale I = [10 20; 30 40] by a factor of 2. Explain forward vs inverse mapping and the result with nearest-neighbour interpolation.
Forward mapping sends each source pixel to (2x, 2y). Only 4 of the 16 output pixels get a value and the other 12 are holes. Inverse mapping takes each output pixel (x′, y′) and asks where it came from: (x′/2, y′/2). That is usually not a whole pixel position, so we interpolate. With nearest neighbour each source pixel fills a 2 × 2 block: [10 10 20 20; 10 10 20 20; 30 30 40 40; 30 30 40 40]. Bilinear interpolation would blend instead (e.g. 15 between 10 and 20), giving a smoother but slightly blurred result.
Q6Compare nearest-neighbour and bilinear interpolation, and explain aliasing.
Nearest neighbour, I′(x,y) = I(round x, round y), is cheap and keeps the original values, but it gives jagged edges and aliasing. Bilinear takes a weighted average of the 4 surrounding pixels using the fractional offsets a and b. It is smoother but slightly blurred, creates new values and costs more. Aliasing is the unwanted effect of sampling too coarsely for the detail: jagged diagonals and curves, moiré patterns, lost detail. Anti-aliasing reduces it, e.g. by smoothing before resampling.
Q7Calculate · For 8-bit images A = [250 120; 30 80] and B = [20 140; 60 80], compute A + B, max(0, A − B) and |A − B|. Which pixels were clipped?
A + B25525590160max(0, A − B)230000|A − B|23020300shaded = clipped (255 top, 0 bottom)
Topic 3 slides 4–5 A + B = [min(255, 270) min(255, 260); 90 160] = [255 255; 90 160], both top pixels clipped at 255. max(0, A − B) = [230 max(0, −20); max(0, −30) 0] = [230 0; 0 0], two pixels clipped at 0. |A − B| = [230 20; 30 0]: no clipping, and the two changes lost by subtraction reappear. Only the bottom-right pixel is truly unchanged.
Q8Match · Match each task to the arithmetic operation the slides give for it: (a) spotting a moving car between two video frames (b) keeping only a circular region of interest (c) correcting non-uniform illumination (d) reducing random noise in still astronomical images (e) making the whole image brighter.
moving car in 2 frames|A − B|keep a circle regionA × mask Muneven lightingA ÷ (B + ε)noisy telescope stillsaverage of Nbrighten a whole imageA + constant
Topic 3 slides 4–12 (a) subtraction, |A − B|: motion detection between frames (slides 5–6). (b) multiplication by a 0/1 mask (slides 8–9). (c) division A/(B + ε) (slide 10); slide 7 also flattens a background gradient by subtraction. (d) averaging N frames of a stationary scene (slide 12). (e) addition of a constant offset (slide 4).
Q9Apply · Two binary masks along one row are A = 1 1 0 1 0 and B = 0 1 1 1 0. Work out A AND B, A OR B, A XOR B and NOT B. Which result is a simple “where do they differ” map?
A11010B01110AND01010OR11110XOR10100NOT B10001
Topic 3 slides 14–15 AND = 0 1 0 1 0 (both 1). OR = 1 1 1 1 0 (either 1). XOR = 1 0 1 0 0 (exactly one is 1). NOT B = 1 0 0 0 1. XOR is the “differ” map: it is 1 only where the masks disagree, like the XOR panel on slide 15 (square and circle minus their overlap).
Q10Calculate · Rotate the point p = (4, 0) by θ = 30° about the origin using the rotation matrix. Give the new coordinates to two decimals.
(4, 0)(3.46, 2.00)30°
Topic 3 slide 20 R = [cos θ −sin θ; sin θ cos θ] with cos 30° = 0.866 and sin 30° = 0.5. x′ = 0.866·4 − 0.5·0 = 3.46; y′ = 0.5·4 + 0.866·0 = 2.00. So p′ = (3.46, 2.00). Check: the distance from the origin stays 4 (3.46² + 2² ≈ 16), as a rotation should keep it. The answer is not a whole pixel position, which is why resampling (slide 25) is needed.
Q11Calculate · A sample point lies at a = 0.4, b = 0.3 inside the four pixels I(x₀,y₀) = 50, I(x₀+1,y₀) = 100, I(x₀,y₀+1) = 150, I(x₀+1,y₀+1) = 200. Find its value by bilinear and by nearest-neighbour interpolation.
50×0.42100×0.28150×0.18200×0.12100a = 0.4 across · b = 0.3 down
Topic 3 slides 29–30 Weights: (1−a)(1−b) = 0.6·0.7 = 0.42; a(1−b) = 0.4·0.7 = 0.28; (1−a)b = 0.6·0.3 = 0.18; ab = 0.4·0.3 = 0.12 (sum 1). Bilinear = 0.42·50 + 0.28·100 + 0.18·150 + 0.12·200 = 21 + 28 + 27 + 24 = 100. Nearest neighbour: round(0.4) = 0 and round(0.3) = 0, so it copies I(x₀,y₀) = 50. Bilinear gives a value not found among the four pixels; NN keeps an original one.
Q12Classify · Name the transform each matrix performs: (a) [1 0.5; 0 1] (b) [−1 0; 0 1] (c) [0.5 0; 0 0.5] (d) [0 −1; 1 0] (e) [3 0; 0 1] (f) [1 0 5; 0 1 −2; 0 0 1].
[1 0.5; 0 1]→shear (kx = 0.5)[−1 0; 0 1]→reflection Fx[0.5 0; 0 0.5]→uniform scale ×0.5[0 −1; 1 0]→rotation 90°[3 0; 0 1]→non-uniform scale[1 0 5; 0 1 −2; 0 0 1]→translation (5, −2)
Topic 3 slides 19–24 (a) shear along x, kx = 0.5 (slide 21). (b) reflection Fx, negates x (slide 22). (c) uniform scaling, Sx = Sy = 0.5, halves the size and keeps the aspect ratio (slide 19). (d) rotation by 90°: cos 90° = 0, sin 90° = 1 (slide 20). (e) non-uniform scaling, Sx = 3 ≠ Sy = 1, stretches only x (slide 19). (f) translation by (5, −2) in homogeneous form; the only 3 × 3 one (slides 23–24).
Q13Describe · Describe what shearing does, using the square with corners (0,0), (2,0), (2,2), (0,2) and kx = 0.5, ky = 0. Give two uses of shearing.
squarekx = 0.5 → parallelogram(2,2)(3,2)(1,2)
Topic 3 slide 21 Shearing slants the image along an axis: x′ = x + kx·y, y′ = ky·x + y. Corners: (0,0) → (0,0); (2,0) → (2,0); (2,2) → (2 + 1, 2) = (3,2); (0,2) → (1,2). The bottom edge stays put and the top slides right by 0.5 × its height, so the square becomes a parallelogram. Uses (slide 21): geometric correction, modelling affine distortions, generating augmented training images.
Q14Compare · A 400 × 300 image is scaled with (Sx, Sy) = (0.5, 0.5) and, separately, with (2, 1). Give each new size and compare the two kinds of scaling.
400 × 300original 4 : 3200 × 150Sx = Sy = 0.5800 × 300Sx = 2, Sy = 1still 4 : 3now 8 : 3 (stretched)
Topic 3 slide 19 (0.5, 0.5): 400·0.5 × 300·0.5 = 200 × 150. Since Sx = Sy the scaling is uniform and the aspect ratio stays 4 : 3. (2, 1): 800 × 300. Sx ≠ Sy, so it is non-uniform: the ratio becomes 8 : 3 and shapes look stretched (a circle becomes an ellipse). Slide 19: “when Sx = Sy scaling is uniform and preserves the aspect ratio, and vice versa.”
Q15Outline · Outline the steps to rotate an image by θ using inverse mapping and an interpolation method from the slides.
output p′p = T⁻¹(p′)inside?interpolatewrite I′inverse mapping, per output pixelrotation: T⁻¹ = rotate by −θ · outside → leave 0
Topic 3 slides 20, 25–30 ① Create the empty output image. ② For every output pixel p′ = (x′, y′), ask where it came from: p = T−1(p′) (slide 27). added For a rotation the inverse is a rotation by −θ. ③ If p falls outside the source, leave the pixel empty (e.g. 0). ④ p is usually between pixels (slide 28), so estimate the value: nearest neighbour I(round x, round y) (slide 29) or bilinear from the 4 neighbours (slide 30). ⑤ Write that value to I′(x′, y′). Every output pixel gets a value, so there are no holes.
Q16Explain · Slide 7 shows a microscope image minus a smooth bright-to-dark image giving a cleaner result. Explain why subtraction helps here, using a cell edge of 180 on the bright side (background 160) and 100 on the dark side (background 80).
A: microscopeB: backgroundA − Bcells + bright-to-darklighting onlycells on a flat levelleft: 180 − 160 = 20 · right: 100 − 80 = 20
Topic 3 slides 5–7 The second image holds only the uneven lighting (a gradient), so A − B removes that slow trend and leaves the detail. Bright side: 180 − 160 = 20; dark side: 100 − 80 = 20. Before, the same cell edge read 180 or 100 depending on where it was; after, both read 20, so the cells look alike across the image. Values where B > A clip to 0 (max(0, A − B), slide 5). added In practice the result is often rescaled to use the full 0–255 range.

Quick MCQs · 14 MCQs in Practise

Not the exam format: quick checks that you understood the topic. Choose 1 or all that apply, then Check.