OpenCV-Python Development Guide (10) --- Scaling of Geometric Transformation

Preface

For the conversion of color space, we will explain to you through HSV actual combat. As for the conversion and application of other color spaces, the follow-up learning other knowledge will come separately. From the beginning of this article, we will learn the geometric transformation of OpenCV.

For example, in the field of image processing, we often encounter operations such as image zooming, rotation, etc., especially for users who have experience in using PS, these operations must be handy, but in fact the underlying code is the knowledge to be explained later. In this article, we will introduce the knowledge of zooming.

Zoom

In OpenCV, the zoom function it provides us is cv2.resize(), which is defined as follows:

def resize(src, dsize, dst=None, fx=None, fy=None, interpolation=None):

src: represents the original image that needs to be scaled

dsize: the size of the zoomed image, it can also be called the size

fx: Represents the scaling ratio in the horizontal direction

fy: represents the scaling ratio in the vertical direction

interpolation: represents the interpolation method

The interpolation values ​​are shown in the table:

Types of Description
cv2.INTER_LINEAR Bilinear interpolation (default method)
cv2.INTER_NEARSET Nearest interpolation
cv2.INTER_CUBIC Cubic spline interpolation. First, perform cubic spline fitting on the 4*4 neighboring area near the source image, and then use the cubic spline value corresponding to the target pixel as the value of the pixel point corresponding to the target image
cv2.INTER_AREA Regional interpolation, sampling the current pixel according to the pixels in the surrounding area of ​​the current pixel, this method is similar to the nearest neighbor interpolation method
cv2.INTER_LANCZ0S4 A Lanczos interpolation method using 8*8 nearest neighbors
cv2.INTER_LINEAR_EXACT Bit-accurate bilinear interpolation
cv2.INTER_MAX Difference encoding mask
cv2.WARP_FILL_OUTLIERS Mark value, fill all the pixels in the target image. If some of them correspond to singular points (outliers) in the source image, they will be set to 0
cv2.WARP_INVERSE_MAP Scaling value, inverse transformation, for example, polar coordinate transformation. If the flag is not set, then convert: dst(Ø,p)=src(x,y); if the flag is set, then convert: dst(x,y)=src(Ø,p)

In the cv2.resize() function, the size of the target image can be specified by either "dsize" or "fx, fy".

When you use dsize to specify, regardless of whether you specify fx, fy, there is a parameter dsize to determine the size of the target image, that is, dsize has the highest priority. The specific mathematical formula is as follows:

Width=(double)dszie.width/src.cols

高度=(double)dszie.height/src.rows

Note that the first parameter of dsize is the number of columns, and the second parameter is the number of rows, which is the opposite of shape.

And when you use fx, fy to specify the target image size, the mathematical formula is as follows:

dsize=Size(round(fxsrc.cols),round(fysrc.rows))

As for the final parameter interpolation, it refers to assigning values ​​to pixels that cannot be directly mapped when performing geometric processing on the image. For example, if the image is enlarged by 2 times in a certain size area, some images may be too small to fit some missing pixels, and some images may be too large to have some pixels. For these pixels, interpolation determines how to determine them Value.

dsize implements scaling

Now that we understand all the parameters of the scaling function resize in OpenCV, let's implement a simple image scaling. The specific code is as follows:

import cv2

img = cv2.imread("4.jpg")
rows,cols=img.shape[:2]
size=(int(cols*2),int(rows*1))
result = cv2.resize(img, size)
cv2.imshow("img", img)
print(img.shape)
cv2.imshow("result", result)
print(result.shape)
cv2.waitKey()
cv2.destroyAllWindows()

Here, we first get the length and width pixels of the picture through img.shape, and then the rows remain unchanged and the columns are enlarged by 2 times. After running, we will get the following picture.

Zoom

fx, fy achieve zoom

Above we are zooming through the dszie parameters. Now we are zooming through the fx and fy parameters. The specific code is as follows:

import cv2

img = cv2.imread("4.jpg")
result = cv2.resize(img, None,fx=2,fy=1)
cv2.imshow("img", img)
print(img.shape)
cv2.imshow("result", result)
print(result.shape)
cv2.waitKey()
cv2.destroyAllWindows()

The effect of this code is consistent with the above, and the running result will not be shown. It can be seen that the code of fx and fy will be more concise and clear.

Guess you like

Origin blog.csdn.net/liyuanjinglyj/article/details/113796231