
上QQ阅读APP看书,第一时间看更新
How to do it...
- Create a new Python file and import the following packages:
# Scaling (Resizing) Images - Cubic, Area, Linear Interpolations # Interpolation is a method of estimating values between known data points # Import Computer Vision package - cv2 import cv2 # Import Numerical Python package - numpy as np import numpy as np
- Read the image using the built-in imread function:
image = cv2.imread('image_3.jpg')
- Display the original image using the built-in imshow function:
cv2.imshow("Original", image)
- Wait until any key is pressed:
cv2.waitKey()
- Adjust the image size based on the operator's command:
# cv2.resize(image, output image size, x scale, y scale, interpolation)
- Adjust the image size using cubic interpolation:
# Scaling using cubic interpolation scaling_cubic = cv2.resize(image, None, fx=.75, fy=.75, interpolation = cv2.INTER_CUBIC)
- Show the output image:
# Display cubic interpolated image cv2.imshow('Cubic Interpolated', scaling_cubic)
- Wait until any key is pressed:
cv2.waitKey()
- Adjust the image size using area interpolation:
# Scaling using area interpolation scaling_skewed = cv2.resize(image, (600, 300), interpolation = cv2.INTER_AREA)
- Show the output image:
# Display area interpolated image cv2.imshow('Area Interpolated', scaling_skewed)
- Wait for the instruction from the operator:
# Wait until any key is pressed cv2.waitKey()
- Adjust the image size using linear interpolation:
# Scaling using linear interpolation scaling_linear = cv2.resize(image, None, fx=0.5, fy=0.5, interpolation = cv2.INTER_LINEAR)
- Show the output image:
# Display linear interpolated image cv2.imshow('Linear Interpolated', scaling_linear)
- Wait until any key is pressed:
cv2.waitKey()
- After completing the image scaling task, terminate the program execution:
# Close all windows cv2.destroyAllWindows()
- The command used to execute the Scaling.py Python program is shown here:

- The original image used for scaling is shown here:

- Linear interpolated output obtained after executing the Scaling.py file is shown here:

- The area-interpolated output obtained after executing the Scaling.py file is shown here:

- The cubic-interpolated output obtained after executing the Scaling.py file is shown here:
