[OpenCV] Image transformation (zooming, translation, rotation, affine)


foreword

Image transformation refers to changing the shape and size of an image by performing transformations such as scaling, translation, rotation, affine, and perspective on the image. In this blog, we will introduce the image transformation functions in OpenCV in detail, and provide sample codes to help readers better understand how to use these functions.


1. Scale transformation

Scaling transformation refers to changing the shape of an image by changing its size. In OpenCV, you can use the resize function to implement scaling transformations. The prototype of this function is as follows:

void resize(InputArray src, OutputArray dst, Size dsize, double fx=0, double fy=0, int interpolation=INTER_LINEAR )

Among them, src represents the original image, dst represents the output image, dsize represents the size of the output image, fx and fy represent the scaling factors in the x and y directions, and interpolation represents the scaling method. There are five common types:

  1. cv2.INTER_NEAREST nearest neighbor interpolation method;
  2. cv2.INTER_LINEAR bilinear interpolation method (default) (enlargement is recommended);
  3. cv2.INTER_AREA Resampling based on local pixels (recommended for shrinking);
  4. cv2.INTER_CUBIC is based on the 3-time interpolation method of 4x4 pixel neighborhood (recommend zooming in);
  5. cv2.INTER_LANCZOS4 lanczos interpolation based on 8x8 pixel neighborhood;

Here is a simple sample code showing how to use the resize function for image scaling:

import cv2

# 加载图像
img = cv2.imread('input.jpg')

# 缩放图像
resized_img = cv2.resize(img, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_LINEAR)

# 显示缩放后的图像
cv2.imshow('Resized Image', resized_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

In the above sample code, we first load the input image using the cv2.imread function, then reduce the image by half using the cv2.resize function, and finally display the scaled image using the cv2.imshow function.

2. Translation transformation

**Translation transformation refers to changing the shape of the image by changing the position of the image. **In OpenCV, you can use the warpAffine function to implement translation transformation. The prototype of this function is as follows:

void warpAffine(InputArray src, OutputArray dst, InputArray M, Size dsize, int flags=INTER_LINEAR, int borderMode=BORDER_CONSTANT, const Scalar& borderValue=Scalar())

Among them, src represents the original image, dst represents the output image, M represents the transformation matrix, dsize represents the size of the output image, flags represents the interpolation method, borderMode represents the border fill mode, and borderValue represents the fill color.

Here is a simple sample code showing how to use the warpAffine function for image translation:

import cv2
import numpy as np

# 加载图像
img = cv2.imread('input.jpg')

# 定义平移矩阵
M = np.float32([[1, 0, 100], [0, 1, 50]])

# 平移图像
translated_img = cv2.warpAffine(img, M, (img.shape[1], img.shape[0]))

# 显示平移后的图像
cv2.imshow('Translated Image', translated_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

In the above sample code, we first use the cv2.imread function to load the input image, and then define a translation matrix M, where the first line represents a translation of 100 pixels in the x direction, and the second line represents a translation of 50 pixels in the y direction. Finally, we use the cv2.warpAffine function to translate the image and use the cv2.imshow function to display the translated image.

3. Rotation transformation

** Rotation transformation refers to changing the shape of an image by changing its orientation. ** In OpenCV, you can use the getRotationMatrix2D and warpAffine functions to achieve rotation transformation. The getRotationMatrix2D function is used to calculate the rotation matrix, and the warpAffine function is used to rotate the image. The prototypes of these two functions are as follows:

Mat getRotationMatrix2D(Point2f center, double angle, double scale);
void warpAffine(InputArray src, OutputArray dst, InputArray M, Size dsize, int flags=INTER_LINEAR, int borderMode=BORDER_CONSTANT, const Scalar& borderValue=Scalar())

Among them, center represents the rotation center, angle represents the rotation angle, scale represents the zoom factor, src represents the original image, dst represents the output image, M represents the transformation matrix, dsize represents the size of the output image, flags represents the interpolation method, borderMode represents the border fill mode, and borderValue represents the fill color.

Here is a simple sample code showing how to use getRotationMatrix2D and warpAffine function for image rotation:

import cv2
import numpy as np

# 加载图像
img = cv2.imread('input.jpg')

# 计算旋转矩阵
center = (img.shape[1] / 2, img.shape[0] / 2)
angle = 45
scale = 1
M = cv2.getRotationMatrix2D(center, angle, scale)

# 旋转图像
rotated_img = cv2.warpAffine(img, M, (img.shape[1], img.shape[0]))

# 显示旋转后的图像
cv2.imshow('Rotated Image', rotated_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

In the above sample code, we first use the cv2.imread function to load the input image, and then use the cv2.getRotationMatrix2D function to calculate the rotation matrix M, where center represents the rotation center, angle represents the rotation angle, and scale represents the scaling factor. Finally, we use the cv2.warpAffine function to rotate the image and use the cv2.imshow function to display the rotated image.

4. Affine transformation

**Affine transformation refers to changing the shape of an image through a linear transformation. **In OpenCV, you can use the getAffineTransform and warpAffine functions to implement affine transformation. The getAffineTransform function is used to calculate the affine transformation matrix, and the warpAffine function is used to perform affine transformation on the image. The prototypes of these two functions are as follows:

Mat getAffineTransform(const Point2f src[], const Point2f dst[]);
void warpAffine(InputArray src, OutputArray dst, InputArray M, Size dsize, int flags=INTER_LINEAR, int borderMode=BORDER_CONSTANT, const Scalar& borderValue=Scalar() )

Among them, src represents the three points in the original image, dst represents the three transformed points, M represents the transformation matrix, dsize represents the size of the output image, flags represents the interpolation method, borderMode represents the border fill mode, and borderValue represents the fill color.

The following is a simple sample code showing how to use getAffineTransform and warpAffine functions for affine transformation:

import cv2
import numpy as np

# 加载图像
img = cv2.imread('input.jpg')

# 定义原图像中的三个点和变换后的三个点
src_points = np.float32([[50, 50], [200, 50], [50, 200]])
dst_points = np.float32([[10, 100], [200, 50], [100, 250]])

# 计算仿射变换矩阵
M = cv2.getAffineTransform(src_points, dst_points)

# 仿射变换图像
affine_img = cv2.warpAffine(img, M, (img.shape[1], img.shape[0]))

# 显示变换后的图像
cv2.imshow('Affine Image', affine_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

In the above sample code, we first use the cv2.imread function to load the input image, and then define three points in the original image and three points after transformation, where src_points represents the coordinates of the three points in the original image, and dst_points represents the coordinates of the three points after transformation. Finally, we use the cv2.getAffineTransform function to calculate the affine transformation matrix M, and use the cv2.warpAffine function to affine transform the image, and use the cv2.imshow function to display the transformed image.

In addition to the several transformations introduced above, OpenCV also provides many other image transformation functions, such as perspective transformation, polar coordinate transformation, etc., which will be shared with you later.

Guess you like

Origin blog.csdn.net/qq_41454577/article/details/131068017