Advertisement
resta89

Untitled

Oct 20th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 KB | None | 0 0
  1. #importing some useful packages
  2. import matplotlib.pyplot as plt
  3. import matplotlib.image as mpimg
  4. import numpy as np
  5. import cv2
  6. from scipy import ndimage, misc
  7. %matplotlib inline
  8. import math
  9.  
  10. def grayscale(img):
  11. img=cv2.imread('test_images/solidWhiteRight.jpg')
  12. """Applies the Grayscale transform
  13. This will return an image with only one color channel
  14. but NOTE: to see the returned image as grayscale
  15. (assuming your grayscaled image is called 'gray')
  16. you should call plt.imshow(gray, cmap='gray')"""
  17. #return np.dot(img[...,:3], [0.299, 0.587, 0.114])
  18. #return cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
  19. # Or use BGR2GRAY if you read an image with cv2.imread()
  20. return cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  21.  
  22. gr=grayscale(img)
  23. #plt.imshow(gr,cmap = plt.get_cmap('gray'))
  24. plt.imshow(gr)
  25. plt.show()
  26. #img = mpimg.imread('test_images/solidWhiteRight.jpg')
  27. #gray = grayscale(img)
  28. #plt.imshow(gray, cmap = plt.get_cmap('gray'))
  29. #plt.show()
  30.  
  31.  
  32.  
  33. def canny(gr, low_threshold, high_threshold):
  34. # """Applies the Canny transform"""
  35.  
  36. #blur_gray = cv2.GaussianBlur(gray,(kernel_size, kernel_size), 0)
  37. low_threshold = 50
  38. high_threshold = 180
  39. #misc.imsave('blur_gray.jpg', blur_gray)
  40. #canny = ndimage.imread('blur_gray.jpg',0)
  41. #edges = cv2.Canny(canny, low_threshold, high_threshold)
  42. return cv2.Canny(img, low_threshold, high_threshold)
  43. e=canny(img,50,180)
  44. #plt.imshow(e, plt.get_cmap('gray'))
  45. plt.imshow(e)
  46. plt.show()
  47.  
  48.  
  49. def gaussian_blur(e, kernel_size):
  50. #"""Applies a Gaussian Noise kernel"""
  51. kernel_size = 3
  52. return cv2.GaussianBlur(e, (kernel_size, kernel_size), 0)
  53. m=gaussian_blur(e,5)
  54. plt.imshow(m)
  55. plt.show()
  56.  
  57.  
  58.  
  59.  
  60. def region_of_interest(m, vertices):
  61. """
  62. Applies an image mask.
  63.  
  64. Only keeps the region of the image defined by the polygon
  65. formed from `vertices`. The rest of the image is set to black.
  66. """
  67. imshape = m.shape
  68. vertices = np.array([[(0,imshape[0]),(450, 290), (490, 290), (imshape[1],imshape[0])]], dtype=np.int32)
  69. mask = np.zeros_like(m)
  70. masked_edges = cv2.bitwise_and(m, mask)
  71. #defining a blank mask to start with
  72.  
  73.  
  74. #defining a 3 channel or 1 channel color to fill the mask with depending on the input image
  75. if len(m.shape) > 2:
  76. channel_count = m.shape[2] # i.e. 3 or 4 depending on your image
  77. ignore_mask_color = (255,) * channel_count
  78. else:
  79. ignore_mask_color = 255
  80.  
  81.  
  82. #filling pixels inside the polygon defined by "vertices" with the fill color
  83. cv2.fillPoly(mask, vertices, ignore_mask_color)
  84.  
  85. #returning the image only where mask pixels are nonzero
  86. masked_image = cv2.bitwise_and(m, mask)
  87. return masked_image
  88.  
  89. f=region_of_interest(m,((190, 470),(450, 270),(490, 270),(590, 470)))
  90. plt.imshow(f)
  91. plt.show()
  92.  
  93.  
  94. def draw_lines(masked, lines, color=[255, 0, 0], thickness=2):
  95. """
  96. NOTE: this is the function you might want to use as a starting point once you want to
  97. average/extrapolate the line segments you detect to map out the full
  98. extent of the lane (going from the result shown in raw-lines-example.mp4
  99. to that shown in P1_example.mp4).
  100.  
  101. Think about things like separating line segments by their
  102. slope ((y2-y1)/(x2-x1)) to decide which segments are part of the left
  103. line vs. the right line. Then, you can average the position of each of
  104. the lines and extrapolate to the top and bottom of the lane.
  105.  
  106. This function draws `lines` with `color` and `thickness`.
  107. Lines are drawn on the image inplace (mutates the image).
  108. If you want to make the lines semi-transparent, think about combining
  109. this function with the weighted_img() function below
  110. """
  111. for line in lines:
  112. for x1,y1,x2,y2 in line:
  113. cv2.line(line_image,(x1,y1),(x2,y2),(255,0,0),10)
  114. color_edges = np.dstack((edges, edges, edges))
  115. lines_edges = cv2.addWeighted(color_edges, 0.8, line_image, 1, 0)
  116.  
  117.  
  118.  
  119.  
  120. def hough_lines(masked_image, rho, theta, threshold, min_line_len, max_line_gap):
  121. """
  122. `img` should be the output of a Canny transform.
  123.  
  124. Returns an image with hough lines drawn.
  125. """
  126. lines = cv2.HoughLinesP(img, rho, theta, threshold, minLineLength=min_line_len, maxLineGap=max_line_gap)
  127. rho = 1 # distance resolution in pixels of the Hough grid
  128. theta = np.pi/180 # angular resolution in radians of the Hough grid
  129. threshold = 1 # minimum number of votes (intersections in Hough grid cell)
  130. min_line_len = 50 #minimum number of pixels making up a line
  131. max_line_gap = 10 # maximum gap in pixels between connectable line segments
  132. # Python 3 has support for cool math symbols.
  133. line_img = np.zeros((edges.shape[0], edges.shape[1], 3), dtype=np.uint8)
  134. draw_lines(line_img, lines)
  135. return draw_lines
  136.  
  137. ht=hough_lines(masked_image,1,np.pi/180,1,50,10)
  138. plt.imshow(ht)
  139. plt.show()
  140.  
  141.  
  142.  
  143. g=hough_lines(edges, rho, theta, threshold, min_line_len, max_line_gap)
  144. plt.show()
  145.  
  146. def weighted_img(img, initial_img, α=0.8, β=1., λ=0.):
  147. """
  148. `img` is the output of the hough_lines(), An image with lines drawn on it.
  149. Should be a blank image (all black) with lines drawn on it.
  150.  
  151. `initial_img` should be the image before any processing.
  152.  
  153. The result image is computed as follows:
  154.  
  155. initial_img * α + img * β + λ
  156. NOTE: initial_img and img must be the same shape!
  157. """
  158. return cv2.addWeighted(initial_img, α, img, β, λ)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement