Image rotation from scratch
The following code shows how to rotate an image using the skimage (scikit-image) library.
Exercise: Write an algorithm from scratch that will do the same (i.e., take an input image as an ndarray, and rotate it).
If you feel creative, you can also write code to magnify (zoom) the image.
A problematic approach
And while we can attempt to fix the problem...
...this is not an optimal approach
For later discussion: interpolation
Bi-linear interpolation
Also see bilinear interpolation on Wikipedia
Some warping experiments!
Fish-eye
Run the following scripts for fun:
(Open up the terminal in the "scripts" directory first)
deswirl.py (run using:
python deswirl.py
)In the UK, a criminal tried to hide his identity by posting swirled pictures of his face online. Here, we use the Mona Lisa to illustrate what he did. Can you restore her face back to normal? (Note that you can adjust the position of the red dot, as well as move the sliders.)
clock_deblur.py
I took a picture of a wall clock while moving the camera. Or perhaps the clock moved. Either way, now I cannot read the time! I've implemented a deblurring algorithm--can you adjust its parameters to help me pin-point the time?
Here's code for a swirl transform:
Can you come up with an even better distortion?
Start with this template:
Composing Transformations
scikit-image allows you to compose several transformations. For example:
The transform
module allows us to rotate images. The inner workings is something like this:
Note that this rotates the cat around the origin (top-left).
Can you modify my_rotate
to rotate the image around the center?
Hint:
Shift the image (see above) so that the center of the image lies at (0, 0)
Rotate the image
Shift the image back---the opposite of what you did in step 1
All of this can be achieved by composing transformations and calling warp
once.
Advanced challenge: rectifying an image
We know the above tiles are laid out in a square--can you transform the image so that the tiles are displayed as if you were viewing them from above?
The centre-points of the corner circles are, given as (row, column) coordinates:
Hint: there is a linear transformation matrix, , such that
where is the homogeneous coordinate in the original image and is the homogeneous coordinate in the rectified image (with homogeneous we simply mean that we add an extra 1 at the end, e.g. (72, 129) becomes (72, 129, 1). The values for and their new values, , are therefore:
(You can choose any output size you like--I chose )
Why do we need homogeneous coordinates? It allows us to have translation as part of H:
Note that each element of the output coordinate is of the form . Without the 1 in the last position of the coordinate, there would have been no and therefore no translation!
The question on how to determine is left for another day. If you are curious, the answer can be found here.
In the meantime, I provide some code to calculate :
Using the code in the cell above, you can compute the target coordinate of any position in the original image.