Sunday, November 17, 2024
Google search engine
HomeLanguagesOpenCV – Resize without Interpolation

OpenCV – Resize without Interpolation

In Interpolation, known data is used to estimate the values at unknown locations with the help of known values. It is used in tasks such as zooming, shrinking, rotating, and geometrically correcting digital images which happens directly in spatial domain. 

In this article, you will learn how to resize an image using Nearest neighbor interpolation technique on OpenCV.

Prerequisites

For employing such Image Processing capabilities on our Python distribution we would be using cv2 library. For installing this library into the python environment, execute the following command in the command processor of the Operating System:

pip install cv2

Also for faster processing, we would be utilizing the NumPy library which could be installed using the following command:

pip install numpy

For demonstration, we would be using the following image

Main Image

 

Why resizing can’t happen without interpolation?

Image resizing is a process of approximation. We use a finite set of data to predict color/intensity values at unknown locations. Therefore it is either impossible or impractical to scale without using some sort of approximation technique for determining the pixel values at the new locations. Ex: If we upscale the image we end up with new pixel locations. Those location don’t have any pixel values. If we don’t use interpolation then the new locations are devoid of intensity. So in order to provide a value to those pixels we use interpolation techniques which calibrate the values of the pixels based on its surroundings. Therefore the closest interpolation method that would produce approximation without producing new intensity levels is nearest neighbor. Which assigns to each new pixel location the intensity of its nearest neighbor (4 neighbor or 8 neighbor) in the original image. 

Let’s see the code for this.

Python3




import numpy as np
import cv2 as cv
  
img = cv.imread("img.jpg")
cv.imhsow('img',img)


Output :

Actual Image

 

In the above code firstly the relevant libraries were imported. Then we opened the predefined image using imread function and saved it to a variable. 

Now we need to use np.shape function to calculate the shape of the image. After which both the dimensions were halved by dividing each by 2. Then a call to the resize function will made passing the image object, desired size, and interpolation used as an argument. This function produces an image whose resolution is downscaled by 50% in contrast to the original. In the end, the altered image is displayed using the imshow function.

Python3




# Obtaining the Dimensions of the image
height, width = img.shape[:2]
  
# Downscaling the image
height = int(height/2)
width = int(width/2)
  
# Performing the resize operation with Nearest neighbor interpolation
res = cv.resize(img, (width, height), interpolation = cv.INTER_NEAREST)
  
# Displaying the image
cv.imshow('img', res)
cv.waitKey(0)
cv.destroyAllWindows()


Output : 

Output After Resize

 

We can also upscaled/downscaled the image by changing 

height = int(height/2)
width = int(width/2)

to 

height = int(x*height)
width = int(x*width)

where x is a scaling constant. Therefore, x = .5 would produced an downscale effect similar to the aforementioned image and x = 3 would produce an image which has thrice the size of the original. The following is are two examples:  

This is the output if x = 0.5 i.e. downscaled to half size.

x = 0.5

 

Output for x = 3 i.e. thrice the actual size.

x = 3

 

Conclusion

After Resizing you will found that the decrease in image size will not compromising with the uniqueness of the content present in it.

Also you can try more Interpolations INTER_LINEAR or INTER_CUBIC and many more.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments