Advertisement
Guest User

Untitled

a guest
Aug 15th, 2016
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.92 KB | None | 0 0
  1. import numpy as np
  2. import cv2
  3. import os
  4. from os import listdir
  5. from os.path import isfile, join
  6. import math
  7.  
  8. #Image folder
  9. impath = str('E:/gp/chalan')
  10. files = [f for f in listdir(impath) if isfile(join(impath, f))]
  11. images = np.empty(len(files), dtype=object)
  12.  
  13. counter = 0
  14. while counter < len(files):
  15.    
  16.     #Optical flow lists initialization
  17.     ls_kpc = []
  18.     ls_kpn = []
  19.        
  20.     #Read Images
  21.     imgc = cv2.imread(join(impath, files[counter]), 0) #First image
  22.     imgn = cv2.imread(join(impath, files[counter+1]), 0) #Next image
  23.  
  24.     #SURF Keypoint Detection
  25.     orb = cv2.ORB(100)
  26.    
  27.     kpc, desc = orb.detectAndCompute(imgc, None)
  28.     kpn, desn = orb.detectAndCompute(imgn, None)
  29.  
  30.     #BFMatcher
  31.     bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
  32.  
  33.     #Matches
  34.     matches = bf.match(desc, desn)
  35.     matches = sorted(matches, key = lambda x:x.distance)
  36.  
  37.     #Matched Points
  38.     for match in matches:
  39.         imgc_idx = match.queryIdx
  40.         imgn_idx = match.trainIdx
  41.         (x1, y1) = kpc[imgc_idx].pt
  42.         (x2, y2) = kpn[imgn_idx].pt
  43.        
  44.         ls_kpc.append((x1,y1))
  45.         ls_kpn.append((x2,y2))
  46.    
  47.     #Assign optical flow points to x, y coordinates lists
  48.     matc = np.array(ls_kpc, dtype=np.int16)
  49.     matn = np.array(ls_kpn, dtype=np.int16)
  50.     mag = np.empty((len(matc), 1), dtype=np.int32)
  51.     speed = np.empty((len(matc),1), dtype=np.int32)
  52.     vDir = np.empty((len(matc),1), dtype=np.float32)
  53.    
  54.     #Speed, displacement magnitude and vector direction
  55.     for i in range(0, len(matc)):
  56.         dX = matn[i,0]-matc[i,0]
  57.         dY = matn[i,1]-matc[i,1]
  58.         mag[i, 0] = math.sqrt((dX**2)+(dY**2))
  59.         speed[i, 0] = (mag[i, 0]/2)
  60.         vDir[i, 0] = math.degrees(math.atan2(dX, dY))
  61.         #print vDir[i], '\n'
  62.     avMag = np.sum(mag)/len(matc)
  63.     avSpeed = np.sum(speed)/len(matc)
  64.     avDir = np.std(vDir)
  65.     print avDir
  66.  
  67.     counter += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement