Mihao

Raspberry Pi Camera Detect Line

Jan 8th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. #Import modules
  2. import picamera
  3. import picamera.array
  4. import time
  5. import cv2
  6.  
  7.  
  8. #Initialize camera
  9. camera = picamera.PiCamera()
  10. camera.resolution = (640,480)
  11. rawCapture = picamera.array.PiRGBArray(camera)
  12. #Let camera warm up
  13. time.sleep(0.1)
  14.  
  15. #Capture image
  16. camera.capture(rawCapture, format="bgr")
  17. img = rawCapture.array
  18.  
  19. #Convert to Grayscale
  20. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  21.  
  22. #Blur image to reduce noise
  23. blurred = cv2.GaussianBlur(gray, (9, 9), 0)
  24.  
  25. #Perform canny edge-detection
  26. edged = cv2.Canny(blurred, 50, 150)
  27.  
  28. #Perform hough lines probalistic transform
  29. lines = cv2.HoughLinesP(edged,1,np.pi/180,10,80,1)
  30.  
  31. #Draw lines on input image
  32. if(lines != None):
  33. for x1,y1,x2,y2 in lines[0]:
  34. cv2.line(resized,(x1,y1),(x2,y2),(0,255,0),2)
  35.  
  36. cv2.imshow("line detect test", img)
  37. cv2.waitKey(0)
Add Comment
Please, Sign In to add comment