Javatpoint Logo
Javatpoint Logo

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

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:

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 44 pixel neighborhood.
    • INTER_LANCOZS4 - Lanczos interpolation over 88 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()

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

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.

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.

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.

Output:

Example of resizing the images




Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA