Welcome to DOFBOT-Ultra repository
6. Image Zoom
Image Zoom
In OpenCV, the function for image scaling is: cv2.resize(InputArray src, OutputArray dst, Size, fx, fy, interpolation)
Code path:
~/dofbot_opencv/scripts/2.Transform/01_zoom_pic.ipynb
Parameter explanation:
| InputArray src | Input image |
|---|---|
| OutputArray dst | Output image |
| Size | Output image size |
| fx, fy | Scale factors along x-axis, y-axis |
| interpolation | Interpolation method |
Available interpolation methods:
| INTER_NEAREST | Nearest neighbor interpolation |
|---|---|
| INTER_LINEAR | Bilinear interpolation (default) |
| INTER_AREA | Resampling using pixel area relation |
| INTER_CUBIC | Bicubic interpolation over 4x4 pixel neighborhood |
| INTER_LANCZOS4 | Lanczos interpolation over 8x8 pixel neighborhood |
Note:
- Output size format is (width, height)
- Default interpolation method: bilinear interpolation
Main code:
x# 1 load 2 info 3 resize 4 checkimport cv2import matplotlib.pyplot as plt # Python 2D plotting library# Read original imageimg = cv2.imread('yahboom.jpg')# Print image dimensionsprint(img.shape)# Assign height and width to x, y respectivelyx, y = img.shape[0:2]# Scale to half the original, output size format is (width, height)img_test1 = cv2.resize(img, (int(y / 2), int(x / 2)))# cv2.imshow('resize0', img_test1)# cv2.waitKey()# Nearest neighbor interpolation scaling# Scale to one quarterimg_test2 = cv2.resize(img, (0, 0), fx=0.25, fy=0.25, interpolation=cv2.INTER_NEAREST)# cv.imshow('resize1', img_test2)# cv.waitKey()# cv.destroyAllWindows()img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)dst1 = cv2.cvtColor(img_test1, cv2.COLOR_BGR2RGB)dst2 = cv2.cvtColor(img_test2, cv2.COLOR_BGR2RGB)# Display original imageplt.imshow(img)plt.show()After execution, you can see the image is 800*800

xxxxxxxxxx# Display scaled 1/2plt.imshow(dst1)plt.show()After execution, you can see the image is 400*400, scaled by half

xxxxxxxxxx# Display scaled 1/4 nearest neighbor interpolationplt.imshow(dst2)plt.show()After execution, you can see the image is 200*200, scaled to one quarter.

Below we talk about matplotlib: Python 2D plotting library.
Reference tutorial: https://www.runoob.com/numpy/numpy-matplotlib.html
xxxxxxxxxximport numpy as npfrom matplotlib import pyplot as pltx = np.arange(1,11)y = 2 * x + 5plt.title("Matplotlib demo")plt.xlabel("x axis caption")plt.ylabel("y axis caption")plt.plot(x,y) plt.show()