Advertisement
Guest User

Untitled

a guest
Jul 30th, 2015
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. import numpy as np
  2. import cv2
  3. import time
  4. ESC=27
  5. camera = cv2.VideoCapture(0)
  6. orb = cv2.ORB_create()
  7. bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
  8.  
  9. imgTrainColor=cv2.imread('/home/shar/q11.jpg')
  10. imgTrainGray = cv2.cvtColor(imgTrainColor, cv2.COLOR_BGR2GRAY)
  11.  
  12. kpTrain = orb.detect(imgTrainGray,None)
  13. kpTrain, desTrain = orb.compute(imgTrainGray, kpTrain)
  14.  
  15. firsttime=True
  16.  
  17. while True:
  18.  
  19. ret, imgCamColor = camera.read()
  20. imgCamGray = cv2.cvtColor(imgCamColor, cv2.COLOR_BGR2GRAY)
  21. kpCam = orb.detect(imgCamGray,None)
  22. kpCam, desCam = orb.compute(imgCamGray, kpCam)
  23. matches = bf.match(desCam,desTrain)
  24. good = []
  25. for m,n in matches:
  26. if m.distance < 0.5*n.distance:
  27. good.append(m)
  28.  
  29. if firsttime==True:
  30. h1, w1 = imgCamColor.shape[:2]
  31. h2, w2 = imgTrainColor.shape[:2]
  32. nWidth = w1+w2
  33. nHeight = max(h1, h2)
  34. hdif = (h1-h2)/2
  35. firsttime=False
  36.  
  37. result = np.zeros((nHeight+1, nWidth+1, 3), np.uint8)
  38. result[hdif:hdif+h2, :w2] = imgTrainColor
  39. result[:h1, w2:w1+w2] = imgCamColor
  40.  
  41. for i in range(len(matches)):
  42. pt_a=(int(kpTrain[matches[i].trainIdx].pt[0]), int(kpTrain[matches[i].trainIdx].pt[1]+hdif))
  43. pt_b=(int(kpCam[matches[i].queryIdx].pt[0]+w2), int(kpCam[matches[i].queryIdx].pt[1]))
  44. cv2.line(result, pt_a, pt_b, (255, 0, 0))
  45.  
  46. cv2.imshow('Camara', result)
  47.  
  48. key = cv2.waitKey(20)
  49. if key == ESC:
  50. break
  51.  
  52. cv2.destroyAllWindows()
  53. camera.release()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement