<s> import numpy as np <EOL> import cv2 <EOL> def draw_match ( img1 , kp1 , <EOL> img2 , kp2 , <EOL> matches , <EOL> output = None , <EOL> matchColor = ( <NUM_LIT:255> , <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> matchesMask = None , <EOL> * args , ** kargs ) : <EOL> h1 = img1 . shape [ <NUM_LIT:0> ] <EOL> w1 = img1 . shape [ <NUM_LIT:1> ] <EOL> h2 = img2 . shape [ <NUM_LIT:0> ] <EOL> w2 = img2 . shape [ <NUM_LIT:1> ] <EOL> w = w1 + w2 <EOL> h = max ( h1 , h2 ) <EOL> outimg = np . full ( ( h , w , <NUM_LIT:3> ) , <NUM_LIT:0> , np . uint8 ) <EOL> img1_ = img1 . copy ( ) <EOL> img2_ = img2 . copy ( ) <EOL> if len ( img1 . shape ) == <NUM_LIT:2> or img1 . shape [ <NUM_LIT:2> ] == <NUM_LIT:1> : <EOL> img1_ = cv2 . cvtColor ( img1_ , cv2 . COLOR_GRAY2BGR ) <EOL> if len ( img2 . shape ) == <NUM_LIT:2> or img2 . shape [ <NUM_LIT:2> ] == <NUM_LIT:1> : <EOL> img2_ = cv2 . cvtColor ( img2_ , cv2 . COLOR_GRAY2BGR ) <EOL> outimg [ <NUM_LIT:0> : h1 , <NUM_LIT:0> : w1 , : ] = img1_ <EOL> outimg [ <NUM_LIT:0> : h2 , w1 : w1 + w2 , : ] = img2_ <EOL> for i , m in enumerate ( matches ) : <EOL> if matchesMask is not None and matchesMask [ i ] == <NUM_LIT:0> : continue <EOL> i1 , i2 = m . queryIdx , m . trainIdx <EOL> pt1 , pt2 = kp1 [ i1 ] . pt , kp2 [ i2 ] . pt <EOL> pt1 = ( int ( pt1 [ <NUM_LIT:0> ] ) , int ( pt1 [ <NUM_LIT:1> ] ) ) <EOL> pt2 = ( int ( w1 + pt2 [ <NUM_LIT:0> ] ) , int ( pt2 [ <NUM_LIT:1> ] ) ) <EOL> cv2 . line ( outimg , pt1 , pt2 , matchColor ) <EOL> if output is not None : <EOL> output = outimg . copy ( ) <EOL> return outimg <EOL> def test_feature_matching_realtime ( detetor = cv2 . ORB ( ) ) : <EOL> from cam import MyCam <EOL> """<STR_LIT>""" <EOL> MIN_MATCH_COUNT = <NUM_LIT:10> <EOL> cam = MyCam ( ) <EOL> cam . size = ( <NUM_LIT> , <NUM_LIT> ) <EOL> img1 = img1 = cv2 . imread ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> cv2 . imshow ( '<STR_LIT:source>' , img1 ) <EOL> while True : <EOL> img2 = cv2 . flip ( cv2 . cvtColor ( cam . read ( ) , cv2 . COLOR_BGR2GRAY ) , <NUM_LIT:1> ) <EOL> k = cv2 . waitKey ( <NUM_LIT:5> ) <EOL> if k == ord ( '<STR_LIT:s>' ) : <EOL> img1 = img2 . copy ( ) <EOL> cv2 . imwrite ( '<STR_LIT>' , img1 ) <EOL> elif k == <NUM_LIT> : <EOL> break <EOL> if k is not None : <EOL> cv2 . destroyWindow ( '<STR_LIT>' ) <EOL> kp1 , des1 = detetor . detectAndCompute ( img1 , None ) <EOL> kp2 , des2 = detetor . detectAndCompute ( img2 , None ) <EOL> if des2 is None : <EOL> img3 = img3 = draw_match ( img1 , kp1 , img2 , kp2 , [ ] ) <EOL> continue <EOL> des1 = des1 . astype ( np . uint8 , copy = False ) <EOL> des2 = des2 . astype ( np . uint8 , copy = False ) <EOL> bf = cv2 . BFMatcher ( cv2 . NORM_HAMMING ) <EOL> matches = bf . knnMatch ( des1 , des2 , k = <NUM_LIT:2> ) <EOL> good = [ ] <EOL> try : <EOL> for m , n in matches : <EOL> if m . distance < <NUM_LIT> * n . distance : <EOL> good . append ( m ) <EOL> except ValueError : <EOL> good = [ ] <EOL> if len ( good ) > MIN_MATCH_COUNT : <EOL> src_pts = np . float32 ( [ kp1 [ m . queryIdx ] . pt for m in good ] ) . reshape ( - <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> dst_pts = np . float32 ( [ kp2 [ m . trainIdx ] . pt for m in good ] ) . reshape ( - <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> M , mask = cv2 . findHomography ( src_pts , dst_pts , cv2 . RANSAC , <NUM_LIT> ) <EOL> matchesMask = mask . ravel ( ) . tolist ( ) <EOL> h , w = img1 . shape <EOL> pts = np . float32 ( [ [ <NUM_LIT:0> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , h - <NUM_LIT:1> ] , [ w - <NUM_LIT:1> , h - <NUM_LIT:1> ] , [ w - <NUM_LIT:1> , <NUM_LIT:0> ] ] ) . reshape ( - <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> dst = cv2 . perspectiveTransform ( pts , M ) <EOL> cv2 . polylines ( img2 , [ np . int32 ( dst ) ] , True , ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:255> ) , <NUM_LIT:3> ) <EOL> else : <EOL> matchesMask = None <EOL> good = [ ] <EOL> img3 = draw_match ( img1 , kp1 , img2 , kp2 , good , matchesMask = matchesMask ) <EOL> cv2 . imshow ( '<STR_LIT>' , img3 ) <EOL> print '<STR_LIT>' <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> test_feature_matching_realtime ( ) </s>
