Up
Main page
We have seen how black-and-white images can be represented by
a single matrix with entries given by 8-bit unsigned integers.
(In other words, each entry is a value between 0 and 255,
inclusive. The value 0 represents black and the value
255 represents white. The values in between represent
various shades of grey.)
Let be an matrix representing a
black-and-white image.
We will now look at how we can perform some basic image
manipulations using matrix algebra on .
Flipping an image upside down
To flip an image upside down, all we need to do is to
reorder the rows of so that
the first row becomes
the last, the second row becomes the second last, and
so on. We can do this using a sequence of row swappings.
Consider the matrix
Note that is a - matrix with ones on the diagonal from
the top right to the bottom left and it can obtained from the
identity matrix by reordering the rows so that the first row becomes
the last, the second row becomes the second last, and so on.
Then the image flipped upside down is represented by the matrix .
Rotating to the left by 90 degrees
Let us look at a small example to see what happens
to the matrix when the image is rotated to the left by 90 degrees.
Suppose that .
What we want to end up with is the matrix
.
Note that is
.
To obtain , we can simply flip the matrix upside down.
Colour inversion
Colour inversion involves flipping the colour of each pixel.
In terms of the entries of the matrix, we want each value
to become .
In particular, a will become a and a
will become a .
This can be accomplished by
where is the matrix of all 's.
(Using the letter to denote a matrix of all ones
is a common convention.)
Vertical blurring
If we take a copy of an image and slide it up one pixel,
and another copy and slide it one pixel down,
the average of the superimposition of the slided
copies and the original image will create a vertically blurred
version of the image.
At the pixel level, we can replace a pixel by the average of
the pixel itself and its immediate neigbours above and below.
For example, if the matrix entry for a pixel, call it ,
has value and the pixel immediately above has value
and the one immediately below right has value ,
then the new value of is
We want to do this for all pixels, noting that if a pixel is
on the top or bottom edge, then it has only one neighbour.
Can the operation described be accomplished using matrix operations?
We look at a small example to illustrate the ideas.
Suppose that .
Suppose that we want to replace the first row with the average of the
first two rows. What we can do is to add the second row to the
first row and then multiply the first row by .
These are both elementary row operations.
We can therefore form the product
where
The result is .
To replace the third row with the average of rows 2, 3, and 4,
we can form the product with
We can accomplish the blurring in one stroke by forming the product
where
In general, to blur an image vertically represented by an
matrix , we form the
product
where the following matrix:
Note that the blurring that results is hardly noticeable.
To increase the amount of blurring, simply
form the product for values of larger than .
With , the blurring becomes quite noticeable.
The following table shows the results for various values of .
Interestingly, is invertible. In other words, vertical blurring
as described above can be undone!
However, we must be careful not to jump to such a conclusion too quickly.
The reason is that when we do the averaging, we might end up with numbers
that are not integers. The numbers will have to be rounded before they
can represent pixels as 8-bit integers. Once rounding is done, deblurring
will not necessarily give us back the original image.
To reduce the impact of rounding, we can first convert the 8-bit
integers to 24-bit or 32-bit floating point values before we perform
any processing. Once all the desired processing has been done, the
floating point value are converted back to 8-bit integers.
Such a conversion process is adopted by many image editing software packages.
Exercises
How would you flip an image left to right using only the operations
discussed above?
How would you blur an image horizontally?