Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. import numpy as np
  2. import cv2
  3. import time
  4. from matplotlib import pyplot as plt
  5.  
  6. imagefn=["drawing"]
  7. MIN_MATCH_COUNT = 10
  8.  
  9. def getComponentsHM(hm):
  10. theta=np.arctan(hm[0,1]/hm[0,0])*(180.0/np.pi)
  11. scale=np.abs(hm[0,0]/np.cos(theta*(np.pi/180.0)))
  12. #homography matrix to translation
  13. #px=0
  14. #py=0
  15. translation =(px,py)
  16. return (translation,theta,scale)
  17.  
  18. for imn in imagefn:
  19. print imn
  20. t = time.time()
  21. # read images
  22. img1 = cv2.imread(imn+'_'+str(0)+'.png',0)
  23. img2 = cv2.imread(imn+'_'+str(1)+'.png',0)
  24. # Feature detection and description with SIFT
  25. sift = cv2.SIFT()
  26. kp1, des1 = sift.detectAndCompute(img1,None)
  27. kp2, des2 = sift.detectAndCompute(img2,None)
  28. # match detected features
  29. bf = cv2.BFMatcher()
  30. matches = bf.match(des1,des2)
  31. # find homography matrix
  32. if len(matches)>MIN_MATCH_COUNT:
  33. src_pts = np.float32([ kp1[m.queryIdx].pt for m in matches]).reshape(-1,1,2)
  34. dst_pts = np.float32([ kp2[m.trainIdx].pt for m in matches]).reshape(-1,1,2)
  35. M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC,5.0)
  36. print "Matrix"
  37. print M
  38. T,th,sc= getComponentsHM(M)
  39. print "(x,y):",T
  40. print "Angle:",th
  41. print "Scale:",sc
  42.  
  43. else:
  44. print "Not enough matches - %d/%d" % (len(matches),MIN_MATCH_COUNT)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement