<s> import unittest <EOL> import os . path <EOL> import cv <EOL> import numpy as np <EOL> import numpy . linalg as la <EOL> import pyvision as pv <EOL> def logPolar ( im , center = None , radius = None , M = None , size = ( <NUM_LIT:64> , <NUM_LIT> ) ) : <EOL> '''<STR_LIT>''' <EOL> w , h = im . size <EOL> if radius == None : <EOL> radius = <NUM_LIT:0.5> * min ( w , h ) <EOL> if M == None : <EOL> M = size [ <NUM_LIT:0> ] / np . log ( radius ) <EOL> if center == None : <EOL> center = pv . Point ( <NUM_LIT:0.5> * w , <NUM_LIT:0.5> * h ) <EOL> src = im . asOpenCV ( ) <EOL> dst = cv . cvCreateImage ( cv . cvSize ( size [ <NUM_LIT:0> ] , size [ <NUM_LIT:1> ] ) , <NUM_LIT:8> , <NUM_LIT:3> ) <EOL> cv . cvLogPolar ( src , dst , center . asOpenCV ( ) , M , cv . CV_INTER_LINEAR + cv . CV_WARP_FILL_OUTLIERS ) <EOL> return pv . Image ( dst ) <EOL> def PerspectiveFromPointsOld ( source , dest , new_size ) : <EOL> '''<STR_LIT>''' <EOL> assert len ( source ) == len ( dest ) <EOL> src_nrm = pv . AffineNormalizePoints ( source ) <EOL> source = src_nrm . transformPoints ( source ) <EOL> dst_nrm = pv . AffineNormalizePoints ( dest ) <EOL> dest = dst_nrm . transformPoints ( dest ) <EOL> A = [ ] <EOL> for i in range ( len ( source ) ) : <EOL> src = source [ i ] <EOL> dst = dest [ i ] <EOL> row1 = [ <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> , - dst . w * src . x , - dst . w * src . y , - dst . w * src . w , dst . y * src . x , dst . y * src . y , dst . y * src . w ] <EOL> row2 = [ dst . w * src . x , dst . w * src . y , dst . w * src . w , <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> , - dst . x * src . x , - dst . x * src . y , - dst . x * src . w ] <EOL> A . append ( row1 ) <EOL> A . append ( row2 ) <EOL> A = np . array ( A ) <EOL> U , D , Vt = la . svd ( A ) <EOL> H = Vt [ <NUM_LIT:8> , : ] . reshape ( <NUM_LIT:3> , <NUM_LIT:3> ) <EOL> matrix = np . dot ( dst_nrm . inverse , np . dot ( H , src_nrm . matrix ) ) <EOL> return PerspectiveTransform ( matrix , new_size ) <EOL> def PerspectiveFromPoints ( source , dest , new_size , method = <NUM_LIT:0> , ransacReprojThreshold = <NUM_LIT> ) : <EOL> '''<STR_LIT>''' <EOL> assert len ( source ) == len ( dest ) <EOL> n_points = len ( source ) <EOL> s = cv . CreateMat ( n_points , <NUM_LIT:2> , cv . CV_32F ) <EOL> d = cv . CreateMat ( n_points , <NUM_LIT:2> , cv . CV_32F ) <EOL> p = cv . CreateMat ( <NUM_LIT:3> , <NUM_LIT:3> , cv . CV_32F ) <EOL> for i in range ( n_points ) : <EOL> s [ i , <NUM_LIT:0> ] = source [ i ] . X ( ) <EOL> s [ i , <NUM_LIT:1> ] = source [ i ] . Y ( ) <EOL> d [ i , <NUM_LIT:0> ] = dest [ i ] . X ( ) <EOL> d [ i , <NUM_LIT:1> ] = dest [ i ] . Y ( ) <EOL> results = cv . FindHomography ( s , d , p , method , ransacReprojThreshold ) <EOL> matrix = pv . OpenCVToNumpy ( p ) <EOL> return PerspectiveTransform ( matrix , new_size ) <EOL> class PerspectiveTransform : <EOL> def __init__ ( self , matrix , new_size , interpolate = None ) : <EOL> self . matrix = matrix <EOL> self . inverse = la . inv ( matrix ) <EOL> self . size = new_size <EOL> self . interpolate = interpolate <EOL> def __call__ ( self , data ) : <EOL> '''<STR_LIT>''' <EOL> if isinstance ( data , pv . Image ) : <EOL> return self . transformImage ( data ) <EOL> elif isinstance ( data , pv . Point ) : <EOL> return self . transformPoint ( data ) <EOL> else : <EOL> return self . transformPoints ( data ) <EOL> def transformImage ( self , im ) : <EOL> '''<STR_LIT>''' <EOL> if isinstance ( self . matrix , cv . cvmat ) : <EOL> matrix = self . matrix <EOL> else : <EOL> matrix = pv . NumpyToOpenCV ( self . matrix ) <EOL> src = im . asOpenCV ( ) <EOL> dst = cv . CreateImage ( ( self . size [ <NUM_LIT:0> ] , self . size [ <NUM_LIT:1> ] ) , cv . IPL_DEPTH_8U , src . nChannels ) ; <EOL> cv . WarpPerspective ( src , dst , matrix ) <EOL> return pv . Image ( dst ) <EOL> def transformPoint ( self , pt ) : <EOL> '''<STR_LIT>''' <EOL> vec = np . dot ( self . matrix , pt . asVector2H ( ) ) <EOL> return pv . Point ( x = vec [ <NUM_LIT:0> , <NUM_LIT:0> ] , y = vec [ <NUM_LIT:1> , <NUM_LIT:0> ] , w = vec [ <NUM_LIT:2> , <NUM_LIT:0> ] ) <EOL> def transformPoints ( self , pts ) : <EOL> '''<STR_LIT>''' <EOL> if len ( pts ) == <NUM_LIT:0> : <EOL> return [ ] <EOL> elif isinstance ( pts [ <NUM_LIT:0> ] , pv . Point ) : <EOL> return [ self . transformPoint ( pt ) for pt in pts ] <EOL> else : <EOL> pts = np . array ( pts . T , dtype = np . float64 ) <EOL> r , c = pts . shape <EOL> pad = np . ones ( ( <NUM_LIT:1> , c ) , dtype = np . float64 ) <EOL> pts = np . concatenate ( ( pts , pad ) , axis = <NUM_LIT:0> ) <EOL> pts = np . dot ( self . matrix , pts ) <EOL> pts /= pts [ <NUM_LIT:2> : <NUM_LIT:3> , : ] <EOL> return pts [ : <NUM_LIT:2> , : ] . T <EOL> def invertPoint ( self , pt ) : <EOL> '''<STR_LIT>''' <EOL> vec = np . dot ( self . inverse , pt . asVector2H ( ) ) <EOL> return pv . Point ( x = vec [ <NUM_LIT:0> , <NUM_LIT:0> ] , y = vec [ <NUM_LIT:1> , <NUM_LIT:0> ] , w = vec [ <NUM_LIT:2> , <NUM_LIT:0> ] ) <EOL> def invertPoints ( self , pts ) : <EOL> '''<STR_LIT>''' <EOL> return [ self . invertPoint ( pt ) for pt in pts ] <EOL> def asMatrix ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . matrix <EOL> def __mul__ ( self , affine ) : <EOL> return PerspectiveTransform ( np . dot ( self . matrix , affine . matrix ) , self . size , self . interpolate ) <EOL> class _PerspectiveTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> fname_a = os . path . join ( pv . __path__ [ <NUM_LIT:0> ] , '<STR_LIT:data>' , '<STR_LIT:test>' , '<STR_LIT>' ) <EOL> fname_b = os . path . join ( pv . __path__ [ <NUM_LIT:0> ] , '<STR_LIT:data>' , '<STR_LIT:test>' , '<STR_LIT>' ) <EOL> self . im_a = pv . Image ( fname_a ) <EOL> self . im_b = pv . Image ( fname_b ) <EOL> self . corners_a = ( pv . Point ( <NUM_LIT> , <NUM_LIT> ) , pv . Point ( <NUM_LIT> , <NUM_LIT> ) , pv . Point ( <NUM_LIT> , <NUM_LIT> ) , pv . Point ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> self . corners_b = ( pv . Point ( <NUM_LIT> , <NUM_LIT> ) , pv . Point ( <NUM_LIT> , <NUM_LIT> ) , pv . Point ( <NUM_LIT> , <NUM_LIT> ) , pv . Point ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> self . corners_t = ( pv . Point ( <NUM_LIT:0> , <NUM_LIT:0> ) , pv . Point ( <NUM_LIT> , <NUM_LIT:0> ) , pv . Point ( <NUM_LIT> , <NUM_LIT> ) , pv . Point ( <NUM_LIT:0> , <NUM_LIT> ) ) <EOL> for pt in self . corners_a : <EOL> self . im_a . annotatePoint ( pt ) <EOL> def test_four_points_a ( self ) : <EOL> p = PerspectiveFromPoints ( self . corners_a , self . corners_t , ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> _ = p . transformPoints ( self . corners_a ) <EOL> _ = p . transformImage ( self . im_a ) <EOL> def test_four_points_b ( self ) : <EOL> p = PerspectiveFromPoints ( self . corners_b , self . corners_t , ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> _ = p . transformPoints ( self . corners_b ) <EOL> _ = p . transformImage ( self . im_b ) <EOL> def test_four_points_ab ( self ) : <EOL> p = PerspectiveFromPoints ( self . corners_a , self . corners_b , ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> _ = p . transformImage ( self . im_a ) </s>
