PIXELBANKv8.2.1
Menu

Sparse Matrix Operations for Images

Implement sparse matrix operations for efficient image processing using scipy.sparse.

Many image operations produce sparse results (few non-zero values). Sparse representation saves memory and speeds up computation.

Sparse format (COO - Coordinate format):

  • Store only (row, col, value) for non-zero entries
  • Memory: O(nnz) instead of O(m×n)

Common sparse operations:

  • Conversion: dense ↔ sparse
  • Matrix-vector multiplication
  • Element access and modification

Applications in CV:

  • Graph-based segmentation (sparse affinity matrices)
  • Optical flow (sparse motion fields)
  • Feature matching (sparse correspondence matrices)

Example:

Input:
matrix = [[0, 0, 3],
          [0, 2, 0],
          [1, 0, 0]]
Output:
rows=[0,1,2], cols=[2,1,0], values=[3,2,1]
Reasoning:

Scanning for non-zero entries:

  • (0, 2) = 3 → row=0, col=2, val=3
  • (1, 1) = 2 → row=1, col=1, val=2
  • (2, 0) = 1 → row=2, col=0, val=1

Sparse representation: Only 3 values stored instead of 9. Compression ratio: 9/3 = 3×

Constraints:

  • Implement: to_sparse(), from_sparse(), sparse_multiply()
  • to_sparse: Returns (rows, cols, values) for non-zero entries
  • from_sparse: Reconstructs dense matrix from sparse format
  • sparse_multiply: Sparse matrix × dense vector
Editor

Test Results

0/0
Run code to see test results.