Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”
This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.
Python OpenCV 365 day learning plan, enter the graphics realm with eraser. This blog is the 41st in the series.
Foundation of basic knowledge
Common geometric transformations in OpenCV include scaling, affine, and perspective transformations. We have already learned scaling functions in the previous content. Today, we are reviewing old knowledge and learning new knowledge.
Let’s take a look at the function prototype corresponding to the three geometric transformations:
dst = cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]])
dst = cv2.warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]])
dst = cv2.warpPerspective(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]])
Copy the code
Let’s start with scaling. The function is named cv2.resize(). There are two non-empty parameters, SRC and dsize, meaning the size of the source image and the scaled image.
import cv2 as cv
src = cv.imread("./t1.jpg")
cv.imshow("src", src)
dst = cv.resize(src, (200.200))
cv.imshow("dst", dst)
cv.waitKey(0)
cv.destroyAllWindows()
Copy the code
This is the simplest code, and it works like this, implementing a simple changeA common error occurs in this case, where the zoomed value provides a floating point type, with the error message
TypeError: integer argument expected, got float
Copy the code
Note also that the two values of the tuple dsize are described as follows, in the correct order.
# dsize = (cols,rows)
dst = cv.resize(src, (400.200))
Copy the code
This place really, really need to remember a lot of detail, for example in the cartesian coordinate system, and record a point coordinates are first x axis, y axis after, but in the computer image is saved in the form of matrix, after the first column, so wide high x x channel images are stored in the three array x width x channel, at the time of image processing, They are memorized in terms of height x width X channel, such as shape.
src = cv.imread("./t1.jpg")
print(src.shape)
Copy the code
The output is (height, width, channel), but the resize function does not follow this, it still uses the (width, height) Settings.
Fx and fy are the zoom ratios of images in x and y directions. To use this parameter, dsize should be set to (0,0) in advance. The test code is as follows:
import cv2 as cv
src = cv.imread("./t1.jpg")
print(src.shape)
cv.imshow("src", src)
# dsize = (cols,rows)
dst = cv.resize(src, (0.0),fx=0.5,fy=0.5)
cv.imshow("dst", dst)
cv.waitKey(0)
cv.destroyAllWindows()
Copy the code
If dsize is not set to (0,0), fx and fy will not take effect. Dsize =(0,0);
SystemError: new style getargs format but argument is not a tuple
Copy the code
Interplolation Interpolation method when scaling
Interplolation is the interpolation method for scaling. There are several methods, which are the focus of today’s exploration.
cv.INTER_NEAREST
: nearest neighbor interpolation;cv.INTER_LINEAR
: bilinear interpolation (default);cv.INTER_CUBIC
: bicubic interpolation in 4×4 pixel neighborhood;cv2.INTER_AREA
: Resampling based on local pixels. It may be the preferred method for image extraction because it produces cloud-free textures. But when the image is scaled, it looks likeINTER_NEAREST
Methods.
Nearest neighbor interpolation
This is a little bit more energy, so in this hour, we’re going to try to figure out an interpolation algorithm, the nearest neighbor interpolation.
The idea of this algorithm is to get the target pixel value from the existing pixel value. I found the most popular explanation while studying, and I will explain it to you.
Suppose there is a 3×3 grayscale image, and a 4×4 grayscale image needs to be obtained through the nearest neighbor interpolation algorithm.First through the coordinate system to understand the change of pixels when scaling.The last conclusion that can be drawn from the figure above is:
- X value of target pixel = x value of original pixel * multiple;
- Y value of target pixel = y value * multiple of original pixel;
What are the multiples of this case? It’s easy to calculate. The original picture was 3×3, now it’s 4×4, and that multiple is 3/4 = 0.75 in both x and y.
Take a look at the result of the operation as follows:Here enumerate the pixel values of two points calculated to take the target image4 x4 grayscale
In the(3, 0)
与 (3, 3)
Two points to illustrate.
(3x0)
The value of the dot is equal to(3 x 0.75 ≈ 2,0 x 0.75 = 0)
, the original image(2, 0)
The color of the dot is zero222
.(3x3)
The value of the dot is equal to(3 x 0.75 ≈ 2,3 x 0.75 ≈ 2)
, the original image(2, 2)
The color of the dot is zero45
;
After mastering the principle, you can implement the algorithm yourself, first look at the OpenCV built-in function implementation results.
import cv2 as cv
import numpy as np
# nearest neighbor interpolation algorithm, the source of dream eraser to https://dream.blog.csdn.net/
def nearest_demo(src, multiple_x, multiple_y) :
src_y, src_x, src_c = src.shape
tar_x, tar_y, tar_c = src_x*multiple_x, src_y*multiple_y, src_c
# Generate a black target image
tar_img = np.zeros((tar_y, tar_x, tar_c), dtype=np.uint8)
print(tar_img.shape)
Render the value of the pixels
# Note that y is the height and x is the width
for y in range(tar_y-1) :for x in range(tar_x-1) :Calculate what the new coordinates (x,y) are in the old graph
old_y = round(y/multiple_y)
old_x = round(x/multiple_x)
tar_img[y, x] = src[old_y, old_x]
return tar_img
src = cv.imread("./t2.jpeg")
print(src.shape)
cv.imshow("src", src)
# dsize = (cols,rows)
dst = cv.resize(src, (0.0), fx=2, fy=2, interpolation=cv.INTER_NEAREST)
cv.imshow("dst", dst)
new_dst = nearest_demo(src, 2.2)
cv.imshow("new_dst", new_dst)
cv.waitKey(0)
cv.destroyAllWindows()
Copy the code
After running it, it turns out that the official algorithm is actually better.
Eraser section
I hope you learned something from today’s hour, and I’ll see you in the next blog