Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.44 KB | None | 0 0
  1. import cv2
  2.  
  3. # constants
  4. IMAGE_SIZE = 200.0
  5. MATCH_THRESHOLD = 3
  6.  
  7. # load haar cascade and street image
  8. roundabout_cascade = cv2.CascadeClassifier('orb/repository/haarcascade_roundabout.xml')
  9. street = cv2.imread('orb/repository/roundabout1.png')
  10.  
  11. # do roundabout detection on street image
  12. gray = cv2.cvtColor(street,cv2.COLOR_RGB2GRAY)
  13. roundabouts = roundabout_cascade.detectMultiScale(
  14.     gray,
  15.     scaleFactor=1.4,
  16.     minNeighbors=3
  17.     )
  18.  
  19. # initialize ORB and BFMatcher
  20. orb = cv2.ORB()
  21. bf = cv2.BFMatcher(cv2.NORM_HAMMING,crossCheck=True)
  22.  
  23. # find the keypoints and descriptors for roadsign image
  24. roadsign = cv2.imread('orb/roundabout.jpg',0)
  25. kp_r,des_r = orb.detectAndCompute(roadsign,None)
  26.  
  27. # loop through all detected objects
  28. for (x,y,w,h) in roundabouts:
  29.  
  30.     # obtain object from street image
  31.     obj = gray[y:y+h,x:x+w]
  32.     ratio = IMAGE_SIZE / obj.shape[1]
  33.     obj = cv2.resize(obj,(int(IMAGE_SIZE),int(obj.shape[0]*ratio)))
  34.  
  35.     # find the keypoints and descriptors for object
  36.     kp_o, des_o = orb.detectAndCompute(obj,None)
  37.     if len(kp_o) == 0 or des_o == None: continue
  38.  
  39.     # match descriptors
  40.     matches = bf.match(des_r,des_o)
  41.      
  42.     # draw object on street image, if threshold met
  43.     if(len(matches) >= MATCH_THRESHOLD):
  44.         cv2.rectangle(street,(x,y),(x+w,y+h),(255,0,0),2)
  45.  
  46. # show objects on street image
  47. cv2.imshow('street image', street)
  48. cv2.waitKey(3000)
  49. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement