Image Geometric Transformation Experiment

1. The purpose of the experiment:

1. Familiar with the operation and basic functions of MATLABor ; OpenCV
2. Understand and master the principles and applications of image translation, vertical mirror transformation, horizontal mirror transformation, scaling and rotation;

2. Experimental principle:

1. The point with the initial coordinates (x,y)is translated (x0,y0), and the coordinates become (x',y'), and the relationship between the two points is:
insert image description here
expressed in matrix form:
insert image description here
2. The mirror transformation of the image is the transformation of exchanging the image with the vertical central axis or the horizontal central axis, It is divided into vertical mirror transformation and horizontal mirror transformation, and the matrix forms of the two are:
insert image description here
3. The transformation matrix of image reduction and enlargement is the same:
insert image description here
at that timeS_x≤1,S_y≤1 , the image is reduced; S_x≥1,S_y≥1at the same time, the image is enlarged.

4. Image rotation is defined as a certain point in the image as the origin to rotate a certain angle counterclockwise or clockwise. Its transformation matrix is:
insert image description here
the transformation matrix is ​​carried out around the origin of the coordinate axis. If it rotates around a specified point (a,b), the coordinate system needs to be translated to this point, rotated, and then translated back to the new coordinate origin.

3. Experiment content steps (record the main steps of the experiment, and after the debugging is successful, take a screenshot or take a photo to save the result) It is required to use MATLABor OpenCVto complete the following experiments.

1. Perform translation transformation on the image;
# 导包
import cv2
import numpy as np
lena = cv2.imread("./lena.jpg")  # 读取原图
height, width = lena.shape[:2]  # 获取图像的高度和宽度
x = 100
y = 200
M = np.float32([[1, 0, x], [0, 1, y]])
Panned_lena = cv2.warpAffine(lena, M, (width, height))
cv2.imshow("原始图像", lena)
cv2.imshow("平移图像", Panned_lena)
cv2.waitKey()
cv2.destroyAllWindows()

insert image description here

2. Perform vertical mirror transformation on the image;
import cv2
lena = cv2.imread("./lena.jpg")
vertical_mirror_lena = cv2.flip(lena, 1)
cv2.imshow("原始图像", lena)
cv2.imshow("垂直镜像图像", vertical_mirror_lena)
cv2.waitKey()
cv2.destroyAllWindows()

insert image description here

3. Perform horizontal mirror transformation on the image;
import cv2
lena = cv2.imread("./lena.jpg")
horizontal_mirror_lena = cv2.flip(lena, 0)
cv2.imshow("原始图像", lena)
cv2.imshow("水平镜像翻转图像", horizontal_mirror_lena)
cv2.waitKey()
cv2.destroyAllWindows()

insert image description here

4. Scale and rotate the image.
import cv2
lena = cv2.imread('./lena.jpg')  # 读入原始图像
print(lena.shape)   # 打印出图片尺寸
x, y = lena.shape[0:2]  # 将图片高和宽分别赋值给x,y
cv2.imshow('原始图像', lena)    # 显示原图

# 缩放到原来的二分之一
lena_zoom_1 = cv2.resize(lena, (int(y / 2), int(x / 2)))
cv2.imshow('缩放至原来二分之一的图像', lena_zoom_1)
cv2.waitKey()

# 缩放到原来的四分之一
lena_zoom_2 = cv2.resize(lena, (0, 0), fx=0.25, fy=0.25,
                         interpolation=cv2.INTER_NEAREST)
cv2.imshow('缩放到原来的四分之一的图像', lena_zoom_2)
cv2.waitKey()
cv2.destroyAllWindows()

insert image description here

import cv2
lena = cv2.imread("./lena.jpg")  # 读取原图
height, width = lena.shape[:2]  # 获取图像的高度和宽度
# 以图像中心为圆点,逆时针旋转 45°,并将目标图像缩小为原始图像的 0.7 倍
M = cv2.getRotationMatrix2D((width/2, height/2), 45, 0.7)
rotate_lena = cv2.warpAffine(lena, M, (width, height))
cv2.imshow("原始图像", lena)
cv2.imshow("旋转图像", rotate_lena)
cv2.waitKey()
cv2.destroyAllWindows()

insert image description here

Guess you like

Origin blog.csdn.net/weixin_51571728/article/details/124796655