官术网_书友最值得收藏!

Affine transformations

In this section, we will discuss about the various generalized geometrical transformations of 2D images. We have been using the function warpAffine quite a bit over the last couple of sections, it's about time we understood what's happening underneath.

Before talking about affine transformations, let's see what Euclidean transformations are. Euclidean transformations are a type of geometric transformations that preserve length and angle measure. As in, if we take a geometric shape and apply Euclidean transformation to it, the shape will remain unchanged. It might look rotated, shifted, and so on, but the basic structure will not change. So technically, lines will remain lines, planes will remain planes, squares will remain squares, and circles will remain circles.

Coming back to affine transformations, we can say that they are generalizations of Euclidean transformations. Under the realm of affine transformations, lines will remain lines but squares might become rectangles or parallelograms. Basically, affine transformations don't preserve lengths and angles.

In order to build a general affine transformation matrix, we need to define the control points. Once we have these control points, we need to decide where we want them to be mapped. In this particular situation, all we need are three points in the source image, and three points in the output image. Let's see how we can convert an image into a parallelogram-like image:

import cv2
import numpy as np

img = cv2.imread('images/input.jpg')
rows, cols = img.shape[:2]

src_points = np.float32([[0,0], [cols-1,0], [0,rows-1]])
dst_points = np.float32([[0,0], [int(0.6*(cols-1)),0], [int(0.4*(cols-1)),rows-1]])
affine_matrix = cv2.getAffineTransform(src_points, dst_points)
img_output = cv2.warpAffine(img, affine_matrix, (cols,rows))

cv2.imshow('Input', img)
cv2.imshow('Output', img_output)
cv2.waitKey()

What just happened?

As we discussed earlier, we are defining control points. We just need three points to get the affine transformation matrix. We want the three points in src_points to be mapped to the corresponding points in dst_points. We are mapping the points as shown in the following:

What just happened?

To get the transformation matrix, we have a function called getAffineTransform in OpenCV. Once we have the affine transformation matrix, we use the warpAffine function to apply this matrix to the input image.

Following is the input image:

What just happened?

If you run the preceding code, the output will look something like this:

What just happened?

We can also get the mirror image of the input image. We just need to change the control points in the following way:

src_points = np.float32([[0,0], [cols-1,0], [0,rows-1]])
dst_points = np.float32([[cols-1,0], [0,0], [cols-1,rows-1]])

Here, the mapping looks something like this:

What just happened?

If you replace the corresponding lines in our affine transformation code with these two lines, you will get the following result:

What just happened?
主站蜘蛛池模板: 西安市| 龙山县| 洛浦县| 思茅市| 阳高县| 永城市| 晴隆县| 当阳市| 保靖县| 合川市| 色达县| 太康县| 灵山县| 昭平县| 油尖旺区| 黄陵县| 曲水县| 大英县| 十堰市| 依安县| 卫辉市| 青河县| 黄梅县| 都兰县| 富平县| 安国市| 江永县| 云霄县| 土默特左旗| 西峡县| 江孜县| 二连浩特市| 香格里拉县| 太谷县| 措勤县| 庐江县| 武功县| 大庆市| 梁平县| 潼南县| 贵州省|