This is the seventh day of my participation in the August More text Challenge. For details, see:August is more challenging
If you have any ideas or techniques you’d like to share, feel free to leave them in the comments section.
This blog will only learn one method, cv2.resize, or image scaling.
Matting knowledge
In the retrieval of image scaling related knowledge points, found a related algorithm, called interpolation algorithm, specifically involving the nearest neighbor interpolation algorithm, bilinear interpolation algorithm and other content.
It is not wise to involve it on the 9th day of learning, so we skip this part of theoretical knowledge and directly use the resize method in OpenCV to realize the interpolation algorithm.
Resize method format and parameters
Resize method can achieve image size transformation, including scaling, the default method is just mentioned bilinear interpolation algorithm.
The method is defined as follows:
dst = cv2.resize(src,dsize,dst=None,fx=None,fy=None,interpolation=None)
Copy the code
Parameter description:
- SRC: Input an image
- Dsize: Size of the output image. If this parameter is 0, it means that the size after scaling needs to be calculated by the formula,
dsize = Size(round(fx*src.cols),round(fy*src.rows))
. Among themfx
与fy
Is the scale of the image in Width and Height directions. - Fx: Width Scale in the direction. If 0, follow
dsize * width/src.cols
To calculate - Fy: The scale in the Height direction, if 0, according to
dsize * height/src.rows
To calculate - Interpolation: interpolation type, or interpolation mode. Default is bilinear interpolation
Method returns the result DST to output the image.
Program implementation
The following example sets dsize to None and then fx and fy to 0.5.
import cv2
import numpy as np
img = cv2.imread('9.jpg')
# Scaling: fx=0.5,fy=0.5
dst = cv2.resize(img,None,fx=0.5,fy=0.5,interpolation=cv2.INTER_LINEAR)
cv2.imshow('dst', dst)
cv2.waitKey(0)
Copy the code
The above method is relatively simple, you can directly zoom to achieve, you can also get the original size of the image and then adjust the scale.
import cv2
img = cv2.imread('9.jpg')
# Get the horizontal and vertical dimensions of the original image
height, width = img.shape[:2]
# dsize= (0.5*width,0.5*height)
dst = cv2.resize(img, (int(0.6 * width), int(0.5 * height)), interpolation=cv2.INTER_LINEAR)
cv2.imshow('dst', dst)
cv2.waitKey(0)
Copy the code
At this point, you already know the simplest way to scale an image with OpenCV. You don’t need to learn the interpolation algorithm right now.
By the way, if you want to implement bilinear interpolation on your own instead of using the existing resize method, see this article.
Altli.blog.csdn.net/article/det…
The last parameter is interpolation
Interpolation is the interpolation method.
- INTER_NEAREST: interpolation of nearest neighbors
- INTER_LINEAR: linear interpolation (default)
- INTER_AREA: area interpolation
- INTER_CUBIC: cubic spline interpolation
- INTER_LANCZOS4: Lanczos interpolation
INTER_AREA and CV2.inter_CUBIC are recommended for zoom out. Cv2.inter_cubic and CV2.inter_linear are recommended for zoom out. The former is slower than the latter.
The above values can be set by yourself to see different effects.
OpenCV end
One hour has passed. Have you mastered the knowledge of OpenCV in Python?
In your spare time, you can subscribe to the eraser crawler hundred example course to learn crawler knowledge.