In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, I will talk to you about how to analyze the geometric transformation in Python image processing. Many people may not know much about it. In order to let everyone know more, Xiaobian summarizes the following contents for everyone. I hope you can gain something according to this article.
I. image geometric transformation
Image geometric transformation does not change the pixel value of the image, pixel transformation is performed on the image plane. Proper geometric transformation can eliminate the negative influence of geometric distortion caused by imaging angle, perspective relationship and lens itself to the greatest extent. Geometric transformation is often used as a preprocessing step in image processing applications and is one of the core tasks of image normalization [1].
A geometric transformation requires a two-part operation:
Spatial transformation: including translation, scaling, rotation and ortho parallel projection, etc., it is needed to express the pixel mapping relationship between the output image and the input image.
Gray interpolation algorithm: According to this transformation relationship, the pixels of the output image may be mapped to non-integer coordinates of the input image [2].
A mapping relationship between pixels of the original image and pixels of the transformed image is established during the process of image geometric transformation. By this relationship, the coordinate position of pixels of one side can be calculated from pixels of the other side. The process of mapping image coordinates to the output is often referred to as forward mapping, whereas the process of mapping the output image to the input is referred to as backward mapping. Backward mapping is often used in practice because it can avoid incomplete mapping and overlapping mapping problems when using forward mapping.
Figure 6-1 shows an example of image enlargement. In the right figure, only four coordinates (0, 0),(0, 2),(2, 0), and (2, 2) have corresponding pixels found in the original image according to the mapping relationship, and the remaining 12 coordinates have no valid values [3].
For digital images, the coordinates of pixels are discrete non-negative integers, but floating point coordinate values may be generated during transformation. This is an invalid coordinate in image processing. To solve this problem, interpolation algorithms are needed. Common algorithms are as follows:
nearest neighbor interpolation
bilinear interpolation
bi-cubic interpolation
Image transformation is based on matrix operation, through matrix operation can quickly find the corresponding relationship. In this article, we will introduce common image geometric transformations, including image translation, image scaling, image rotation, image mirroring, image affine, image perspective, etc.
II. image translation
Image translation is to move all pixels in the image horizontally or vertically according to a given amount of translation. Assuming that the position coordinates of the original pixel are (x0, y0), after translation (△x, △y), the coordinates become (x1, y1), as shown in Figure 6.2 [3-5].
The mathematical formula is expressed as formula (6-1).
The matrix representation is shown in Equation (6-2):
In the formula, the matrix is called the translation transformation matrix or factor, and △x and △y are called translation quantities. Image translation first defines translation matrix M, and then calls warpAffine() function to realize translation. The core function is as follows:
M = np.float32([[1, 0, x], [0, 1, y]])
- M denotes the translation matrix, where x denotes horizontal translation and y denotes vertical translation
shifted = cv2.warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]])
src represents the original image
- M denotes translation matrix
- dsize indicates the size of the transformed output image
- dst is the output image, its size is dsize, the same type as src
- flag indicates a combination and optional value of interpolation methods
- borderValue indicates pixel extrapolation, and when borderMode = BORDER_TRANSPARENT, indicates that pixels in the target image do not modify outliers in the source image.
- borderValue is used when the boundary is unchanged, 0 by default
The following code is a simple example of image translation, which defines the image translation matrix M and then calls warpAffine() to translate the original image vertically down 50 pixels and horizontally to the right 100 pixels.
# -*- coding: utf-8-*-# By: Eastmountimport cv2import numpy as np#Read image src = cv2.imread ('scenery.png ')#Image translation matrix M = np.float32([[1, 0, 100], [0, 1, 50])#Get original image columns and rows, cols = src.shape[:2]#Image translation result = cv2.warpAffine(src, M, (cols, rows)) #Display image cv2.imshow ("original", src)cv2.imshow("result", result)#Wait for cv2.waitKey(0) cv2.destrucyAllWindows ()
The output is shown in Figure 6-3:
The following example is the process of translating the image down, up, right, and left, respectively, and then calling matplotlib to draw the image in turn.
# -*- coding:utf-8 -*-# By: Eastmountimport cv2 import numpy as npimport matplotlib.pyplot as plt #Read Image img = cv2.imread ('scenery.png')image = cv2.cvtColor (img,cv2.COLOR_BGR2RGB)#Image pan #Vertical pan down 100M = np.float32 ([[1, 0, 0], [0, 1, 100]])img1 = cv2.warpAffine (image, M, (image.shape[1], image.shape[0]))#Vertical translation up 100M = np.float32 ([[1, 0, 0], [0, 1, -100]])img2 = cv2.warpAffine (image, M, (image.shape[1], image.shape[0]))#Horizontal translation to the right 100M = np.float32 ([[1, 0, 100], [0, 1, 0]])img3 = cv2.warpAffine (image, M, (image.shape[1], image.shape[0]))#Horizontal left translation 100M = np.float32 ([[1, 0, -100], [0, 1, 0]])img4 = cv2.warpAffine (image, M, (image.shape[1], image.shape[0]))#Loop Display Graphics titles = [ 'Image 1','Image 2','Image 3','Image 4'] images = [img1, img2, img3, img4] for i in range(4): plt.subplot(2,2,i+1), plt.imshow(images[i], 'gray') plt.title(titles[i]) plt.xticks([]),plt.yticks([]) plt.show()
The output is shown in Figure 6-4, translated in all four directions, and the subplot() function is called to draw the four subplots together.
III. image scaling
Image scaling refers to the process of resizing digital images. In Python, image scaling is mainly implemented by calling the resize() function, and the function prototype is as follows:
result = cv2.resize(src, dsize[, result[. fx[, fy[, interpolation]]]])
src represents the original image
- dsize indicates the size of the image zoom
- result indicates the image result
- fx indicates the multiple of the image x-axis scaling size
- fy indicates the multiple of the image y-axis scaling size
- interpolation indicates the transformation method. CV_INTER_NN represents nearest neighbor interpolation;CV_INTER_LINEAR represents bilinear interpolation (default);
CV_INTER_AREA indicates resampling using pixel relations, which can avoid ripples when the image is reduced, similar to CV_INTER_NN when the image is enlarged;
CV_INTER_CUBIC indicates cubic interpolation
Two common ways to scale an image are as follows, the first way is to set the original image to (160, 160) pixel size, and the second way is to reduce the original image to 0.5 times.
result = cv2.resize(src, (160,160))
result = cv2.resize(src, None, fx=0.5, fy=0.5)
Let (x1, y1) be the coordinates after scaling,(x0, y0) be the coordinates before scaling, sx, sy be scaling factors, then the calculation formula of image scaling is shown in (6-3):
Below is Python code for image scaling, which reduces the size of the landscape image it reads.
# -*- coding:utf-8 -*-# By:Eastmountimport cv2 import numpy as np #Read image src = cv2.imread ('scenery.png ')#Image zoom result = cv2.resize(src, (200,100))print(result.shape)#Show image cv2.imshow ("original", src)cv2.imshow("result", result)#Wait to display cv2.waitKey(0)cv2.destroyAllWindows()
The output is shown in Figure 6-5, with the image reduced to (100, 200, 3) pixels. Note that the code calls the function cv2.resize(src, (200,100)) to set the new image size dsize to 200 columns and 100 rows.
Another method of image scaling transformation is explained below. The image transformation is performed by multiplying the original image pixels by the scaling coefficient. The code is as follows:
# -*- coding:utf-8 -*-# By:Eastmountimport cv2 import numpy as np #Read image src = cv2.imread ('scenery.png ')rows, cols = src.shape[:2]print(rows, cols)#Image zoom dsize(columns, rows)result = cv2.resize(src, (int(cols*0.6), int(rows*1.2))#Show image cv2.imshow("src", src)cv2.imshow("result", result)cv2.waitKey(0)cv2.destroyAllWindows()
Obtain the pixel value of the element of the picture "scenery.png", its rows value is 384, its cols value is 512, and then perform the processing of reducing the width by 0.6 times and enlarging the height by 1.2 times. The comparison effect before and after running is shown in Figure 6-6.
Finally, explain how to call the (fx,fy) parameter to set the zoom factor, and zoom in or out on the original image. The following code reduces the size of the original image by 0.3 times in the fx and fy directions.
# -*- coding:utf-8 -*-# By:Eastmountimport cv2 import numpy as np #Read image src = cv2.imread ('scenery.png ')rows, cles = src.shape[:2]print(rows, cles)#Image zoom result = cv2.resize(src, None, fx=0.3, fy=0.3)#Show image cv2.imshow("src", src)cv2.imshow("result", result)#Wait for cv2.waitKey(0) cv2.destrucyAllWindows ()
The output is shown in Figure 6-7, scaled down by 0.3×0.3.
IV. image rotation
Image rotation refers to the process that an image rotates a certain angle around a certain point to form a new image. Image rotation transformation will have a rotation center, this rotation center is generally the center of the image, after rotation the size of the image will generally change. Figure 6-8 shows the rotation of the coordinates (x0, y0) of the original image to (x1, y1).
The rotation formula is shown in (6-4), where (m,n) is the center of rotation, a is the angle of rotation, and (left,top) is the upper-left corner coordinate of the rotated image.
The image rotation transformation is mainly realized by calling getRotationMatrix2D() function and warpAffine() function, rotating around the center of the image. The function prototype is as follows:
M = cv2.getRotationMatrix2D(center, angle, scale)
- center indicates the rotation center point, usually set to (col/2, rows/2)
- angle indicates rotation angle, positive value indicates counterclockwise rotation, coordinate origin is set to upper left corner
- scale indicates the scale factor
rotated = cv2.warpAffine(src, M, (cols, rows))
src represents the original image
- M represents the rotation parameter, i.e. the result defined by getRotationMatrix2D() function
- (cols, rows) Indicates the width and height of the original image
The implementation code looks like this:
# -*- coding:utf-8 -*-# By:Eastmountimport cv2 import numpy as np #Read image src = cv2.imread ('scenery.png ')#Height, width and number of channels of source image rows, cles, channel = src.shape#Rotation around center of image #Function parameters: Rotation center rotation degrees scaleM = cv2.getRotationMatrix2D ((cols/2, rows/2), 30, 1)#Function Parameters: Original Image Rotation Parameters Element Image Width Height rotated = cv2.warpAffine(src, M, (cols, rows)) #Display Image cv2.imshow("src", src)cv2.imshow("rotated", rotated)#Wait for cv2.waitKey(0)cv2.destroyAllWindows()
The display effect is shown in Figure 6-9, rotated 30 degrees counterclockwise around the center of the image.
After reading the above, do you have any further understanding of how to analyze geometric transformations in Python image processing? If you still want to know more knowledge or related content, please pay attention to the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.