TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

OpenCV Resize Image

OpenCV Resize Image with What is OpenCV, History, Installation, Reading Images, Writing Images, Resize Image, Image Rotation, Gaussian Blur, Blob Detection, Face Detection and Face Recognition etc.

<< Back to OPENCV

OpenCV Resize the image

Sometimes, 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: 1

import 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)

OpenCV Resize the image

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:

  • src - source/input image (required).
  • dsize - desired size for the output image(required)
  • fx - Scale factor along the horizontal axis.(optional)
  • fy - Scale factor along the vertical axis.
  • Interpolation(optional) - This flag uses following methods:
    • INTER_NEAREST - A nearest-interpolation INTER_AREA - resampling using pixel area relation. When we attempt to do image zoom, it is similar to the INTER_NEAREST method.
    • INTER_CUBIC - A bicubic interpolation over 4�4 pixel neighborhood.
    • INTER_LANCOZS4 - Lanczos interpolation over 8�8 pixel neighborhood.

Example of resizing the images

There are several ways to resize the image. Below are some examples to perform resize operation:

  1. Retain Aspect Ratio ( height to width ratio of the image is retained)
    • Downscale(Decrement in the size of the image)
    • Upscale(Increment in the size of image)
  2. Do not preserve Aspect Ratio
    • Resize only the width
    • Resize only the height
  3. Resize the specified width and height

Retain the aspect ratio

  • Downscale with resize()
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)

Example of resizing the images

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)

Example of resizing the images

Not retaining the aspect ratio

  • Resize only the width

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)

Example of resizing the images
  • Resize the height

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)

Example of resizing the images

Resize the specific width and height

  • We can specify both 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:

Example of resizing the images




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf