Exhaustive Computer Vision and Image Processing Study Guide

Course Administration, Evaluation, and Environment Setup

  • Course Identifiers: CV1620 / DPS920 (Computer Vision).

  • Instructional Schedule & Mode:

    • Delivery Mode: Hybrid sessions (in-person, online, and recorded).
    • Weekly Contact: 3.5 hours3.5\,\text{hours} per week.
    • Tuesday Session: 1:30 PM to 3:15 PM (in-person).
    • Thursday Session: 3:20 PM to 5:05 PM (online).
    • Term Pattern: Typical Summer term delivery.
  • Grading Scheme:

    • Assignments: 15%15\%
    • Labs: 15%15\%
    • Quizzes: 15%15\%
    • Midterm Exam: 20%20\%
    • Final Exam: 22.5%22.5\%
    • Final Project: 12.5%12.5\%
  • Credit Criteria:

    • Achieve ≥50%\ge 50\% of the overall course mark.
    • Achieve ≥50%\ge 50\% average across all examinations.
    • Successfully complete the final project.
  • Reference Literature:

    • Kenneth Dawson-Howe, A Practical Introduction to Computer Vision with OpenCV, Wiley.
    • François Chollet, Deep Learning with Python, Second Edition, Manning.
    • Adrian Kaehler & Gary Bradski, Learning OpenCV 3: Computer Vision in C++ with the OpenCV Library, O'Reilly.
  • Development Environment Setup Procedure:

    1. Install Visual Studio Code (VSCode).
    2. Install Python, executable setup, and ensure Python is added to the system PATH variable.
    3. Add the Python extension in VSCode.
    4. Install virtual environment utility via pip: bash pip install virtualenv      
    5. Verify virtualenv installation: bash pip list # or virtualenv --version      
    6. Create isolated environment using target Python binary path: bash virtualenv -p <path_to_python_version> <env_name> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    7. Activate virtual environment: bash <path_to_project>/<env_name>/Scripts/activate &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    8. Install required scientific compute and vision packages: bash pip install numpy pip install opencv-python pip install matplotlib pip install ipykernel &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    9. Add Jupyter Notebook extension in VSCode.
    10. Register virtual environment kernel to Jupyter: bash python -m ipykernel install --user --name=<venv_name> --display-name "Python <venv_name>" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    11. Configure Git repository controls.

Fundamentals of Image Processing and Computer Vision

  • Core Definitions:

    • Image Processing: A subfield of signal processing that uses mathematical operations to transform and manipulate digital images. Its primary objective is to enhance image quality (e.g., noise reduction, contrast adjustment) or extract fundamental features (e.g., edge detection, object segmentation).
    • Computer Vision: An interdisciplinary field combining AI, machine learning, and image processing to interpret, analyze, and understand the contents of digital visual media (e.g., object detection, facial recognition, semantic segmentation).
  • Historical Evolution:

    • 1960s: Birth of digital image processing. Early developments emerged within electrical engineering and computer science, driven primarily by space exploration. NASA utilized digital image processing techniques to correct and enhance lunar images captured during the Apollo missions.
    • 1970s: Rapid advancements in medical imaging modalities (e.g., CT scans, X-rays) and satellite remote sensing.
    • 1980s: Rise of personal computing and expansion of desktop visual processing.
    • 1990s: Digital multimedia revolution, image compression standardization, and consumer digital cameras.
    • 2000s: Integration of computer vision tools into consumer software and web platforms.
    • 2010s–Present: Modern era defined by the convergence of classical image processing with artificial intelligence and deep learning models (such as Convolutional Neural Networks, post-2012).
  • OpenCV Background: Developed originally at Intel to accelerate computational benchmarks on multi-core CPUs. The library is written natively in C++ for maximum computational performance, with bindings provided for Python and Java. Key architect: Gary Bradski.

Human Vision vs. Digital Vision Systems

  • Anatomy of the Human Eye:
    • Light passes through the Cornea, Aqueous humour, Pupil (encircled by the Iris), and Crystalline lens (adjusted by Ciliary muscles).
    • Focuses through the Vitreous humour onto the Retina, which converts light into nerve impulses routed to the brain via the Optic nerve.

