Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. #importing some useful packages
  2. import os
  3. import matplotlib.pyplot as plt
  4. import matplotlib.image as mpimg
  5. import numpy as np
  6. import cv2
  7. %matplotlib inline
  8. import math
  9. image_list=os.listdir("test_images/")
  10. #for image_Name in image_list:
  11. #reading in an image
  12. image_Name='solidYellowCurve2.jpg'
  13. image = mpimg.imread('test_images/'+image_Name)
  14. #printing out some stats and plotting
  15. print('This image is:', type(image), 'with dimesions:', image.shape)
  16. y_size=image.shape[0]
  17. x_size=image.shape[1]
  18. plt.imshow(image) #call as plt.imshow(gray, cmap='gray') to show a grayscaled image
  19. left_bottom = [0, y_size-1]
  20. right_bottom = [x_size-1, y_size-1]
  21. apex = [480, 250]
  22. vrio=np.array([left_bottom,right_bottom,apex])
  23. #vrio=np.int32(np.array([[0, 539],[959, 539],[470, 300]]))
  24.  
  25. low_threshold = 200
  26. high_threshold = 300
  27. ker_gb=7
  28.  
  29. rho=2
  30. theta=np.pi/180
  31. threshold=25
  32. min_line_len=40
  33. max_line_gap=20
  34.  
  35.  
  36. def grayscale(img):
  37. return cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  38.  
  39. def canny(img, low_threshold, high_threshold):
  40. return cv2.Canny(img, low_threshold, high_threshold)
  41.  
  42. def gaussian_blur(img, kernel_size):
  43. return cv2.GaussianBlur(img, (kernel_size, kernel_size), 0)
  44.  
  45. def region_of_interest(img, vertices):
  46. #defining a blank mask to start with
  47. mask = np.zeros_like(img)
  48. #defining a 3 channel or 1 channel color to fill the mask with depending on the input image
  49. if len(img.shape) > 2:
  50. channel_count = img.shape[2] # i.e. 3 or 4 depending on your image
  51. ignore_mask_color = (255,) * channel_count
  52. else:
  53. ignore_mask_color = 255
  54.  
  55. #filling pixels inside the polygon defined by "vertices" with the fill color
  56. cv2.fillPoly(mask, vertices, ignore_mask_color)
  57.  
  58. #returning the image only where mask pixels are nonzero
  59. masked_image = cv2.bitwise_and(img, mask)
  60. return masked_image
  61.  
  62.  
  63. def draw_lines(img, lines, color=[255, 0, 0], thickness=3):
  64. right_lines=[]
  65. left_lines=[]
  66. for line in lines:
  67. for x1,y1,x2,y2 in line:
  68. m=((y2-y1)/(x2-x1))
  69. if m >0:
  70. right_lines.append([x1,y1])
  71. right_lines.append([x2,y2])
  72. else:
  73. left_lines.append([x1,y1])
  74. left_lines.append([x2,y2])
  75.  
  76. [vx, vy, x, y] = cv2.fitLine(np.array(left_lines), cv2.DIST_L2, 0, 0.01, 0.01)
  77. b = int((-x*vy/vx) +y)
  78. x1=int((y_size-1-b)*vx/vy)
  79. x2=int((300-b)*vx/vy)
  80. cv2.line(img,(x1,y_size-1),(x2,300),color, thickness)
  81.  
  82. [vx, vy, x, y] = cv2.fitLine(np.array(right_lines), cv2.DIST_L2, 0, 0.01, 0.01)
  83. b = int((-x*vy/vx) +y)
  84. x1=int((y_size-1-b)*vx/vy)
  85. x2=(300-b)*vx/vy
  86. cv2.line(img,(x1,y_size-1),(x2,300),color, thickness)
  87.  
  88.  
  89.  
  90.  
  91.  
  92. def hough_lines(img, rho, theta, threshold, min_line_len, max_line_gap):
  93. lines = cv2.HoughLinesP(img, rho, theta, threshold, np.array([]), minLineLength=min_line_len, maxLineGap=max_line_gap)
  94. line_img = np.zeros((*img.shape, 3), dtype=np.uint8)
  95. draw_lines(line_img, lines)
  96. #print(lines)
  97. return line_img
  98.  
  99. # Python 3 has support for cool math symbols.
  100.  
  101. def weighted_img(img, initial_img, α=0.8, β=1., λ=0.):
  102. return cv2.addWeighted(initial_img, α, img, β, λ)
  103.  
  104.  
  105.  
  106. lower=np.array([0, 100, 100])
  107. upper=np.array([255, 255, 255])
  108.  
  109. ranged_image = cv2.inRange(image, lower, upper)
  110. gray_image=grayscale(ranged_image)
  111. gb_image=gaussian_blur(gray_image,ker_gb)
  112. #roi_image=region_of_interest(gb_image,[vrio])
  113. canny_image=canny(gb_image,low_threshold,high_threshold)
  114. lines=hough_lines(canny_image, rho, theta, threshold, min_line_len, max_line_gap)
  115. roi_image=region_of_interest(lines,[vrio])
  116. #lines_f=hough_lines(roi_image, rho, theta, threshold, min_line_len, max_line_gap)
  117. final_image=weighted_img(roi_image, image, α=0.8, β=1., λ=0.)
  118. plt.imshow(final_image)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement