# How to interpolate an image ?

Is there an opencv python function that returns the pixel intensity at the interpolated location ? I am trying to write my own function for it but I am confused. I know how to interpolate for 1 dimensions but not 2. It does not have to be efficient. It's not a homework. I am just trying to implement the active contours for practice and understanding and part of it requires me to do some interpolation.

def interp(x, x1, x2, fx1, fx2):
return (x - x1)/(x2 - x1)*(fx2 - fx1) + fx1 edit retag close merge delete

Sort by » oldest newest most voted

I'm not a python expert but I think that's a good answer to your question :

f(0,0)=1, f(1,0)=3 f(0,1)=5, f(1,1)=4

I want to solve f(i,j)=z = M a where p=[ a0 a1 a2] and a0 xi+ a1yi +a2 =zi

import numpy as np
from numpy.linalg import inv
z = np.array([, , , ])
m=np.array([[0, 0 ,1],[1,0 ,1],[0 ,1 ,1],[1 ,1 ,1]]);
print ("z = ",z)
print ("m = ",m)
mm=np.dot(m.T,m)
pinvm = inv(mm)
a= np.dot(pinvm,np.dot(m.T,z))
print ("a = ",a )
zz = np.dot(np.array([[0.5, 0.5 ,1]]),a)
print ("z = ",zz )


results interpolate point at 0.5,0.5 is 3.25 :

z =  [


]
m =  [[0 0 1]
[1 0 1]
[0 1 1]
[1 1 1]]
a =  [[0.5 ]
[2.5 ]
[1.75]]
z =  [[3.25]]


Appuyez sur une touche pour continuer...

this answer is off topics : no opencv function

In c++ it's here with opencv function

more

thank you. And sorry but are which are the interpolating coordinates ? i.e. xi and yi in my original image. Would they be [0.5, 0.5, 1] ? I assume the 1 is just to act as an augmented vector ?

In your figure (x,y) is at(0,0) and (x+1,y+1) at(1,1)... You can give (dx,dy) dx and dy ranging from 0 to 1 and it will give you f(dx,dy) you can give dx outside of [0,1] but it is not interpolate but extrapolate!

okay thank you. I can copy paste that into a function. Does this line of code zz = np.dot(np.array([[0.5, 0.5 ,1]]),a) set dx and dy ?

Are u using the unit square formulation from wikipedia ? https://en.wikipedia.org/wiki/Bilinea... I am not sure I understand your code

it is https://en.wikipedia.org/w/index.php?... with a3=0 (you ask bilinear but you can try with quadratic if you have 6 points). Method is least square (regression)

Yes zz = np.dot(np.array([[0.5, 0.5 ,1]]),a) set dx and dy : zz = np.dot(np.array([[dx, dy ,1]]),a)

Official site

GitHub

Wiki

Documentation

## Stats

Asked: 2018-04-06 08:18:20 -0500

Seen: 643 times

Last updated: Apr 06 '18