Home > Article > Backend Development > Computer Vision Examples in Python: Image Transformation
With the continuous development of modern computer technology, computer vision technology has become one of the important branches of digital image processing and computer graphics. Computer vision technology can process and analyze images so that users can obtain information and complete target tasks. In this article, we will explore some computer vision examples written in Python, specifically, image transformations. We will cover some common image transformation methods, such as rotation, scaling, cropping, flipping, etc.
First, let’s take a look at how to implement the image rotation function in Python. Rotation is a commonly used image transformation technique. In Python, we can use OpenCV to achieve image rotation. OpenCV is a powerful computer vision library with many convenient and easy-to-use functions that can easily process and analyze images. The following is a simple Python code example that demonstrates how to use OpenCV to implement image rotation:
import cv2 # 读取图像 image = cv2.imread('image.jpg') # 图像旋转 (h, w) = image.shape[:2] center = (w // 2, h // 2) M = cv2.getRotationMatrix2D(center, 45, 1.0) image = cv2.warpAffine(image, M, (w, h)) # 显示图像 cv2.imshow('Rotated Image', image) cv2.waitKey(0) cv2.destroyAllWindows()
In this example, we first read the input image using the cv2.imread
function. Next, we use the cv2.getRotationMatrix2D
function to define the rotation matrix. This function needs to pass in three parameters: rotation center, rotation angle and scaling ratio. Finally, we use the cv2.warpAffine
function to rotate the image. This function needs to pass in three parameters: input image, transformation matrix and output image size. After processing the image, we use the cv2.imshow
function to display the rotated image, then use the cv2.waitKey
function to wait for the user's key press, and finally use cv2.destroyAllWindows
Function to close all windows.
Next, let’s take a look at how to use Python to implement the image scaling function. Scaling is a very important image transformation technique that can make an image smaller or larger. In Python, we can also use OpenCV to achieve image scaling. The following is a simple Python code example that demonstrates how to use OpenCV to implement image scaling:
import cv2 # 读取图像 image = cv2.imread('image.jpg') # 图像缩放 scale_percent = 50 width = int(image.shape[1] * scale_percent / 100) height = int(image.shape[0] * scale_percent / 100) dim = (width, height) image = cv2.resize(image, dim, interpolation = cv2.INTER_AREA) # 显示图像 cv2.imshow('Resized Image', image) cv2.waitKey(0) cv2.destroyAllWindows()
In this example, we use the cv2.imread
function to read the input image. Next, we define a scaling ratio and calculate the width and height of the new image based on that ratio. Finally, we use the cv2.resize
function to scale and transform the image. This function requires three parameters: input image, new image size and interpolation method. After processing the image, we use the cv2.imshow
function to display the scaled image, then use the cv2.waitKey
function to wait for the user's key press, and finally use cv2.destroyAllWindows
Function to close all windows.
In addition to image rotation and scaling, Python can also implement many other image transformation functions, such as cropping, flipping, affine, etc. These image transformation technologies can be widely used in the field of computer vision, such as image processing, computer graphics, machine learning, etc. By learning computer vision examples in Python, we can gain a deeper understanding of the principles and methods of image transformation and lay a good foundation for future work.
The above is the detailed content of Computer Vision Examples in Python: Image Transformation. For more information, please follow other related articles on the PHP Chinese website!