Açıklama Yok

lane_detection.py 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Thu Sep 21 16:14:36 2017
  5. @author: alexdrake
  6. """
  7. import cv2
  8. import numpy as np
  9. import os
  10. # get working directory
  11. loc = os.path.abspath('')
  12. # import image for detecting lane edges
  13. img = cv2.imread(loc+"/trafficCounter/backgrounds/625_bg.jpg",0)
  14. kernel = np.ones((5,5),np.uint8)
  15. #Removing noise from image
  16. blur = cv2.bilateralFilter(img, 11, 3, 3)
  17. edges = cv2.Canny(img, 0, 820)
  18. edges2 = cv2.Canny(img, 0, 800)
  19. # get difference between edges
  20. diff = cv2.absdiff(edges, cv2.convertScaleAbs(edges2))
  21. laplacian = cv2.Laplacian(diff, cv2.CV_8UC1)
  22. # Do a dilation and erosion to accentuate the triangle shape
  23. dilated = cv2.dilate(laplacian, kernel, iterations = 2)
  24. erosion = cv2.erode(dilated,kernel,iterations = 3)
  25. # show erosion output to user
  26. cv2.imshow("ero", erosion)
  27. cv2.waitKey(0)
  28. # find contours
  29. im2, contours, hierarchy = cv2.findContours(erosion,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
  30. #keep 10 largest contours
  31. cnts = sorted(contours, key = cv2.contourArea, reverse = True)[:10]
  32. screenCnt = None
  33. for c in cnts:
  34. # approximate the contour
  35. peri = cv2.arcLength(c, True)
  36. approx = cv2.approxPolyDP(c, 0.05 * peri, True)
  37. # if our approximated contour has three points, then
  38. # it must be the road markings
  39. if len(approx) == 4:
  40. screenCnt = approx
  41. break
  42. cv2.drawContours(img, [approx], -1, (0, 255, 0), 3)
  43. cv2.imshow("Road markings", img)
  44. cv2.waitKey(0)