Mihao

Raspberry Pi Camera Tracking 2

Jan 8th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import sys
  3. import time
  4. import cv2
  5. import numpy as np
  6. import os
  7.  
  8. Kernel_size=15
  9. low_threshold=40
  10. high_threshold=120
  11.  
  12. rho=10
  13. threshold=15
  14. theta=np.pi/180
  15. minLineLength=10
  16. maxLineGap=1
  17.  
  18. #Initialize camera
  19. video_capture = cv2.VideoCapture(0)
  20.  
  21. while True:
  22. # CAPTURE FRAME-BY-FRAME
  23. ret, frame = video_capture.read()
  24. time.sleep(0.1)
  25. #Convert to Grayscale
  26. gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  27. #Blur image to reduce noise. if Kernel_size is bigger the image will be more blurry
  28. blurred = cv2.GaussianBlur(gray, (Kernel_size, Kernel_size), 0)
  29.  
  30. #Perform canny edge-detection.
  31. #If a pixel gradient is higher than high_threshold is considered as an edge.
  32. #if a pixel gradient is lower than low_threshold is is rejected , it is not an edge.
  33. #Bigger high_threshold values will provoque to find less edges.
  34. #Canny recommended ratio upper:lower between 2:1 or 3:1
  35. edged = cv2.Canny(blurred, low_threshold, high_threshold)
  36. #Perform hough lines probalistic transform
  37. lines = cv2.HoughLinesP(edged,rho,theta,threshold,minLineLength,maxLineGap)
  38.  
  39. #Draw cicrcles in the center of the picture
  40. cv2.circle(frame,(320,240),20,(0,0,255),1)
  41. cv2.circle(frame,(320,240),10,(0,255,0),1)
  42. cv2.circle(frame,(320,240),2,(255,0,0),2)
  43.  
  44. #With this for loops only a dots matrix is painted on the picture
  45. #for y in range(0,480,20):
  46. #for x in range(0,640,20):
  47. #cv2.line(frame,(x,y),(x,y),(0,255,255),2)
  48.  
  49. #With this for loops a grid is painted on the picture
  50. for y in range(0,480,40):
  51. cv2.line(frame,(0,y),(640,y),(255,0,0),1)
  52. for x in range(0,640,40):
  53. cv2.line(frame,(x,0),(x,480),(255,0,0),1)
  54.  
  55. #Draw lines on input image
  56. if(lines != None):
  57. for x1,y1,x2,y2 in lines[0]:
  58. cv2.line(frame,(x1,y1),(x2,y2),(0,255,0),2)
  59. cv2.putText(frame,'lines_detected',(50,50),cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0),1)
  60. cv2.imshow("line detect test", frame)
  61.  
  62. if cv2.waitKey(1) & 0xFF == ord('q'):
  63. break
  64.  
  65. # When everything is done, release the capture
  66.  
  67. video_capture.release()
  68. cv2.destroyAllWindows()
Add Comment
Please, Sign In to add comment