Advertisement
MrLunk

Ball Tracking OpenCV - Py2CV3

May 12th, 2017
5,398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.40 KB | None | 0 0
  1. import cv2
  2. import cv2 as cv
  3. import numpy as np
  4.  
  5. kernel = np.ones((5,5),np.uint8)
  6.  
  7. # Take input from webcam
  8. cap = cv2.VideoCapture(0)
  9.  
  10. # Reduce the size of video to 320x240 so rpi can process faster
  11. cap.set(3,320)
  12. cap.set(4,240)
  13.  
  14. def nothing(x):
  15.     pass
  16. # Creating a windows for later use
  17. cv2.namedWindow('HueComp')
  18. cv2.namedWindow('SatComp')
  19. cv2.namedWindow('ValComp')
  20. cv2.namedWindow('closing')
  21. cv2.namedWindow('tracking')
  22.  
  23.  
  24. # Creating track bar for min and max for hue, saturation and value
  25. # You can adjust the defaults as you like
  26. cv2.createTrackbar('hmin', 'HueComp',12,179,nothing)
  27. cv2.createTrackbar('hmax', 'HueComp',37,179,nothing)
  28.  
  29. cv2.createTrackbar('smin', 'SatComp',96,255,nothing)
  30. cv2.createTrackbar('smax', 'SatComp',255,255,nothing)
  31.  
  32. cv2.createTrackbar('vmin', 'ValComp',186,255,nothing)
  33. cv2.createTrackbar('vmax', 'ValComp',255,255,nothing)
  34.  
  35. # My experimental values
  36. # hmn = 12
  37. # hmx = 37
  38. # smn = 145
  39. # smx = 255
  40. # vmn = 186
  41. # vmx = 255
  42.  
  43.  
  44. while(1):
  45.  
  46.     buzz = 0
  47.     _, frame = cap.read()
  48.  
  49.     #converting to HSV
  50.     hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
  51.     hue,sat,val = cv2.split(hsv)
  52.  
  53.     # get info from track bar and appy to result
  54.     hmn = cv2.getTrackbarPos('hmin','HueComp')
  55.     hmx = cv2.getTrackbarPos('hmax','HueComp')
  56.    
  57.  
  58.     smn = cv2.getTrackbarPos('smin','SatComp')
  59.     smx = cv2.getTrackbarPos('smax','SatComp')
  60.  
  61.  
  62.     vmn = cv2.getTrackbarPos('vmin','ValComp')
  63.     vmx = cv2.getTrackbarPos('vmax','ValComp')
  64.  
  65.     # Apply thresholding
  66.     hthresh = cv2.inRange(np.array(hue),np.array(hmn),np.array(hmx))
  67.     sthresh = cv2.inRange(np.array(sat),np.array(smn),np.array(smx))
  68.     vthresh = cv2.inRange(np.array(val),np.array(vmn),np.array(vmx))
  69.  
  70.     # AND h s and v
  71.     tracking = cv2.bitwise_and(hthresh,cv2.bitwise_and(sthresh,vthresh))
  72.  
  73.     # Some morpholigical filtering
  74.     dilation = cv2.dilate(tracking,kernel,iterations = 1)
  75.     closing = cv2.morphologyEx(dilation, cv2.MORPH_CLOSE, kernel)
  76.     closing = cv2.GaussianBlur(closing,(5,5),0)
  77.  
  78.     # Detect circles using HoughCircles
  79.     circles = cv2.HoughCircles(closing,cv2.HOUGH_GRADIENT,2,120,param1=120,param2=50,minRadius=10,maxRadius=0)
  80.     # circles = np.uint16(np.around(circles))
  81.    
  82.  
  83.     #Draw Circles
  84.     if circles is not None:
  85.             for i in circles[0,:]:
  86.                 # If the ball is far, draw it in green
  87.                 if int(round(i[2])) < 30:
  88.                     cv2.circle(frame,(int(round(i[0])),int(round(i[1]))),int(round(i[2])),(0,255,0),5)
  89.                     cv2.circle(frame,(int(round(i[0])),int(round(i[1]))),2,(0,255,0),10)
  90.                 # else draw it in red
  91.                 elif int(round(i[2])) > 35:
  92.                     cv2.circle(frame,(int(round(i[0])),int(round(i[1]))),int(round(i[2])),(0,0,255),5)
  93.                     cv2.circle(frame,(int(round(i[0])),int(round(i[1]))),2,(0,0,255),10)
  94.                     buzz = 1
  95.  
  96.     #you can use the 'buzz' variable as a trigger to switch some GPIO lines on Rpi :)
  97.     # print buzz                    
  98.     # if buzz:
  99.         # put your GPIO line here
  100.  
  101.    
  102.     #Show the result in frames
  103.     cv2.imshow('HueComp',hthresh)
  104.     cv2.imshow('SatComp',sthresh)
  105.     cv2.imshow('ValComp',vthresh)
  106.     cv2.imshow('closing',closing)
  107.     cv2.imshow('tracking',frame)
  108.  
  109.     k = cv2.waitKey(5) & 0xFF
  110.     if k == 27:
  111.         break
  112.  
  113. cap.release()
  114.  
  115. cv2.destroyAllWindows()
  116.  
  117. #https://www.facebook.com/mrlunk
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement