xdenisx

dsim

Apr 14th, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.66 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import cv2
  4. import os
  5. import urllib2
  6. import sys
  7.  
  8. def load_sample_image(filename):
  9.    response = urllib2.urlopen('https://raw.github.com/Itseez/opencv/master/samples/c/' + filename)
  10.    return cv2.imdecode(np.fromstring(response.read(), dtype='uint8'), 0)
  11.  
  12. def show_image(img):
  13.     plt.imshow(img)
  14.     if len(img.shape)<3:
  15.         plt.gray()
  16.     plt.axis("off")
  17.     #plt.show()
  18.     plt.savefig('sss.png', dpi=900)
  19.  
  20. img11 = cv2.imread(sys.argv[2]) #load_sample_image('box.png')
  21. img1 = cv2.cvtColor( img11, cv2.COLOR_RGB2GRAY)
  22. img22 = cv2.imread(sys.argv[3]) #load_sample_image('box_in_scene.png')
  23. img2 = cv2.cvtColor( img22, cv2.COLOR_RGB2GRAY)
  24.  
  25. print("Read img1 of size {} and img2 of size {}.".format(img1.shape, img2.shape))
  26. print("Image 1:")
  27. #show_image(img1)
  28. print("Image 2:")
  29. #show_image(img2)
  30.  
  31. th = int(sys.argv[1])
  32.  
  33. octaves = 7
  34. oct_layers = 5
  35.  
  36. detector = cv2.SURF(th,octaves,oct_layers)
  37. norm = cv2.NORM_L2
  38. matcher = cv2.BFMatcher(norm)
  39.  
  40. kp1, desc1 = detector.detectAndCompute(img1, None)
  41. kp2, desc2 = detector.detectAndCompute(img2, None)
  42.  
  43. print '%s feature points for image1' % len(kp1)
  44. print '%s feature points for image2' % len(kp2)
  45.  
  46. raw_matches = matcher.knnMatch(desc1, trainDescriptors = desc2, k = 2)
  47.  
  48. def filter_matches(kp1, kp2, matches, ratio = 0.9):
  49.     """
  50.    Keep only matches that have distance ratio to
  51.    second closest point less than 'ratio'.
  52.    """
  53.     mkp1, mkp2 = [], []
  54.     for m in matches:
  55.         if m[0].distance < m[1].distance * ratio:
  56.             m = m[0]
  57.             mkp1.append( kp1[m.queryIdx] )
  58.             mkp2.append( kp2[m.trainIdx] )
  59.     p1 = np.float32([kp.pt for kp in mkp1])
  60.     p2 = np.float32([kp.pt for kp in mkp2])
  61.     kp_pairs = zip(mkp1, mkp2)
  62.     return p1, p2, kp_pairs
  63.  
  64. p1, p2, kp_pairs = filter_matches(kp1, kp2, raw_matches)
  65.  
  66. H, status = cv2.findHomography(p1, p2, cv2.RANSAC, 40.0)
  67.  
  68. h1, w1 = img1.shape[:2]
  69. h2, w2 = img2.shape[:2]
  70. vis = np.zeros((max(h1, h2), w1), np.uint8)
  71. vis[:h1, :w1] = img1
  72. #vis[:h2, w1:w1+w2] = img2
  73. vis = cv2.cvtColor(vis, cv2.COLOR_GRAY2BGR)
  74.  
  75. #show_image(vis)
  76.  
  77. p1 = np.int32([kpp[0].pt for kpp in kp_pairs])
  78. p2 = np.int32([kpp[1].pt for kpp in kp_pairs]) #+ (w1, 0)
  79.  
  80. # plot the matches
  81. color = (0, 255, 0)
  82. ch_valid = 0
  83. for (x1, y1), (x2, y2), inlier in zip(p1, p2, status):
  84.     if inlier:
  85.         if (abs(x2-x1)>3 or abs(y2-y1)>3):
  86.             cv2.circle(vis, (x1, y1), 2, color, -1)
  87.             #cv2.circle(vis, (x2, y2), 2, color, -1)
  88.             cv2.line(vis, (x1, y1), (x2, y2), color)
  89.             ch_valid = ch_valid + 1
  90. print '%s number of valid vectors' % ch_valid
  91. show_image(vis)
Advertisement
Add Comment
Please, Sign In to add comment