SHARE
TWEET

Untitled

a guest Sep 11th, 2019 102 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import matplotlib.pylab as plt
  2. import cv2
  3. import numpy as np
  4.  
  5. def region_of_interest(img, vertices):
  6.     mask = np.zeros_like(img)
  7.     #channel_count = img.shape[2]
  8.     match_mask_color = 255
  9.     cv2.fillPoly(mask, vertices, match_mask_color)
  10.     masked_image = cv2.bitwise_and(img, mask)
  11.     return masked_image
  12.  
  13. def drow_the_lines(img, lines):
  14.     img = np.copy(img)
  15.     blank_image = np.zeros((img.shape[0], img.shape[1], 3), dtype=np.uint8)
  16.  
  17.     for line in lines:
  18.         for x1, y1, x2, y2 in line:
  19.             cv2.line(blank_image, (x1,y1), (x2,y2), (0, 255, 0), thickness=10)
  20.  
  21.     img = cv2.addWeighted(img, 0.8, blank_image, 1, 0.0)
  22.     return img
  23.  
  24. # = cv2.imread('road.jpg')
  25. #image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  26. def process(image):
  27.     print(image.shape)
  28.     height = image.shape[0]
  29.     width = image.shape[1]
  30.     region_of_interest_vertices = [
  31.         (0, height),
  32.         (width/2, height/2),
  33.         (width, height)
  34.     ]
  35.     gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
  36.     canny_image = cv2.Canny(gray_image, 100, 120)
  37.     cropped_image = region_of_interest(canny_image,
  38.                     np.array([region_of_interest_vertices], np.int32),)
  39.     lines = cv2.HoughLinesP(cropped_image,
  40.                             rho=2,
  41.                             theta=np.pi/180,
  42.                             threshold=50,
  43.                             lines=np.array([]),
  44.                             minLineLength=40,
  45.                             maxLineGap=100)
  46.     image_with_lines = drow_the_lines(image, lines)
  47.     return image_with_lines
  48.  
  49. cap = cv2.VideoCapture('test.mp4')
  50.  
  51. while cap.isOpened():
  52.     ret, frame = cap.read()
  53.     frame = process(frame)
  54.     cv2.imshow('frame', frame)
  55.     if cv2.waitKey(1) & 0xFF == ord('q'):
  56.         break
  57.  
  58. cap.release()
  59. cv2.destroyAllWindows()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top