DIP+CV · Programming Exercise

EP03_02 — 🔀 Alpha Blending of Two Images

3.12.2 EP03_02 🔀 Alpha Blending of Two Images

In nuclear medicine, images from different modalities (computed tomography and magnetic resonance imaging) are fused to aid in diagnosis. Weighted blending (alpha blending) is the fundamental operation of this process, allowing the radiologist to interactively control the weight of each modality in the displayed image.

See Figure 3.27 for a simulation of this EP.

3.12.2.1 📋 Implementation Guidelines

  1. Dimensions: Read the integers \(L\) (rows) and \(C\) (columns).
  2. Parameter: Read the real value \(\alpha \in [0, 1]\).
  3. Data: Read the integer values of matrix \(f_1\) (image 1) followed by those of matrix \(f_2\) (image 2).
  4. Mapping: For each position \((i, j)\), compute:

\[g(i,j) = \text{clip}\left(\text{round}\left(\alpha \cdot f_1(i,j) + (1-\alpha) \cdot f_2(i,j)\right)\right)\]

  1. Output: Display the resulting \(L \times C\) matrix.

3.12.2.2 📌 Computational Constraints

  • Rounding: Apply round before converting to integer.
  • Saturation: Constrain to the interval \([0, 255]\) with \(\text{clip}(x) = \max(0, \min(255, x))\).
  • Float operation: Perform the operation in floating point before rounding.

3.12.2.3 🧠 Theoretical Background

Value of \(\alpha\) Result
\(\alpha = 1.0\) Only \(f_1\)
\(\alpha = 0.5\) Arithmetic mean of \(f_1\) and \(f_2\)
\(\alpha = 0.0\) Only \(f_2\)

3.12.2.4 📦 Input and Output Specification (VPL)

Input:

  • Line 1: Integer \(L\).
  • Line 2: Integer \(C\).
  • Line 3: Real \(\alpha\).
  • Following lines: Elements of \(f_1\) (\(L\) lines with \(C\) values each).
  • Following lines: Elements of \(f_2\) (\(L\) lines with \(C\) values each).

Output:

  • Resulting \(L \times C\) matrix.

3.12.2.5 📌 Examples

Input Output Observation
1
3
0.5
0 100 200
100 200 50
50 150 125 Mean between the two images
1
3
1.0
10 20 30
90 80 70
10 20 30 Only \(f_1\) (alpha=1)
🔀 Simulator EP03_02: Alpha Blending of Two Images g = α·f1 + (1−α)·f2

Adjust the transparency parameter α to observe the pixel-by-pixel weighted linear combination between images f1 and f2.

0.50
α = 0.00 → Only f2  |  α = 0.50 → Equal Weighted Average  |  α = 1.00 → Only f1
Image f1
Image f2
Result g
Formula: clip(round(0.50 · f1 + 0.50 · f2))
Figure 3.27: Simulator EP03_02: Alpha Blending of Two Images (g = α·f1 + (1−α)·f2)
%%writefile EP03_02.cpp
// your solution
Overwriting EP03_02.cpp
TestSuite("EP03_02.cpp").run()
✔️ EP03_02.cases already exists in casos/
📋 6 case(s) loaded from casos/EP03_02.cases

🔍 Testing C++: EP03_02.cpp
⚠️ EP03_02.cpp: Empty file (fewer than 3 lines). Tests skipped.