# Setup
import models
import ransac
import os
import numpy as np
import h5py
import json
import math
import sys
from scipy.spatial import distance
import cv2
import time
import glob
import matplotlib.pyplot as plt
from pylab import axis

def main():
    script, jsonfile = sys.argv
    with open(jsonfile) as data_file1:
            data1 = json.load(data_file1)
    
    pms = data1['pointmatches']
    pointmatches = []
    for i in range(0, len(pms)):
        pointmatches.append((np.array(pms[i]['point1']), np.array(pms[i]['point2'])))
    
    point1s = map(list, zip(*pointmatches))[0]
    point1r = map(list, zip(*pointmatches))[0]
    point1s = map(lambda x: np.matrix(x).T, point1s)
    point2s = map(list, zip(*pointmatches))[1]
    point2r = map(list, zip(*pointmatches))[1]
    point2s = map(lambda x: np.matrix(x).T, point2s)
    centroid1 = [np.array(point1s)[:,0].mean(), np.array(point1s)[:,1].mean()]
    centroid2 = [np.array(point2s)[:,0].mean(), np.array(point2s)[:,1].mean()]

    model_index = 1
    iterations = 500
    max_epsilon = 200
    min_inlier_ratio = 0
    min_num_inlier = 7
    max_trust = 3
    pointmatchesr = np.array([point1r, point2r])
    model, filtered_matches = ransac.filter_matches(pointmatchesr, model_index, iterations, max_epsilon, min_inlier_ratio, min_num_inlier, max_trust)
    R = model.get_matrix()[0:2,0:2]
    '''
    h = np.matrix(np.zeros((2,2)))
    for i in range(0, len(point1s)):
        sumpart = (np.matrix(point1s[i]) - centroid1).dot((np.matrix(point2s[i]) - centroid2).T)
        h = h + sumpart
    U, S, Vt = np.linalg.svd(h)
    R = Vt.T.dot(U.T)
    print R
    '''

    plt.figure(1)
    linelens = []
    for i in range(0,len(pointmatches)):
        point1, point2 = pointmatches[i]
        # point1 = np.matrix(point1 - centroid1).dot(R.T).tolist()[0]
        point1 = point1 - centroid1
        point2 = point2 - centroid2
        plt.plot([point1[0], point2[0]], [point1[1], point2[1]])
        linelen = ((point2[0] - point1[0]) ** 2 + (point2[1] - point1[1]) ** 2) ** 0.5
        linelens.append(linelen)
        axis('equal')
    plt.show()
    # plt.hist(linelens)
    # plt.show()

if __name__ == '__main__':
    main()

@@ -144,19 +144,8 @@
 nce%0A
-import cv2%0A
 impo
@@ -205,25 +205,44 @@
 rom 
-pylab import axis
+matplotlib import collections  as mc
 %0A%0Ade
@@ -364,20 +364,16 @@
 _file1)%0A
-    
 %0A    pms
@@ -410,238 +410,49 @@
 oint
-matches = %5B%5D%0A    for i in range(0, len(pms)):%0A        pointmatches.append((np.array(pms%5Bi%5D%5B'point1'%5D), np.array(pms%5Bi%5D
+s1 = np.array(%5Bp
 %5B'point
-2'%5D)))%0A    %0A    point1s = map(list, zip(*pointmatches))%5B0%5D%0A    point1r = map(list, zip(*pointmatches))%5B0%5D
+1'%5D for p in pms%5D)
 %0A   
@@ -461,356 +461,192 @@
 oint
-1
 s
+2
  = 
-map(lambda x: np.matrix(x).T, point1s)%0A    point2s = map(list, zip(*pointmatches))%5B1%5D%0A    point2r = map(list, zip(*pointmatches))%5B1%5D%0A    point2s = map(lambda x: np.matrix(x).T, point2s)%0A    centroid1 = %5Bnp.array(point1s)%5B:,0%5D.mean(), np.array(point1s)%5B:,1%5D.mean()%5D%0A    centroid2 = %5Bnp.array(point2s)%5B:,0%5D.mean(), np.array(point2s)%5B:,1%5D.mean()%5D
+np.array(%5Bp%5B'point2'%5D for p in pms%5D)%0A%0A    assert points1.shape%5B1%5D == 2%0A    %0A    centroid1 = points1.mean(axis=0, keepdims=True)%0A    centroid2 = points2.mean(axis=0, keepdims=True)
 %0A%0A  
@@ -702,17 +702,17 @@
 silon = 
-2
+1
 00%0A    m
@@ -796,35 +796,25 @@
 r = 
-np.array(%5B
+(
 point
+s
 1
-r
 , point
+s
 2
-r%5D
 )%0A  
@@ -1281,156 +1281,39 @@
     
-plt.figure(1)%0A    linelens = %5B%5D%0A    for i in range(0,len(pointmatches)):%0A        point1, point2 = pointmatches%5Bi%5D%0A        # point1 = np.matrix
+new_points1 = np.dot(R, 
 (point
+s
 1 - 
@@ -1327,51 +1327,36 @@
 d1).
-dot(R.T).tolist()%5B0%5D
+T).T
 %0A    
-    
+new_
 point
-1
+s2
  = point
 1 - 
@@ -1351,17 +1351,18 @@
  = point
-1
+s2
  - centr
@@ -1368,239 +1368,249 @@
 roid
-1%0A        point2 = point2 - centroid2%0A        plt.plot(%5Bpoint1%5B0%5D, point2%5B0%5D%5D, %5Bpoint1%5B1%5D, point2%5B1%5D%5D)%0A        linelen = ((point2%5B0%5D - point1%5B0%5D) ** 2 + (point2%5B1%5D - 
+2%0A    lines = %5B%5Bp1, p2%5D for p1, p2 in zip(new_points1, new_points2)%5D%0A    lc = mc.LineCollection(lines)%0A    plt.figure()%0A    plt.gca().add_collection(lc)%0A    plt.scatter(new_points1%5B:, 0%5D, new_
 point
+s
 1%5B
-1%5D) ** 2) ** 0.5%0A        linelens.append(linelen
+:, 1%5D)%0A    plt.gca().autoscale(
 )%0A    
-    
+plt.
 axis

simplify code, speed it up