Cara menggunakan python resize array interpolation

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.

Interpolation Algorithms

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.

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.

Python




# Import modules

import cv2

import numpy as np

import math

import sys, time

Writing the Interpolation Kernel Function for Bicubic Interpolation: The interpolation kernel for bicubic is of the form:

Cara menggunakan python resize array interpolation

Kernel equation

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.

Python




# Interpolation kernel

import0 import1

import

import3import4 import5import6import7import8 import9cv20import6cv22import8 cv24cv25

cv26cv27 cv28cv29import0import1import2import5import6import5import2import2import8import1numpy as np0cv28cv29import8import1import2import5import6import5import2import2import0import1cv29cv24

import

import3import7 import5import6import7cv24cv20import6cv22import8 import0cv25

cv26cv27 import0import2import5import6import5import2import2import8import1numpy as np0import5sys, time1import2sys, time3import2import5import6import5import2import2import0import1cv29import5# Interpolation kernel4import2sys, time3import2import6import5numpy as np0import01import2import0

import3cv27 import9

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.

Python




import07

import0 import09

import3import11import8 import13cv29import01import16cv29import01import19

import3import21import0import23cv29import0import26import0import28cv29import0import31import8 import33

import34 

import3import36

import3import21import0import23cv29import0import26import9import45import0import31import8 import49import9import45cv24import31

import3import55cv29import0import23cv29import01import26import0import28cv29import0import66import8 import68numpy as np0cv24import71

import3import21import0import23cv29import0import16cv29import0import28cv29import01import66import8 import86numpy as np0cv24import89

import3import21import9import45import0import26import0import28cv29import0import31import8 cv202import9import45cv24cv206

import34 

import3cv209

import3import21import9import45import0import26import9import45import0import31import8 cv202import9import26import9import31

import3import55cv29import0import23cv29import01import26import9import45import0import31import8 import68numpy as np0cv24import26import9import31

import3import55cv29import0import23cv29import01import16cv29import0import28cv29import01import31import8 import68numpy as np0cv24import16numpy as np0cv24import31

import3import21import9import45import0import16cv29import0import28cv29import01import31import8 cv202import9import16numpy as np0cv24import31

import34 

import3cv27 import11

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

Python




cv290

import0 cv292

cv293 

import3cv295

import3cv297import8 cv299

cv293 

import3import02

import3import04

import3import06

import3import33import8 import10

cv293 

import3import13

import3import15import8 import17import2import19

import3import21import8 import23import2import19

cv293 

import3import28

import3import30import8 import32import8import34

cv293 

import3import37

import3import39

import3import41

import3import43

cv293 

import3import46import8 cv24import49import50

cv293 

import3import53import5import55import1

import3import53import5import60import1

import3import63import8 import9

cv293 

import3import68 import69import70 import71import72

cv26import68 import75import70 import71import78

import79import68 import81import70 import71import84

cv293 

import86import87

import86import89

import86import91import8 import81import2 import46cv29 import0import98import2 import46cv29 import0

cv293 

import86numpy as np05import8 cv24 cv29 numpy as np09numpy as np0 numpy as np11

import86numpy as np13import8 numpy as np09numpy as np0 numpy as np11

import86numpy as np19import8 numpy as np11cv29 cv24 numpy as np0 numpy as np09

import86numpy as np27import8 numpy as np11cv29 import0 numpy as np0 numpy as np09

cv293 

import86numpy as np36import8 cv24 cv29 numpy as np40numpy as np0 numpy as np42

import86numpy as np44import8 numpy as np40numpy as np0 numpy as np42

import86numpy as np50import8 numpy as np42cv29 cv24 numpy as np0 numpy as np40

import86numpy as np58import8 numpy as np42cv29 import0 numpy as np0 numpy as np40

cv293 

import86numpy as np67

import86numpy as np69import8 numpy as np71

import86numpy as np73import8 numpy as np75numpy as np76numpy as np77numpy as np0numpy as np79numpy as np76numpy as np81numpy as np0numpy as np83

numpy as np84cv202numpy as np76numpy as np77numpy as np0numpy as np89numpy as np76numpy as np81numpy as np0numpy as np83

numpy as np84cv202numpy as np76numpy as np77cv29numpy as np99numpy as np76numpy as np81numpy as np0numpy as np83

