Skip to content

Commit

Permalink
Added pose estimation example. Changed import statements for PCV modu…
Browse files Browse the repository at this point in the history
…les.
  • Loading branch information
jesolem committed Oct 1, 2012
1 parent 506fd04 commit 30501a8
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 7 deletions.
2 changes: 1 addition & 1 deletion PCV/geometry/homography.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def H_from_ransac(fp,tp,model,maxiter=1000,match_theshold=10):
input: fp,tp (3*n arrays) points in hom. coordinates. """

import ransac
from PCV.tools import ransac

# group corresponding points
data = vstack((fp,tp))
Expand Down
2 changes: 1 addition & 1 deletion PCV/geometry/warp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pylab import *
from numpy import *

import homography
from PCV.geometry import homography


def image_in_image(im1,im2,tp):
Expand Down
3 changes: 2 additions & 1 deletion PCV/imagesearch/vocabulary.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from numpy import *
from scipy.cluster.vq import *
import sift

from PCV.localdescriptors import sift


class Vocabulary(object):
Expand Down
5 changes: 3 additions & 2 deletions PCV/localdescriptors/dsift.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from PIL import Image
import os
from numpy import *
import sift
import os

from PCV.localdescriptors import sift


def process_image_dsift(imagename,resultname,size=20,steps=10,force_orientation=False,resize=None):
Expand Down
2 changes: 1 addition & 1 deletion PCV/localdescriptors/sift.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from PIL import Image
import os
from numpy import *
from pylab import *
import os


def process_image(imagename,resultname,params="--edge-thresh 10 --peak-thresh 5"):
Expand Down
2 changes: 1 addition & 1 deletion PCV/tools/graphcut.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pygraph.classes.digraph import digraph
from pygraph.algorithms.minmax import maximum_flow

import bayes
from PCV.tools import bayes

"""
Graph Cut image segmentation using max-flow/min-cut.
Expand Down
124 changes: 124 additions & 0 deletions examples/ar_cube.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
from pylab import *
from numpy import *
from PIL import Image

# If you have PCV installed, these imports should work
from PCV.geometry import homography, camera
from PCV.localdescriptors import sift

"""
This is the augmented reality and pose estimation cube example from section 4.3.
"""

def cube_points(c,wid):
""" Creates a list of points for plotting
a cube with plot. (the first 5 points are
the bottom square, some sides repeated). """
p = []
# bottom
p.append([c[0]-wid,c[1]-wid,c[2]-wid])
p.append([c[0]-wid,c[1]+wid,c[2]-wid])
p.append([c[0]+wid,c[1]+wid,c[2]-wid])
p.append([c[0]+wid,c[1]-wid,c[2]-wid])
p.append([c[0]-wid,c[1]-wid,c[2]-wid]) #same as first to close plot

# top
p.append([c[0]-wid,c[1]-wid,c[2]+wid])
p.append([c[0]-wid,c[1]+wid,c[2]+wid])
p.append([c[0]+wid,c[1]+wid,c[2]+wid])
p.append([c[0]+wid,c[1]-wid,c[2]+wid])
p.append([c[0]-wid,c[1]-wid,c[2]+wid]) #same as first to close plot

# vertical sides
p.append([c[0]-wid,c[1]-wid,c[2]+wid])
p.append([c[0]-wid,c[1]+wid,c[2]+wid])
p.append([c[0]-wid,c[1]+wid,c[2]-wid])
p.append([c[0]+wid,c[1]+wid,c[2]-wid])
p.append([c[0]+wid,c[1]+wid,c[2]+wid])
p.append([c[0]+wid,c[1]-wid,c[2]+wid])
p.append([c[0]+wid,c[1]-wid,c[2]-wid])

return array(p).T


def my_calibration(sz):
"""
Calibration function for the camera (iPhone4) used in this example.
"""
row,col = sz
fx = 2555*col/2592
fy = 2586*row/1936
K = diag([fx,fy,1])
K[0,2] = 0.5*col
K[1,2] = 0.5*row
return K



# compute features
sift.process_image('book_frontal.JPG','im0.sift')
l0,d0 = sift.read_features_from_file('im0.sift')

sift.process_image('book_perspective.JPG','im1.sift')
l1,d1 = sift.read_features_from_file('im1.sift')


# match features and estimate homography
matches = sift.match_twosided(d0,d1)
ndx = matches.nonzero()[0]
fp = homography.make_homog(l0[ndx,:2].T)
ndx2 = [int(matches[i]) for i in ndx]
tp = homography.make_homog(l1[ndx2,:2].T)

model = homography.RansacModel()
H, inliers = homography.H_from_ransac(fp,tp,model)

# camera calibration
K = my_calibration((747,1000))

# 3D points at plane z=0 with sides of length 0.2
box = cube_points([0,0,0.1],0.1)

# project bottom square in first image
cam1 = camera.Camera( hstack((K,dot(K,array([[0],[0],[-1]])) )) )
# first points are the bottom square
box_cam1 = cam1.project(homography.make_homog(box[:,:5]))


# use H to transfer points to the second image
box_trans = homography.normalize(dot(H,box_cam1))

# compute second camera matrix from cam1 and H
cam2 = camera.Camera(dot(H,cam1.P))
A = dot(linalg.inv(K),cam2.P[:,:3])
A = array([A[:,0],A[:,1],cross(A[:,0],A[:,1])]).T
cam2.P[:,:3] = dot(K,A)

# project with the second camera
box_cam2 = cam2.project(homography.make_homog(box))



# plotting
im0 = array(Image.open('book_frontal.JPG'))
im1 = array(Image.open('book_perspective.JPG'))

figure()
imshow(im0)
plot(box_cam1[0,:],box_cam1[1,:],linewidth=3)
title('2D projection of bottom square')
axis('off')

figure()
imshow(im1)
plot(box_trans[0,:],box_trans[1,:],linewidth=3)
title('2D projection transfered with H')
axis('off')

figure()
imshow(im1)
plot(box_cam2[0,:],box_cam2[1,:],linewidth=3)
title('3D points projected in second image')
axis('off')

show()

0 comments on commit 30501a8

Please sign in to comment.