Advertisement
resta89

Untitled

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