<s> '''<STR_LIT>''' <EOL> import numpy as np <EOL> import cv2 <EOL> from common import anorm , getsize <EOL> FLANN_INDEX_KDTREE = <NUM_LIT:1> <EOL> FLANN_INDEX_LSH = <NUM_LIT:6> <EOL> def init_feature ( name ) : <EOL> chunks = name . split ( '<STR_LIT:->' ) <EOL> if chunks [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> detector = cv2 . SIFT ( ) <EOL> norm = cv2 . NORM_L2 <EOL> elif chunks [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> detector = cv2 . SURF ( <NUM_LIT> ) <EOL> norm = cv2 . NORM_L2 <EOL> elif chunks [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> detector = cv2 . ORB ( <NUM_LIT> ) <EOL> norm = cv2 . NORM_HAMMING <EOL> else : <EOL> return None , None <EOL> if '<STR_LIT>' in chunks : <EOL> if norm == cv2 . NORM_L2 : <EOL> flann_params = dict ( algorithm = FLANN_INDEX_KDTREE , trees = <NUM_LIT:5> ) <EOL> else : <EOL> flann_params = dict ( algorithm = FLANN_INDEX_LSH , <EOL> table_number = <NUM_LIT:6> , <EOL> key_size = <NUM_LIT:12> , <EOL> multi_probe_level = <NUM_LIT:1> ) <EOL> matcher = cv2 . FlannBasedMatcher ( flann_params , { } ) <EOL> else : <EOL> matcher = cv2 . BFMatcher ( norm ) <EOL> return detector , matcher <EOL> def filter_matches ( kp1 , kp2 , matches , ratio = <NUM_LIT> ) : <EOL> mkp1 , mkp2 = [ ] , [ ] <EOL> for m in matches : <EOL> if len ( m ) == <NUM_LIT:2> and m [ <NUM_LIT:0> ] . distance < m [ <NUM_LIT:1> ] . distance * ratio : <EOL> m = m [ <NUM_LIT:0> ] <EOL> mkp1 . append ( kp1 [ m . queryIdx ] ) <EOL> mkp2 . append ( kp2 [ m . trainIdx ] ) <EOL> p1 = np . float32 ( [ kp . pt for kp in mkp1 ] ) <EOL> p2 = np . float32 ( [ kp . pt for kp in mkp2 ] ) <EOL> kp_pairs = zip ( mkp1 , mkp2 ) <EOL> return p1 , p2 , kp_pairs <EOL> def explore_match ( win , img1 , img2 , kp_pairs , status = None , H = None ) : <EOL> h1 , w1 = img1 . shape [ : <NUM_LIT:2> ] <EOL> h2 , w2 = img2 . shape [ : <NUM_LIT:2> ] <EOL> vis = np . zeros ( ( max ( h1 , h2 ) , w1 + w2 ) , np . uint8 ) <EOL> vis [ : h1 , : w1 ] = img1 <EOL> vis [ : h2 , w1 : w1 + w2 ] = img2 <EOL> vis = cv2 . cvtColor ( vis , cv2 . COLOR_GRAY2BGR ) <EOL> if H is not None : <EOL> corners = np . float32 ( [ [ <NUM_LIT:0> , <NUM_LIT:0> ] , [ w1 , <NUM_LIT:0> ] , [ w1 , h1 ] , [ <NUM_LIT:0> , h1 ] ] ) <EOL> corners = np . int32 ( cv2 . perspectiveTransform ( corners . reshape ( <NUM_LIT:1> , - <NUM_LIT:1> , <NUM_LIT:2> ) , H ) . reshape ( - <NUM_LIT:1> , <NUM_LIT:2> ) + ( w1 , <NUM_LIT:0> ) ) <EOL> cv2 . polylines ( vis , [ corners ] , True , ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) ) <EOL> if status is None : <EOL> status = np . ones ( len ( kp_pairs ) , np . bool_ ) <EOL> p1 = np . int32 ( [ kpp [ <NUM_LIT:0> ] . pt for kpp in kp_pairs ] ) <EOL> p2 = np . int32 ( [ kpp [ <NUM_LIT:1> ] . pt for kpp in kp_pairs ] ) + ( w1 , <NUM_LIT:0> ) <EOL> green = ( <NUM_LIT:0> , <NUM_LIT:255> , <NUM_LIT:0> ) <EOL> red = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:255> ) <EOL> white = ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) <EOL> kp_color = ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) <EOL> for ( x1 , y1 ) , ( x2 , y2 ) , inlier in zip ( p1 , p2 , status ) : <EOL> if inlier : <EOL> col = green <EOL> cv2 . circle ( vis , ( x1 , y1 ) , <NUM_LIT:2> , col , - <NUM_LIT:1> ) <EOL> cv2 . circle ( vis , ( x2 , y2 ) , <NUM_LIT:2> , col , - <NUM_LIT:1> ) <EOL> else : <EOL> col = red <EOL> r = <NUM_LIT:2> <EOL> thickness = <NUM_LIT:3> <EOL> cv2 . line ( vis , ( x1 - r , y1 - r ) , ( x1 + r , y1 + r ) , col , thickness ) <EOL> cv2 . line ( vis , ( x1 - r , y1 + r ) , ( x1 + r , y1 - r ) , col , thickness ) <EOL> cv2 . line ( vis , ( x2 - r , y2 - r ) , ( x2 + r , y2 + r ) , col , thickness ) <EOL> cv2 . line ( vis , ( x2 - r , y2 + r ) , ( x2 + r , y2 - r ) , col , thickness ) <EOL> vis0 = vis . copy ( ) <EOL> for ( x1 , y1 ) , ( x2 , y2 ) , inlier in zip ( p1 , p2 , status ) : <EOL> if inlier : <EOL> cv2 . line ( vis , ( x1 , y1 ) , ( x2 , y2 ) , green ) <EOL> cv2 . imshow ( win , vis ) <EOL> def onmouse ( event , x , y , flags , param ) : <EOL> cur_vis = vis <EOL> if flags & cv2 . EVENT_FLAG_LBUTTON : <EOL> cur_vis = vis0 . copy ( ) <EOL> r = <NUM_LIT:8> <EOL> m = ( anorm ( p1 - ( x , y ) ) < r ) | ( anorm ( p2 - ( x , y ) ) < r ) <EOL> idxs = np . where ( m ) [ <NUM_LIT:0> ] <EOL> kp1s , kp2s = [ ] , [ ] <EOL> for i in idxs : <EOL> ( x1 , y1 ) , ( x2 , y2 ) = p1 [ i ] , p2 [ i ] <EOL> col = ( red , green ) [ status [ i ] ] <EOL> cv2 . line ( cur_vis , ( x1 , y1 ) , ( x2 , y2 ) , col ) <EOL> kp1 , kp2 = kp_pairs [ i ] <EOL> kp1s . append ( kp1 ) <EOL> kp2s . append ( kp2 ) <EOL> cur_vis = cv2 . drawKeypoints ( cur_vis , kp1s , flags = <NUM_LIT:4> , color = kp_color ) <EOL> cur_vis [ : , w1 : ] = cv2 . drawKeypoints ( cur_vis [ : , w1 : ] , kp2s , flags = <NUM_LIT:4> , color = kp_color ) <EOL> cv2 . imshow ( win , cur_vis ) <EOL> cv2 . setMouseCallback ( win , onmouse ) <EOL> return vis <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> print __doc__ <EOL> import sys , getopt <EOL> opts , args = getopt . getopt ( sys . argv [ <NUM_LIT:1> : ] , '<STR_LIT>' , [ '<STR_LIT>' ] ) <EOL> opts = dict ( opts ) <EOL> feature_name = opts . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> try : fn1 , fn2 = args <EOL> except : <EOL> fn1 = '<STR_LIT>' <EOL> fn2 = '<STR_LIT>' <EOL> img1 = cv2 . imread ( fn1 , <NUM_LIT:0> ) <EOL> img2 = cv2 . imread ( fn2 , <NUM_LIT:0> ) <EOL> detector , matcher = init_feature ( feature_name ) <EOL> if detector != None : <EOL> print '<STR_LIT>' , feature_name <EOL> else : <EOL> print '<STR_LIT>' , feature_name <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> kp1 , desc1 = detector . detectAndCompute ( img1 , None ) <EOL> kp2 , desc2 = detector . detectAndCompute ( img2 , None ) <EOL> print '<STR_LIT>' % ( len ( kp1 ) , len ( kp2 ) ) <EOL> def match_and_draw ( win ) : <EOL> print '<STR_LIT>' <EOL> raw_matches = matcher . knnMatch ( desc1 , trainDescriptors = desc2 , k = <NUM_LIT:2> ) <EOL> p1 , p2 , kp_pairs = filter_matches ( kp1 , kp2 , raw_matches ) <EOL> if len ( p1 ) >= <NUM_LIT:4> : <EOL> H , status = cv2 . findHomography ( p1 , p2 , cv2 . RANSAC , <NUM_LIT> ) <EOL> print '<STR_LIT>' % ( np . sum ( status ) , len ( status ) ) <EOL> else : <EOL> H , status = None , None <EOL> print '<STR_LIT>' % len ( p1 ) <EOL> vis = explore_match ( win , img1 , img2 , kp_pairs , status , H ) <EOL> match_and_draw ( '<STR_LIT>' ) <EOL> cv2 . waitKey ( ) <EOL> cv2 . destroyAllWindows ( ) </s>