Human Eye Anatomy

  • System Comparison Metrics:

    • Receptor Elements: The human retina contains approximately 127×106127 \times 10^6 photoreceptor elements (rods and cones). A digital camera sensor requires a comparable number of discrete sensor pixels to match human visual resolution.
    • Channel Depth: Human vision encodes color via three cone types (sensitive to red, green, blue spectrums). Digital color sensors represent every spatial coordinate using three color values.
    • Temporal Frame Rate: Human vision processes continuous light into a perceived visual stream equivalent to 50 fps50\,\text{fps} to 60 fps60\,\text{fps}.
    • Binocularity: Dual physical receptors (two eyes / two stereo cameras) provide simultaneous stereoscopic data streams for depth perception.
  • Capture Mechanisms: Transitioned from chemical film cameras (relying on atomic/chemical reactions of silver halide emulsions inside light-tight film cartridges exposed in darkrooms) to solid-state digital sensors (CCD and CMOS microchips capturing light-generated electrical charges).

Digital Image Representation and Color Models

  • Signal Foundations:

    • Analog Signals: Continuous physical signals containing infinite possible values within a defined range.
    • Digital Signals: Discrete physical signals quantized into finite numeric representations (binary 00s and 11s).
    • Visible Light Spectrum: Humans perceive light at wavelengths between 400 nm400\,\text{nm} and 700 nm700\,\text{nm}. Camera digital sensors are designed specifically to operate within this range.
  • Pixel Structure and Coordinates:

    • Pixel (Picture Element): The smallest discrete, addressable component of a digital image storing color or intensity values.
    • Resolution: Total count of pixels in an image matrix, defined as width (ww) multiplied by height (hh).
    • Spatial Coordinate System: Structured as a 2D grid matrix where index (0,0)(0, 0) represents the top-left corner pixel. Horizontal indices represent columns (xx axis), and vertical indices represent rows (yy axis).
  • Color Models and Spaces:

    • Binary: 1 bit per pixel. Pixels are strictly restricted to binary values: 00 (black) or 11 / 255255 (white).
    • Grayscale: Single-channel intensity representation. Standard 8-bit unsigned integer (uint8) stores scalar values ranging from 00 (black) to 255255 (white).
    • RGB Color Model: Additive model combining Red, Green, and Blue channels. Standard 24-bit representation allocates 8 bits per channel (00 to 255255 per channel). Total possible color states: 256×256×256=224=16,777,216256 \times 256 \times 256 = 2^{24} = 16,777,216 unique colors.
    • BGR Color Model: Standard spatial channel order used by OpenCV internally, ordering channels as Blue (Index 0), Green (Index 1), and Red (Index 2). (Libraries like TensorFlow and Matplotlib utilize default RGB order).
    • CMYK Color Model: Subtractive color model combining Cyan, Magenta, Yellow, and Key (Black), used primarily in physical color printing processes.
    • HSV Color Model: Cylindrical color representation comprising Hue (color angle/type), Saturation (color purity/intensity), and Value (color brightness).

3D Array Representation

  • File Storage Formats:
    • JPEG (.jpg): Lossy compression algorithm. Reduces file size significantly at the cost of high-frequency visual fidelity. Does not support alpha channel transparency.
    • PNG (.png): Lossless compression algorithm. Preserves exact pixel data and supports full alpha channel transparency.
    • TIFF (.tif): High-fidelity format supporting uncompressed, lossless, or lossy modes with multiple layer storage. Used in commercial publishing and medical applications.

