Guest User

Untitled

a guest
Jul 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. import sys
  2. import cv2
  3. import numpy as np
  4. from math import sqrt, acos
  5. from optparse import OptionParser
  6.  
  7. #FULL = "full.jpg"
  8. #CROP = "crop.jpg"
  9. #FULL = "pleiades_hyades_50percent.jpg"
  10. #CROP = "pleiades_hyades_50percent_crop.jpg"
  11. #FULL = "baum0vbhs9m8xf.jpg"
  12. #CROP = "baum0vbhs9m8xf_crop.jpg"
  13.  
  14.  
  15. MATCH_COUNT = 10
  16.  
  17. if __name__ == '__main__':
  18.  
  19. parser = OptionParser()
  20. (options, args) = parser.parse_args()
  21.  
  22. if len(args) != 2:
  23. print "Usage: templatematching image1 crop"
  24. sys.exit(0)
  25.  
  26.  
  27. full = cv2.imread(args[0]) # queryImage
  28. crop = cv2.imread(args[1]) # trainImage
  29. img2 = cv2.cvtColor(full, cv2.COLOR_BGR2GRAY)
  30. img1 = cv2.cvtColor(crop, cv2.COLOR_BGR2GRAY)
  31.  
  32. # Initiate SIFT detector
  33. orb = cv2.ORB_create()
  34. print orb
  35.  
  36. # find the keypoints and descriptors with SIFT
  37. kp1, des1 = orb.detectAndCompute(img1, None)
  38. kp2, des2 = orb.detectAndCompute(img2, None)
  39.  
  40. # create BFMatcher object
  41. bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
  42.  
  43. # Match descriptors.
  44. matches = bf.match(des1,des2)
  45.  
  46.  
  47. # Sort them in the order of their distance.
  48. matches = sorted(matches, key = lambda x:x.distance)[:MATCH_COUNT]
  49. for m in matches:
  50. print m.distance
  51.  
  52. if len(matches) >= MATCH_COUNT:
  53. src_pts = np.float32([ kp1[m.queryIdx].pt for m in matches ]).reshape(-1,1,2)
  54. dst_pts = np.float32([ kp2[m.trainIdx].pt for m in matches ]).reshape(-1,1,2)
  55.  
  56. M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
  57. matchesMask = mask.ravel().tolist()
  58.  
  59. h,w = img1.shape
  60. pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2)
  61. dst = cv2.perspectiveTransform(pts,M)
  62. mp = cv2.perspectiveTransform(np.float32([[w/2.0, h/2.0]]).reshape(-1,1,2), M)[0][0]
  63. cv2.circle(img2, (mp[0], mp[1]), 5, 255, -1)
  64.  
  65. #img2 = cv2.polylines(img2,[np.int32(dst)],True,255,3, cv2.LINE_AA)
  66. else:
  67. print "Not enough matches! (minimum is %d matches" % MATCH_COUNT
  68. sys.exit()
  69.  
  70. # Draw matches.
  71. img2 = cv2.polylines(img2, [np.int32(dst)], True, 255, 5, cv2.LINE_AA)
  72. img3 = cv2.drawMatches(img1, kp1, img2, kp2, matches, None, flags=2)
  73. result = cv2.polylines(full, [np.int32(dst)], True, (255, 255, 255), 5, cv2.LINE_AA)
  74. cv2.circle(result, (mp[0], mp[1]), 2, (255, 255, 255), -1)
  75. cv2.circle(result, (mp[0], mp[1]), 10, (255, 255, 255), 2)
  76.  
  77. ###########
  78. # calculate info
  79. ###########
  80. # vector of upper edge
  81. vec = dst[3][0] - dst[0][0]
  82. #print sqrt(np.dot(vec, vec))
  83. # zoom factor crop width / full width
  84. zoom = img2.shape[1] / sqrt(np.dot(vec, vec))
  85. # angle upper edge to x axis
  86. angle = acos(np.dot(vec, np.array([1, 0])) / (sqrt(vec[0]**2 + vec[1]**2)))
  87.  
  88. print args[0], args[1]
  89. print "middlepoint:", mp
  90. print "zoom:", zoom
  91. print "angle:", np.rad2deg(angle)
  92. print "corners:"
  93. print "\n".join([str(i[0]) for i in dst])
  94.  
  95. ### write data to file
  96. with open("results.txt", "a") as f:
  97. f.write("\n")
  98. f.write("%s %s\n" % (args[0], args[1]))
  99. f.write("middlepoint: %s\n" % str(mp) )
  100. f.write("zoom: %.2f\n" % zoom )
  101. f.write("angle: %.2f\n" % np.rad2deg(angle) )
  102. f.write("corners: %s\n" % ", ".join([str(i[0]) for i in dst]))
  103.  
  104.  
  105. cv2.namedWindow("image", cv2.WINDOW_NORMAL)
  106. cv2.imshow('image', img3)
  107. cv2.waitKey(0)
  108. cv2.imshow('image', result)
  109. cv2.waitKey(0)
Add Comment
Please, Sign In to add comment