Python OpenCV – Bicubic Interpolation for Resizing Image
Image resizing is a crucial concept that wishes to augment or reduce the number of pixels in a picture. Applications of image resizing can occur under a wider form of scenarios: transliteration of the image, correcting for lens distortion, changing perspective, and rotating a picture. The results of resizing greatly vary looking on the kind of interpolation algorithm used.
Note: While applying interpolation algorithms, some information is certain to be lost as these are approximation algorithms.
What is Interpolation?
Interpolation works by using known data to estimate values at unknown points. For example: if you wanted to understand the pixel intensity of a picture at a selected location within the grid (say coordinate (x, y), but only (x-1,y-1) and (x+1,y+1) are known, you’ll estimate the value at (x, y) using linear interpolation. The greater the quantity of already known values, the higher would be the accuracy of the estimated pixel value.
Different interpolation algorithms include the nearest neighbor, bilinear, bicubic, and others. Betting on their complexity, these use anywhere from 0 to 256 (or more) adjacent pixels when interpolating. The accuracy of those algorithms is increased significantly by increasing the number of neighboring pixels considered while evaluation of the new pixel value. Interpolation algorithms are predominantly used for resizing and distorting a high-resolution image to an occasional resolution image. There are various interpolation algorithms one of them is Bicubic Interpolation.
In addition to going 2×2 neighborhood of known pixel values, Bicubic goes one step beyond bilinear by considering the closest 4×4 neighborhood of known pixels — for a complete of 16 pixels. The pixels that are closer to the one that’s to be estimated are given higher weights as compared to those that are further away. Therefore, the farthest pixels have the smallest amount of weight. The results of Bicubic interpolation are far better as compared to NN or bilinear algorithms. This can be because a greater number of known pixel values are considered while estimating the desired value. Thus, making it one of all the foremost standard interpolation methods.
Implementing Bicubic Interpolation with Python
Importing the necessary modules: We import all dependencies like cv2 (OpenCV), NumPy, and math.
Writing the Interpolation Kernel Function for Bicubic Interpolation: The interpolation kernel for bicubic is of the form:
Here the value of coefficient a determines the performance of the kernel and it lies mostly between -0.5 to -0.75 for optimum performance.
Adding padding to the input image: Define padding function to add borders to your image. OpenCV has various padding functions. When interpolations require padding the source, the boundary of the source image needs to be extended because it needs to have information such that it can compute the pixel values of all destination pixels that lie along the boundaries.
Writing the bicubic interpolation function: Define bicubic function and pass the image as an input. (You can vary the scaling factor as x2 or x4 based on the requirement.)
Taking input from the user and passing the input to the bicubic function to generate the resized image: Passing the desired image to the bicubic function and saving the output as a separate file in the directory.
Compare the generated image with the input image: Use the shape() method to compare the height, width, and color mode of both images.
Thus, from the above code, we can see that the input image has been resized using bicubic interpolation technique. The image given below has been compressed for publishing reasons. You can run the above code to see the implementation of increasing the size of the image smoothly using bicubic interpolation. The unknown pixel values here are filled by considering the 16 nearest known values.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course