def find_image_position(origin='origin.png', query='query.png', outfile=None):
    '''
    find all image positions
    @return None if not found else a tuple: (origin.shape, query.shape, postions)
    might raise Exception
    '''
    img1 = cv2.imread(query, 0) # query image(small)
    img2 = cv2.imread(origin, 0) # train image(big)

    # Initiate SIFT detector
    sift = cv2.SIFT()

    # find the keypoints and descriptors with SIFT
    kp1, des1 = sift.detectAndCompute(img1,None)
    kp2, des2 = sift.detectAndCompute(img2,None)
    print len(kp1), len(kp2)

    FLANN_INDEX_KDTREE = 0
    index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
    search_params = dict(checks = 50)

    # flann
    flann = cv2.FlannBasedMatcher(index_params, search_params)
    matches = flann.knnMatch(des1, des2, k=2)

    # store all the good matches as per Lowe's ratio test.
    good = []
    for m,n in matches:
        if m.distance < 0.7*n.distance:
            good.append(m)
    print len(kp1), len(kp2), 'good cnt:', len(good)

    if len(good)*1.0/len(kp1) < 0.5:
    #if len(good)<MIN_MATCH_COUNT:
        print "Not enough matches are found - %d/%d" % (len(good),MIN_MATCH_COUNT)
        return img2.shape, img1.shape, []

    queryPts = []
    trainPts = []
    for dm in good:
        queryPts.append(kp1[dm.queryIdx])
        trainPts.append(kp2[dm.trainIdx])

    img3 = cv2.drawKeypoints(img1, queryPts)
    cv2.imwrite('image/query.png', img3)

    img3 = cv2.drawKeypoints(img2, trainPts)
    point = _middlePoint(trainPts)
    print 'position in', point

    if outfile:
        edge = 10
        top_left = (point[0]-edge, point[1]-edge)
        bottom_right = (point[0]+edge, point[1]+edge)
        cv2.rectangle(img3, top_left, bottom_right, 255, 2)
        cv2.imwrite(outfile, img3)
    return img2.shape, img1.shape, [point]
def find_image_position ( origin = 'origin.png' , query = 'query.png' , outfile = None ) : img1 = cv2 . imread ( query , 0 ) img2 = cv2 . imread ( origin , 0 ) sift = cv2 . SIFT ( ) kp1 , des1 = sift . detectAndCompute ( img1 , None ) kp2 , des2 = sift . detectAndCompute ( img2 , None ) print len ( kp1 ) , len ( kp2 ) FLANN_INDEX_KDTREE = 0 index_params = dict ( algorithm = FLANN_INDEX_KDTREE , trees = 5 ) search_params = dict ( checks = 50 ) flann = cv2 . FlannBasedMatcher ( index_params , search_params ) matches = flann . knnMatch ( des1 , des2 , k = 2 ) good = [ ] for m , n in matches : if m . distance < 0.7 * n . distance : good . append ( m ) print len ( kp1 ) , len ( kp2 ) , 'good cnt:' , len ( good ) if len ( good ) * 1.0 / len ( kp1 ) < 0.5 : print "Not enough matches are found - %d/%d" % ( len ( good ) , MIN_MATCH_COUNT ) return img2 . shape , img1 . shape , [ ] queryPts = [ ] trainPts = [ ] for dm in good : queryPts . append ( kp1 [ dm . queryIdx ] ) trainPts . append ( kp2 [ dm . trainIdx ] ) img3 = cv2 . drawKeypoints ( img1 , queryPts ) cv2 . imwrite ( 'image/query.png' , img3 ) img3 = cv2 . drawKeypoints ( img2 , trainPts ) point = _middlePoint ( trainPts ) print 'position in' , point if outfile : edge = 10 top_left = ( point [ 0 ] - edge , point [ 1 ] - edge ) bottom_right = ( point [ 0 ] + edge , point [ 1 ] + edge ) cv2 . rectangle ( img3 , top_left , bottom_right , 255 , 2 ) cv2 . imwrite ( outfile , img3 ) return img2 . shape , img1 . shape , [ point ]
find all image positions
    @return None if not found else a tuple: (origin.shape, query.shape, postions)
    might raise Exception