Ask Your Question
1

How to interpolate an image ?

asked 2018-04-06 08:18:20 -0600

Nbb gravatar image

updated 2018-04-06 08:31:39 -0600

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

image description

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-04-06 10:04:02 -0600

LBerger gravatar image

updated 2018-04-06 13:31:42 -0600

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([[1], [3], [5], [4]])
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 =  [[1]
 [3]
 [5]
 [4]]
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

edit flag offensive delete link more

Comments

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 ?

Nbb gravatar imageNbb ( 2018-04-06 15:28:04 -0600 )edit

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!

LBerger gravatar imageLBerger ( 2018-04-06 15:42:49 -0600 )edit

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 ?

Nbb gravatar imageNbb ( 2018-04-06 16:53:08 -0600 )edit

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

Nbb gravatar imageNbb ( 2018-04-06 17:29:10 -0600 )edit

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)

LBerger gravatar imageLBerger ( 2018-04-07 03:15:20 -0600 )edit

Question Tools

1 follower

Stats

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

Seen: 2,757 times

Last updated: Apr 06 '18