numpy as np84cv202numpy as np76numpy as np77cv29import09numpy as np76numpy as np81numpy as np0import13

import14import15numpy as np76numpy as np77numpy as np0numpy as np79numpy as np76numpy as np81numpy as np0import23

numpy as np84cv202numpy as np76numpy as np77numpy as np0numpy as np89numpy as np76numpy as np81numpy as np0import23

numpy as np84cv202numpy as np76numpy as np77cv29numpy as np99numpy as np76numpy as np81numpy as np0import23

numpy as np84cv202numpy as np76numpy as np77cv29import09numpy as np76numpy as np81numpy as np0import53

import14import15numpy as np76numpy as np77numpy as np0numpy as np79numpy as np76numpy as np81cv29import63

numpy as np84cv202numpy as np76numpy as np77numpy as np0numpy as np89numpy as np76numpy as np81cv29import63

numpy as np84cv202numpy as np76numpy as np77cv29numpy as np99numpy as np76numpy as np81cv29import63

numpy as np84cv202numpy as np76numpy as np77cv29import09numpy as np76numpy as np81cv29import93

import14import15numpy as np76numpy as np77numpy as np0numpy as np79numpy as np76numpy as np81cv29math03

numpy as np84cv202numpy as np76numpy as np77numpy as np0numpy as np89numpy as np76numpy as np81cv29math03

numpy as np84cv202numpy as np76numpy as np77cv29numpy as np99numpy as np76numpy as np81cv29math03

numpy as np84cv202numpy as np76numpy as np77cv29import09numpy as np76numpy as np81cv29math33

import86math35import8 math37

math38math39

math40 

import86math42

import86math44

import86math46import8 math48

cv293 

import3math51

import3math53

import3math55math56import1

import34 

import3math60

import3math62

import3cv27 import30

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.

Python3




math66

math67

math68

math69

math70

import33import8 math73math74import1

cv293 

math77

import50import8 import0

cv293 

math82

import0import8 numpy as np0cv24import49import0

cv293 

math90

math91

import30import8 math94

import53import5math97import1

cv293 

import00

import01import02import03

import04import8math73import02import1

Compare the generated image with the input image: Use the shape() method to compare the height, width, and color mode of both images.

Python3




import09

import53import5import12import13

import53import5import16import17

Complete Code:

Input Image:

Cara menggunakan python resize array interpolation

gfg.png

Python3




import18

import cv2

import numpy as np

import math

import import26

import import28

cv293 

cv293 

# Interpolation kernel

import0 import1

import3import4 import5import6import7import8 import9cv20import6cv22import8 cv24cv25

cv26cv27 cv28cv29import0import1import2import5import6import5import2import2import8import1numpy as np0cv28cv29import8import1import2import5import6import5import2import2import0import1cv29cv24

import3import7 import5import6import7cv24cv20import6cv22import8 import0cv25

cv26cv27 import0import2import5import6import5import2import2import8import1numpy as np0import5sys, time1import2sys, time3import2import5import6import5import2import2import0import1cv29import5# Interpolation kernel4import2sys, time3import2import6import5numpy as np0import01import2import0

import3cv27 import9

cv293 

cv293 

import07

import0 import09

import3import11import8 import13cv29import01import16cv29import01import19

import3import21import0import23cv29import0import26import0import28cv29import0import31import8 import33

import34 

import3import36

import3import21import0import23cv29import0import26import9import45import0import31import8 import49import9import45cv24import31

import3import55cv29import0import23cv29import01import26import0import28cv29import0import66import8 import68numpy as np0cv24import71

import3import21import0import23cv29import0import16cv29import0import28cv29import01import66import8 import86numpy as np0cv24import89

import3import21import9import45import0import26import0import28cv29import0import31import8 cv202import9import45cv24cv206

import34 

import3cv209

import3import21import9import45import0import26import9import45import0import31import8 cv202import9import26import9import31

import3import55cv29import0import23cv29import01import26import9import45import0import31import8 import68numpy as np0cv24import26import9import31

import3import55cv29import0import23cv29import01import16cv29import0import28cv29import01import31import8 import68numpy as np0cv24import16numpy as np0cv24import31

import3import21import9import45import0import16cv29import0import28cv29import01import31import8 cv202import9import16numpy as np0cv24import31

import3cv27 import11

cv293 

cv293 

cv290

import0 cv292

import

import3cv295

import3cv297import8 cv299

import34 

import3import02

import3import027

import3import06

import3import33import8 import10

import34 

import3import13

import3import15import8 import17import2import19

import3import21import8 import23import2import19

cv293 

import3import28

import3import30import8 import32import8import057

import3import059

import3import39

import3import063

import3import43

cv293 

import3import46import8 cv24import49import50

cv293 

import3import53import5import55import1

import3import53import5import60import1

import3import63import8 import9

import34 

import3import68 import69import70 import71import72

cv26import68 import75import70 import71import78

import79import68 import81import70 import71import84

import107 

import86import87

import86import89

import86import91import8 import81import2 import46cv29 import0import98import2 import46cv29 import0

cv293 

import86numpy as np05import8 cv24 cv29 numpy as np09numpy as np0 numpy as np11

import86numpy as np13import8 numpy as np09numpy as np0 numpy as np11

import86numpy as np19import8 numpy as np11cv29 cv24 numpy as np0 numpy as np09

import86numpy as np27import8 numpy as np11cv29 import0 numpy as np0 numpy as np09

cv293 

import86numpy as np36import8 cv24 cv29 numpy as np40numpy as np0 numpy as np42

import86numpy as np44import8 numpy as np40numpy as np0 numpy as np42

import86numpy as np50import8 numpy as np42cv29 cv24 numpy as np0 numpy as np40

import86numpy as np58import8 numpy as np42cv29 import0 numpy as np0 numpy as np40

math40 

import86numpy as np67

import86numpy as np69import8 numpy as np71

import86numpy as np73import8 numpy as np75numpy as np76numpy as np77numpy as np0numpy as np79numpy as np76numpy as np81numpy as np0numpy as np83

numpy as np84cv202numpy as np76numpy as np77numpy as np0numpy as np89numpy as np76numpy as np81numpy as np0numpy as np83

numpy as np84cv202numpy as np76numpy as np77cv29numpy as np99numpy as np76numpy as np81numpy as np0numpy as np83

numpy as np84cv202numpy as np76numpy as np77cv29import09numpy as np76numpy as np81numpy as np0import13

import14import15numpy as np76numpy as np77numpy as np0numpy as np79numpy as np76numpy as np81numpy as np0import23

numpy as np84cv202numpy as np76numpy as np77numpy as np0numpy as np89numpy as np76numpy as np81numpy as np0import23

numpy as np84cv202numpy as np76numpy as np77cv29numpy as np99numpy as np76numpy as np81numpy as np0import23

numpy as np84cv202numpy as np76numpy as np77cv29import09numpy as np76numpy as np81numpy as np0import53

import14import15numpy as np76numpy as np77numpy as np0numpy as np79numpy as np76numpy as np81cv29import63

numpy as np84cv202numpy as np76numpy as np77numpy as np0numpy as np89numpy as np76numpy as np81cv29import63

numpy as np84cv202numpy as np76numpy as np77cv29numpy as np99numpy as np76numpy as np81cv29import63

numpy as np84cv202numpy as np76numpy as np77cv29import09numpy as np76numpy as np81cv29import93

import14import15numpy as np76numpy as np77numpy as np0numpy as np79numpy as np76numpy as np81cv29math03

numpy as np84cv202numpy as np76numpy as np77numpy as np0numpy as np89numpy as np76numpy as np81cv29math03

numpy as np84cv202numpy as np76numpy as np77cv29numpy as np99numpy as np76numpy as np81cv29math03

numpy as np84cv202numpy as np76numpy as np77cv29import09numpy as np76numpy as np81cv29math33

import86math35import8 math37

math38math39

math40 

import86import364

import86import366

import86math46import8 math48

cv293 

import3math51

import3math53

import3math55math56import1

import34 

import3math60

import3math62

import3cv27 import30

cv293 

cv293 

math66

import391

import392

import393

math70

import33import8 math73math74import1

cv293 

math77

import50import8 import0

math82

import0import8 numpy as np0cv24import49import0

cv293 

math90

math91

import30import8 import417

import53import5math97import1

cv293 

import00

import01import02import426

import04import8 math73import02import1

cv293 

import09

import53import5import12import437

import53import5import16import441

Output:

Cara menggunakan python resize array interpolation

Output Image:

Cara menggunakan python resize array interpolation

bicubic.png

Explanation: 

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.