Image Processing and Spatial linear transformations
22 Jun 2015We can think of an image as a function, f, from $$\pmb R^2 \rightarrow R$$ (or a 2D signal):
- f (x,y) gives the intensity at position (x,y)
Realistically, we expect the image only to be defined over a rectangle, with a finite range:
f: [a,b]x[c,d] -> [0,1]
A color image is just three functions pasted together. We can write this as a “vector-valued” function:
$$\pmb {f(x,y)} = \bigg[ \begin{array} {c} & r(x,y) \cr \ & g(x,y) \cr \ & b(x,y) \end{array} \bigg] $$
- Computing Transformations
If you have a transformation matrix you can evaluate the transformation that would be performed by multiplying the transformation matrix by the original array of points.
Examples of Transformations in 2D Graphics
In 2D graphics Linear transformations can be represented by 2x2 matrices. Most common transformations such as rotation, scaling, shearing, and reflection are linear transformations and can be represented in the 2x2 matrix. Other affine transformations can be represented in a 3x3 matrix.
Rotation
For rotation by an angle θ clockwise about the origin, the functional form is \( x' = xcosθ + ysinθ \)
and \( y' = − xsinθ + ycosθ \). Written in matrix form, this becomes:
$$
\begin{bmatrix} x' \cr \
y' \end{bmatrix} = \begin{bmatrix} \cos \theta & \sin\theta \cr \
-\sin \theta & \cos \theta \end{bmatrix} \begin{bmatrix} x \cr \
y \end{bmatrix}
$$
Scaling
For scaling we have \( x' = s_x \cdot x \) and \( y' = s_y \cdot y \). The matrix form is:
$$
\begin{bmatrix} x' \cr \
y' \end{bmatrix} = \begin{bmatrix} s_x & 0 \cr \
0 & s_y \end{bmatrix} \begin{bmatrix} x \cr \
y \end{bmatrix}
$$
Shearing
For shear mapping (visually similar to slanting), there are two possibilities.
For a shear parallel to the x axis has \( x' = x + ky \) and \( y' = y \) ; the shear matrix, applied to column vectors, is:
$$
\begin{bmatrix} x' \cr \
y' \end{bmatrix} = \begin{bmatrix} 1 & k \cr \
0 & 1 \end{bmatrix} \begin{bmatrix} x \cr \
y \end{bmatrix}
$$
A shear parallel to the y axis has \( x' = x \) and \( y' = y + kx \) , which has matrix form:
$$
\begin{bmatrix} x' \cr \
y' \end{bmatrix} = \begin{bmatrix} 1 & 0 \cr \
k & 1 \end{bmatrix} \begin{bmatrix} x \cr \
y \end{bmatrix}
$$
Image Processing
The package EBImage is an R package which provides general purpose functionality for the reading, writing, processing and analysis of images.
Image Properties
Images are stored as multi-dimensional arrays containing the pixel intensities. All EBImage functions are also able to work with matrices and arrays.
- Adjusting Brightness
- Adjusting Contrast
- Gamma Correction
- Cropping Image
Spatial Transformation
Spatial image transformations are done with the functions resize, rotate, translate and the functions flip and flop to reflect images.
Next we show the functions flip, flop, rotate and translate:
All spatial transforms except flip and flop are based on the general affine transformation.
Linear transformations using the function affine:
- Horizontal flip
$${m} = \left[ \begin{array}{cc} \ -1 & 0 \cr \ \ 0 & 1 \end{array} \right] $$
$$ \begin{equation} Result = image * m \end{equation} $$
- Horizontal shear $${m} = \left[ \begin{array}{cc} 1, 1/2 \cr \ 0, 1 \end{array} \right] $$
- Rotation by π/6 $${m} = \left[ \begin{array}{cc} cos(pi/6), -sin(pi/6) \cr \ sin(pi/6), cos(pi/6) \end{array} \right] $$
- Squeeze mapping with r=3/2 $${m} = \left[ \begin{array}{cc} 3/2, 0 \cr \ 0, 2/3 \end{array} \right] $$
- Scaling by a factor of 3/2 $${m} = \left[ \begin{array}{cc} 3/2, 0 \cr \ 0, 3/2 \end{array} \right] $$
- Scaling horizontally by a factor of 1/2 $${m} = \left[ \begin{array}{cc} 1/2, 0 \cr \ 0, 1 \end{array} \right] $$
References
- http://www.iitg.ernet.in/scifac/qip/publichtml/cdcell/chapters/digimageprocessin.pdf
- http://www.math.ksu.edu/research/i-center/UndergradScholars/manuscripts/FernandoRoman.pdf
- http://www.statpower.net/Content/310/R%20Stuff/SampleMarkdown.html
- http://linear.ups.edu/html/section-LT.html
- http://www.r-bloggers.com/r-image-analysis-using-ebimage/
- http://www.maa.org/external_archive/joma/Volume8/Kalman/Linear3.html
- https://en.wikipedia.org/wiki/Linear_map
- https://en.wikipedia.org/wiki/Matrix_%28mathematics%29
- https://en.wikipedia.org/wiki/Affine_transformation
- http://mathforum.org/mathimages/index.php/Transformation_Matrix
comments powered by Disqus