C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
OpenCV Resize the imageSometimes, it is necessary to transform the loaded image. In the image processing, we need to resize the image to perform the particular operation. Images are generally stored in Numpy ndarray(array). The ndarray.shape is used to obtain the dimension of the image. We can get the width, height, and numbers of the channels for each pixel by using the index of the dimension variable. Example: 1import cv2 img = cv2.imread(r'C:\Users\DEVANSH SHARMA\cat.jpeg', 1) scale = 60 width = int(img.shape[1] * scale / 100) height = int(img.shape[0] * scale / 100) dim = (width, height) # resize image resized = cv2.resize(img, dim, interpolation=cv2.INTER_AREA) print('Resized Dimensions : ', resized.shape) cv2.imshow("Resized image", resized) cv2.waitKey(0) cv2.destroyAllWindows() Output: Resized Dimensions : (199, 300, 3) The resizing of image means changing the dimension of the image, its width or height as well as both. Also the aspect ratio of the original image could be retained by resizing an image. OpenCV provides cv2.resize() function to resize the image. The syntax is given as: cv2.resize(src, dsize[, dst[, fx[,fy[,interpolation]]]) Parameters:
Example of resizing the imagesThere are several ways to resize the image. Below are some examples to perform resize operation:
Retain the aspect ratio
import cv2 img = cv2.imread(r'C:\Users\DEVANSH SHARMA\cat.jpeg', 1) print('Original Dimensions : ', img.shape) scale = 60 # percent of original size width = int(img.shape[1] * scale / 100) height = int(img.shape[0] * scale / 100) dim = (width, height) # resize image resized = cv2.resize(img, dim, interpolation=cv2.INTER_AREA) print('Resized Dimensions : ', resized.shape) cv2.imshow("Resized image", resized) cv2.waitKey(0) cv2.destroyAllWindows() Output: Original Dimensions : (332, 500, 3) Resized Dimensions : (199, 300, 3) In the above example, the scale_per variable holds the percentage of the image which needs to be scaled. The value<100 is used to downscale the provided image. We will use this scale_per value along with the original image's dimension to calculate the width and height of the output image. Upscale with resize() import cv2 img = cv2.imread(r'C:\Users\DEVANSH SHARMA\cat.jpeg', 1) print('Original Dimensions : ', img.shape) scale = 150 # percent of original size width = int(img.shape[1] * scale / 100) height = int(img.shape[0] * scale / 100) dim = (width, height) # resize image resized = cv2.resize(img, dim, interpolation=cv2.INTER_AREA) print('Resized Dimensions : ', resized.shape) cv2.imshow("Resized image", resized) cv2.waitKey(0) cv2.destroyAllWindows() Output: Original Dimensions : (332, 500, 3) Resized Dimensions : (398, 600, 3) Not retaining the aspect ratio
In the below example, we have provided a specific value in pixel for width and the height will remain unaffected. import cv2 img = cv2.imread(r'C:\Users\DEVANSH SHARMA\cat.jpeg', cv2.IMREAD_UNCHANGED) print('Original Dimensions : ', img.shape) width = img.shape[1] # keep original width height = 440 dim = (width, height) # resize image resized = cv2.resize(img, dim, interpolation=cv2.INTER_AREA) print('Resized Dimensions : ', resized.shape) cv2.imshow("Resized image", resized) cv2.waitKey(0) cv2.destroyAllWindows() Output: Original Dimensions : (332, 500, 3) Resized Dimensions : (440, 500, 3)
In the below example, the scale_per value holds the percentage by which height has to be scaled or we can provide the specific value in pixels. import cv2 img = cv2.imread(r'C:\Users\DEVANSH SHARMA\cat.jpeg', 1) print('Original Dimensions : ', img.shape) width = img.shape[1] # keep original width height = 200 dim = (width, height) # resize image resized = cv2.resize(img, dim, interpolation=cv2.INTER_AREA) print('Resized Dimensions : ', resized.shape) cv2.imshow("Resized image", resized) cv2.waitKey(0) cv2.destroyAllWindows() Output: Original Dimensions : (332, 500, 3) Resized Dimensions : (200, 500, 3) Resize the specific width and height
import cv2 img = cv2.imread(r'C:\Users\DEVANSH SHARMA\cat.jpeg', 1) print('Original Dimensions : ', img.shape) width = 350 height = 450 dim = (width, height) # resize image resized = cv2.resize(img, dim, interpolation=cv2.INTER_AREA) print('Resized Dimensions : ', resized.shape) cv2.imshow("Resized image", resized) cv2.waitKey(0) cv2.destroyAllWindows() Output:
Next TopicOpenCV Image Rotation
|