Core Image Operations in OpenCV and NumPy

  • Basic I/O Operations:

    • Reading Images:
    import cv2
    import numpy as np
    
    # Reads image into a BGR uint8 NumPy array
    image_array = cv2.imread("Lucy.jpg")
    print(f"Image shape: {image_array.shape}")  # Returns (height, width, channels)
    &nbsp;&nbsp;&nbsp;&nbsp;```
    - **Displaying Images**:
    

    python cv2.imshow("Window Title", image_array) cv2.waitKey(0) # Halts execution until a key is pressed cv2.destroyAllWindows()     ```

    • Writing/Saving Images: ```python

    Encodes array into target format and writes to disk

    cv2.imwrite("lucy_output.jpg", image_array)     ```

  • Array Slicing, ROI, and Memory Allocation:

    • Shallow Copy vs. Deep Copy Hazard:
    • Assigning img_assigned = img creates a pointer/reference to the same underlying memory buffer. Modifying img_assigned mutates the original img array.
    • To create a completely isolated clone, explicit deep copying is required:
      img_copied = img.copy()
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;```
    - **Region of Interest (ROI) & Cropping**:
    - Extracted by slicing NumPy array coordinates `img[y1:y2, x1:x2]`:
    

    python # Extract region between row 50:200 and column 100:300 roi = image[50:200, 100:300]       ```

  • Color Channel Operations:

    • Splitting and Merging:
    # Decompose into individual B, G, R 2D matrices
    b, g, r = cv2.split(image)
    
    # Recombine individual single-channel arrays into a 3D image matrix
    merged_image = cv2.merge([b, g, r])
    &nbsp;&nbsp;&nbsp;&nbsp;```
    - **Color Space Conversions**:
    

    python

    Convert BGR to Grayscale

    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    Convert BGR to RGB (e.g. for Matplotlib display)

    rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    Convert BGR to HSV

    hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)     ```

  • Intensity Min/Max Analytics:

    • Identifying intensity bounds is crucial for dynamic range adjustment, normalization, error detection (overexposure/underexposure), thresholding, and feature extraction.
    • Custom Red Channel Peak Extraction Code: ```python import cv2

    img = cv2.imread("sample.png") rows, cols, ncolor = img.shape red_channel_idx = 2 # BGR ordering max_red_val = 0

    for i in range(rows): for j in range(cols): val = img.item(i, j, red_channel_idx) if val > max_red_val: max_red_val = val

    print("Maximum red value in image is ", max_red_val)     ```

Point and Arithmetic Operations

  • Point Operators vs. Neighborhood Operators:

    • Point Operator: Function where the output pixel intensity O(x,y)O(x, y) depends exclusively on the input pixel intensity I(x,y)I(x, y) at the exact same spatial coordinate (and optional global constants):     O(x,y)=f(I(x,y))O(x, y) = f(I(x, y))
    • Examples: Contrast stretching, brightness shift, global thresholding, color inversion.
    • Neighborhood Operator: Function where output intensity O(x,y)O(x, y) depends on I(x,y)I(x, y) and the spatial neighborhood of surrounding input pixels:     O(x,y)=f({I(x+k,y+l)∣(k,l)∈N})O(x, y) = f(\{I(x + k, y + l) \mid (k, l) \in N\})
    • Examples: Image smoothing, blurring, edge filtering, spatial convolutions.
  • Arithmetic Operations & Saturated Arithmetic:

    • Standard NumPy Arithmetic Hazard: Standard arithmetic on 8-bit unsigned integer arrays (uint8) causes modulo overflow wrapping (250+10=4250 + 10 = 4; 5−10=2515 - 10 = 251).
    • Saturated Arithmetic: OpenCV enforces saturation limits to prevent wraparound artifacts:     Saturate(x)={255if x>2550if x<0xotherwise\text{Saturate}(x) = \begin{cases} 255 & \text{if } x > 255 \\ 0 & \text{if } x < 0 \\ x & \text{otherwise} \end{cases}
    • OpenCV Functions: cv2.add(img1, img2) and cv2.subtract(img1, img2).
  • Multiplication & Division:

    • Scaling image matrices by factors >1> 1 brightens highlights.
    • Scaling image matrices by factors <1< 1 darkens intensities.
  • Weighted Image Blending (Linear Blend):

    • Combines two images linearly according to assigned weights:     dst(x,y)=α⋅img1(x,y)+β⋅img2(x,y)+γ\text{dst}(x, y) = \alpha \cdot \text{img}_1(x, y) + \beta \cdot \text{img}_2(x, y) + \gamma
    • Implementation: ```python

    Images must possess identical spatial dimensions

    resized_img2 = cv2.resize(img2, (img1.shape[1], img1.shape[0])) blended_img = cv2.addWeighted(img1, 0.6, resized_img2, 0.4, 0.0)     ```

  • Thresholding: Converts grayscale images into binary format by comparing pixel intensities against a defined threshold value TT to separate foreground objects from background.

Spatial Padding and Boundary Handling

  • Purposes of Padding:

    • Spatial Dimension Preservation: Maintains image spatial dimensions during convolutional operations in Neural Networks.
    • Data Augmentation: Allows translation, rotation, and cropping transforms without discarding border content.
    • Feature Truncation Prevention: Prevents loss of edge features situated at image borders.
    • Image Alignment: Standardizes canvas sizes when stitching images of varying dimensions.
  • OpenCV Padding Syntax:

  padded_img = cv2.copyMakeBorder(src, top, bottom, left, right, borderType, value=[B, G, R])
&nbsp;&nbsp;```
  - Parameters `top`, `bottom`, `left`, `right`: Border thickness in pixels.

- **Border Types (`borderType`)**:
  - `cv2.BORDER_CONSTANT`: Adds a uniform colored margin specified by `value` (e.g., `value=[255, 0, 0]`).
  - `cv2.BORDER_REFLECT`: Mirror-reflects edge elements (`abcdefg` →\rightarrow `gfedcba`).
  - `cv2.BORDER_REFLECT_101` (or `cv2.BORDER_DEFAULT`): Mirror-reflects elements with slight edge-point adjustment.
  - `cv2.BORDER_REPLICATE`: Extends edge boundary pixels outward (`abcdefgh` →\rightarrow `aaaaa|abcdefgh|hhhhh`).
  - `cv2.BORDER_WRAP`: Wraps opposite edge pixels to create seamless tiling.

![Padding Modes](https://assets.knowt.com/pdf-flow-prod/45874123-3bf9-4670-b8d3-425126cd4885-figures/1.jpg)


# Drawing Operations and Video Processing

- **Drawing Shapes on Images**:
  - **Line**:
    ```python
    cv2.line(image, pt1=(50, 50), pt2=(350, 350), color=(255, 255, 255), thickness=3)
&nbsp;&nbsp;&nbsp;&nbsp;```
  - **Rectangle**:
    ```python
    # thickness=-1 fills the shape completely
    cv2.rectangle(image, pt1=(50, 50), pt2=(350, 300), color=(0, 255, 0), thickness=5)
&nbsp;&nbsp;&nbsp;&nbsp;```
  - **Circle**:
    ```python
    cv2.circle(image, center=(200, 200), radius=50, color=(0, 0, 255), thickness=-1)
&nbsp;&nbsp;&nbsp;&nbsp;```
  - Additional Primitive Functions: `cv2.ellipse()`, `cv2.polylines()`, `cv2.fillPoly()`, `cv2.putText()`, `cv2.arrowedLine()`, `cv2.drawMarker()`.

- **Video Stream Manipulation and FPS Processing**:
  - Video is processed as a sequential series of image frames.
  - **Webcam/File Capture Initialization**:
    ```python
    # Device index 0 captures default webcam; pass string path for video files
    cap = cv2.VideoCapture(0)
    ret, frame = cap.read()  # ret is boolean; frame is the image array
    cap.release()
&nbsp;&nbsp;&nbsp;&nbsp;```
  - **Custom Frame Rate (FPS) Throttle Pipeline**:
    ```python
    import cv2

    desired_fps = 10
    video_path = "sample_video.mp4"
    cap = cv2.VideoCapture(video_path)

    original_fps = int(cap.get(cv2.CAP_PROP_FPS))
    frame_interval = int(original_fps / desired_fps)
    frame_count = 0

    while True:
        ret, frame = cap.read()
        if not ret:
            break

        # Process and display frame only at target intervals
        if frame_count % frame_interval == 0:
            cv2.imshow("Frame", frame)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

        frame_count += 1

    cap.release()
    cv2.destroyAllWindows()
&nbsp;&nbsp;&nbsp;&nbsp;```


# Geometric Transformations and Linear Algebra Intuition

- **Mathematical Intuition of Transformations**:
  - Vector and matrix operations in spatial transformation alter coordinate vectors or modify the coordinate space itself.
  - Multiplying a coordinate vector by a transformation matrix mapped in vector space changes object position, scale, orientation, or spatial geometry.

- **Affine Transformation Definition**:
  - A linear spatial mapping that preserves straight lines and parallel line distance ratios. Object position, orientation, scale, and skew may change.

![Geometric Transformation Spectrum](https://assets.knowt.com/pdf-flow-prod/279c0977-5e88-4e03-a811-36b48bc71eb2-figures/1.jpg)

- **Matrix Formulations for Transformations**:
  - **Identity Transform**:
&nbsp;&nbsp;&nbsp;&nbsp;[uv]=[1001][xy]\begin{bmatrix} u \\ v \end{bmatrix} = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix}
  - **Scaling / Resizing**:
&nbsp;&nbsp;&nbsp;&nbsp;[uv]=[sx00sy][xy]\begin{bmatrix} u \\ v \end{bmatrix} = \begin{bmatrix} s_x & 0 \\ 0 & s_y \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix}
  - **Reflection (Vertical Flip across Horizontal Axis)**:
&nbsp;&nbsp;&nbsp;&nbsp;[uv]=[100−1][xy]\begin{bmatrix} u \\ v \end{bmatrix} = \begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix}
  - **Horizontal Shear**:
&nbsp;&nbsp;&nbsp;&nbsp;[uv]=[1shx01][xy]\begin{bmatrix} u \\ v \end{bmatrix} = \begin{bmatrix} 1 & sh_x \\ 0 & 1 \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix}
  - **Vertical Shear**:
&nbsp;&nbsp;&nbsp;&nbsp;[uv]=[10shy1][xy]\begin{bmatrix} u \\ v \end{bmatrix} = \begin{bmatrix} 1 & 0 \\ sh_y & 1 \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix}
  - **Translation / Shift (Using Homogeneous Coordinates)**:
&nbsp;&nbsp;&nbsp;&nbsp;[uv]=[10tx01ty][xy1]\begin{bmatrix} u \\ v \end{bmatrix} = \begin{bmatrix} 1 & 0 & t_x \\ 0 & 1 & t_y \end{bmatrix} \begin{bmatrix} x \\ y \\ 1 \end{bmatrix}
  - **Pure Rotation around Origin**:
&nbsp;&nbsp;&nbsp;&nbsp;[uv]=[cos⁡(θ)−sin⁡(θ)0sin⁡(θ)cos⁡(θ)0][xy1]\begin{bmatrix} u \\ v \end{bmatrix} = \begin{bmatrix} \cos(\theta) & -\sin(\theta) & 0 \\ \sin(\theta) & \cos(\theta) & 0 \end{bmatrix} \begin{bmatrix} x \\ y \\ 1 \end{bmatrix}

- **OpenCV 2D Rotation Matrix Formulation**:
  - Standard mathematical counterclockwise 2D rotation matrix around center (cx,cy)(c_x, c_y) with scale ss:
&nbsp;&nbsp;&nbsp;&nbsp;Rmath=[cos⁡(θ)⋅ssin⁡(θ)⋅s(1−cos⁡(θ)⋅s)⋅cx−sin⁡(θ)⋅s⋅cy−sin⁡(θ)⋅scos⁡(θ)⋅ssin⁡(θ)⋅s⋅cx+(1−cos⁡(θ)⋅s)⋅cy]R_{\text{math}} = \begin{bmatrix} \cos(\theta) \cdot s & \sin(\theta) \cdot s & (1 - \cos(\theta) \cdot s) \cdot c_x - \sin(\theta) \cdot s \cdot c_y \\ -\sin(\theta) \cdot s & \cos(\theta) \cdot s & \sin(\theta) \cdot s \cdot c_x + (1 - \cos(\theta) \cdot s) \cdot c_y \end{bmatrix}
  - OpenCV defaults to a **clockwise** convention, flipping the sign of sin⁡(θ)\sin(\theta). The official OpenCV affine rotation matrix is:
&nbsp;&nbsp;&nbsp;&nbsp;ROpenCV=[αβ(1−α)⋅cx−β⋅cy−βαβ⋅cx+(1−α)⋅cy]R_{\text{OpenCV}} = \begin{bmatrix} \alpha & \beta & (1 - \alpha) \cdot c_x - \beta \cdot c_y \\ -\beta & \alpha & \beta \cdot c_x + (1 - \alpha) \cdot c_y \end{bmatrix}
&nbsp;&nbsp;&nbsp;&nbsp;where α=s⋅cos⁡(θ)\alpha = s \cdot \cos(\theta) and β=s⋅sin⁡(θ)\beta = s \cdot \sin(\theta).

- **Rotation Code Implementation**:

python import cv2

image = cv2.imread("Lucy.jpg") (h, w) = image.shape[:2] center = (w // 2, h // 2) angle = 45 # degrees scale = 1.0

# Compute 2x3 affine rotation matrix rotation_matrix = cv2.getRotationMatrix2D(center, angle, scale)

# Apply affine transform rotated_image = cv2.warpAffine(image, rotation_matrix, (w, h))   ```

  • Shear Code Implementation:
  import cv2
  import numpy as np

  image = cv2.imread("image.jpg")
  (h, w) = image.shape[:2]

  shear_factor_x = 0.5
  shear_factor_y = 0.2

  shear_matrix = np.array([
      [1, shear_factor_x, 0],
      [shear_factor_y, 1, 0]
  ], dtype=np.float32)

  # Compute transformed bounding dimensions
  new_w = int(w + abs(shear_factor_x * h))
  new_h = int(h + abs(shear_factor_y * w))

  sheared_image = cv2.warpAffine(image, shear_matrix, (new_w, new_h))
&nbsp;&nbsp;```


# Resizing, Sampling, and Interpolation Algorithms

- **Resizing Mechanics**:
  - Adjusts image canvas spatial dimensions. Upsampling generates new pixel values; downsampling decimates pixel counts.
  - Function Signature: `cv2.resize(src, dsize, fx=0, fy=0, interpolation=cv2.INTER_LINEAR)`.

- **OpenCV Interpolation Algorithms**:
  - `cv2.INTER_NEAREST`: Nearest Neighbor interpolation.
  - `cv2.INTER_LINEAR`: Bilinear interpolation (default).
  - `cv2.INTER_CUBIC`: Bicubic interpolation over a 4×44 \times 4 pixel neighborhood.
  - `cv2.INTER_AREA`: Resampling using pixel area relations. Recommended for decimation (downsampling) as it produces Moiré-free output.
  - `cv2.INTER_LANCZOS4`: Lanczos interpolation over an 8×88 \times 8 pixel neighborhood.
  - `cv2.INTER_LINEAR_EXACT`: Bit-exact bilinear interpolation.
  - `cv2.INTER_NEAREST_EXACT`: Bit-exact nearest neighbor interpolation.

- **Nearest Neighbor Interpolation Mechanics**:
  - Selects the intensity value of the single spatially closest pixel from the input image and assigns it to the target pixel.
  - **Nearest Neighbor Variants**:
    - **Averaging Variant (N=2N=2) Worked Example**:
      - Input pixel row: [180,255,9,56,127,255,0,73,0][180, 255, 9, 56, 127, 255, 0, 73, 0]
      - Interconstruct adjacent pixel pairs:
        - Between 180180 and 255255: ⌊(180+255)/2⌋=217\lfloor (180 + 255) / 2 \rfloor = 217
        - Between 255255 and 99: ⌊(255+9)/2⌋=132\lfloor (255 + 9) / 2 \rfloor = 132
        - Between 99 and 99: 99
        - Vertical/diagonal midpoints computed similarly: between 180180 and 5656 (below it) →\rightarrow ⌊(180+56)/2⌋=118\lfloor (180 + 56) / 2 \rfloor = 118
        - Corner midpoint →\rightarrow ⌊(217+118)/2⌋=167\lfloor (217 + 118) / 2 \rfloor = 167

- **Custom Array Upsampling Algorithm without OpenCV**:

python # Pure NumPy upsampling algorithm (factor of 2) # Row duplication half_upscaled[0:height2:2, :, :] = original_img[:, :, :] half_upscaled[1:height2:2, :, :] = original_img[:, :, :]

# Column duplication upscaled_img[:, 0:width2:2, :] = half_upscaled[:, :, :] upscaled_img[:, 1:width2:2, :] = half_upscaled[:, :, :]   ```

  • Bilinear Interpolation Mathematical Formulation:

    • Computes target intensity InewI_{\text{new}} at position xx between boundary points x1x_1 and x2x_2 with known intensities I1I_1 and I2I_2 (where x1≤x≤x2x_1 \le x \le x_2):     Inew=(x2−xx2−x1)⋅I1+(x−x1x2−x1)⋅I2I_{\text{new}} = \left( \frac{x_2 - x}{x_2 - x_1} \right) \cdot I_1 + \left( \frac{x - x_1}{x_2 - x_1} \right) \cdot I_2
  • Bilinear Interpolation Worked Example:

    • Given x1=0x_1 = 0 with intensity I1=233I_1 = 233, and x2=1x_2 = 1 with intensity I2=200I_2 = 200.
    • Target spatial coordinate: x=0.3x = 0.3.
    • Spatial distances: x−x1=0.3x - x_1 = 0.3, x2−x1=1.0x_2 - x_1 = 1.0, x2−x=0.7x_2 - x = 0.7.
    • Weight calculation:     Inew=(0.71.0)×233+(0.31.0)×200I_{\text{new}} = \left( \frac{0.7}{1.0} \right) \times 233 + \left( \frac{0.3}{1.0} \right) \times 200Inew=163.1+60=223.1I_{\text{new}} = 163.1 + 60 = 223.1