EN FR

Digital Image processing

Computer Representation of an Image

Digital images are essentially grids of tiny squares called pixels, which hold the color information for that specific point in the image. This grid structure is represented by a matrix.

Grayscale Images

In grayscale images, each pixel has a single value representing the intensity of light, usually ranging from 0 (black) to 255 (white).

Color Images

Color images are typically represented using the RGB color model. In this case, we would typically have three separate matrices, one for each color channel (Red, Green, and Blue). Each matrix cell would then hold the intensity value for that specific color at that pixel location.

The following code demonstrates how to open an image file using PIL and convert it to a NumPy array. For a color image, it's usually a 3D array with dimensions representing height, width, and color channels (RGB). For grayscale images, it's a 2D array with height and width.

from PIL import Image
import numpy
 
img= Image.open("myimage.png")
np_img = numpy.array(img)

Image Manipulations

Once represented as a matrix, we can apply linear algebra propreties and techniques to manipulate the image and extract usefull information and even transform the image.

Eigenvalues and Eigenvectors

Eigenvalues and eigenvectors are fundamental concepts in linear algebra with far-reaching applications across various fields, including digital image processing.

Here is a simple example showcasing how powerfull eigenvalues are:

Image compression

PCA (Principal component analysis) is a technique that can be used to compress images.

PCA analyzes this matrix to identify the directions (eigenvectors) that capture the most significant variations in the image data. These directions are called principal components. Eigenvalues help determine the importance of these directions, with larger eigenvalues indicating greater significance.

The eigenvectors and eigenvalues with the lowest values contain less information and are eliminated. The matrix is then reconstructed. This reduces the image’s storage without losing much information, demonstrating the critical role of eigenvalues and eigenvectors in preserving essential image details.

This example highlights how eigenvalues and eigenvectors contribute to efficient image compression techniques like PCA, balancing reduced storage with minimal loss of image fidelity.

I recommend this article for a detailed Image compression example using PCA.

SVD decomposition is a another popular technique that it's used for various applications, such as image compression and Object and Facial recognition.

You can perform Singular Value Decomposition on a matrix using scipy.linalg.svd in python. For example:

import numpy as np
from scipy.linalg import svd

data = np.array([
    [1, 2, 3, 4, 5],
    [5, 4, 3, 2, 1],
    [2, 3, 4, 5, 6],
    [6, 5, 4, 3, 2],
    [1, 3, 5, 7, 9]
])

U, S, VT = svd(data, full_matrices=False)
Read more about SVD transforamtion.

Edge detection

Given an image, we may want to extract the shape of an object in the image. This can be achieved through various techniques.

For grayscale images, an edge is a change in the intensity of light that occupies a small region. This change can be detected by applying a gradient operator to the image matrix. The gradient operator acts like a fluctuation detector because it calculates the derivative of the intensity values.

Before applying the gradient operator, we may apply some filters to reduce image noise and sharpen the edges. Common filters include Gaussian blur to smooth the image and remove noise, which helps in obtaining cleaner edge detection results.

I recommend this article for a detailed Edge detection example using OpenCV in Python.