📘
Covariance Matrix from Image Patches
HardVectors
Compute the covariance matrix of image patches for PCA-based texture analysis.
Given an image and patch size, extract all overlapping patches, flatten them to vectors, and compute the covariance matrix. This is a fundamental step in texture analysis and eigenface computation.
Algorithm:
- Extract all overlapping patches of size p×p from the image
- Flatten each patch into a vector of length p2
- Stack vectors as rows of data matrix X (shape: n_patches × p²)
- Center the data: Xc=X−Xˉ (subtract mean vector)
- Compute covariance: Σ=n−11XcTXc
The covariance matrix Σ captures correlations between pixel positions within patches.
Applications:
- PCA for dimensionality reduction
- Eigenface/Fisherface recognition
- Texture synthesis and analysis
Example:
Input:
image = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
patch_size = 2Output:
[[2.5, 2.5, 2.5, 2.5], [2.5, 2.5, 2.5, 2.5], [2.5, 2.5, 2.5, 2.5], [2.5, 2.5, 2.5, 2.5]]
Reasoning:
Step 1: Extract 2×2 patches From 3×3 image, we get 4 overlapping patches:
- Patch 1 (top-left): [[1,2],[4,5]] → flattened: [1,2,4,5]
- Patch 2 (top-right): [[2,3],[5,6]] → flattened: [2,3,5,6]
- Patch 3 (bottom-left): [[4,5],[7,8]] → flattened: [4,5,7,8]
- Patch 4 (bottom-right): [[5,6],[8,9]] → flattened: [5,6,8,9]
Step 2: Create data matrix X (4×4) X=1245235645785689
Step 3: Compute mean and center Mean vector: [3, 4, 6, 7] Xc=X−mean
Step 4: Compute covariance Σ=31XcTXc
All entries are 2.5 due to the regular structure of this simple example.
Constraints:
- image: 2D numpy array (grayscale)
- patch_size: integer p (patches are p×p)
- Return: covariance matrix of shape (p², p²)
- Round to 4 decimal places
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.