Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #importing some useful packages
- import os
- import matplotlib.pyplot as plt
- import matplotlib.image as mpimg
- import numpy as np
- import cv2
- %matplotlib inline
- import math
- image_list=os.listdir("test_images/")
- #for image_Name in image_list:
- #reading in an image
- image_Name='solidYellowCurve2.jpg'
- image = mpimg.imread('test_images/'+image_Name)
- #printing out some stats and plotting
- print('This image is:', type(image), 'with dimesions:', image.shape)
- y_size=image.shape[0]
- x_size=image.shape[1]
- plt.imshow(image) #call as plt.imshow(gray, cmap='gray') to show a grayscaled image
- left_bottom = [0, y_size-1]
- right_bottom = [x_size-1, y_size-1]
- apex = [480, 250]
- vrio=np.array([left_bottom,right_bottom,apex])
- #vrio=np.int32(np.array([[0, 539],[959, 539],[470, 300]]))
- low_threshold = 200
- high_threshold = 300
- ker_gb=7
- rho=2
- theta=np.pi/180
- threshold=25
- min_line_len=40
- max_line_gap=20
- def grayscale(img):
- return cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
- def canny(img, low_threshold, high_threshold):
- return cv2.Canny(img, low_threshold, high_threshold)
- def gaussian_blur(img, kernel_size):
- return cv2.GaussianBlur(img, (kernel_size, kernel_size), 0)
- def region_of_interest(img, vertices):
- #defining a blank mask to start with
- mask = np.zeros_like(img)
- #defining a 3 channel or 1 channel color to fill the mask with depending on the input image
- if len(img.shape) > 2:
- channel_count = img.shape[2] # i.e. 3 or 4 depending on your image
- ignore_mask_color = (255,) * channel_count
- else:
- ignore_mask_color = 255
- #filling pixels inside the polygon defined by "vertices" with the fill color
- cv2.fillPoly(mask, vertices, ignore_mask_color)
- #returning the image only where mask pixels are nonzero
- masked_image = cv2.bitwise_and(img, mask)
- return masked_image
- def draw_lines(img, lines, color=[255, 0, 0], thickness=3):
- right_lines=[]
- left_lines=[]
- for line in lines:
- for x1,y1,x2,y2 in line:
- m=((y2-y1)/(x2-x1))
- if m >0:
- right_lines.append([x1,y1])
- right_lines.append([x2,y2])
- else:
- left_lines.append([x1,y1])
- left_lines.append([x2,y2])
- [vx, vy, x, y] = cv2.fitLine(np.array(left_lines), cv2.DIST_L2, 0, 0.01, 0.01)
- b = int((-x*vy/vx) +y)
- x1=int((y_size-1-b)*vx/vy)
- x2=int((300-b)*vx/vy)
- cv2.line(img,(x1,y_size-1),(x2,300),color, thickness)
- [vx, vy, x, y] = cv2.fitLine(np.array(right_lines), cv2.DIST_L2, 0, 0.01, 0.01)
- b = int((-x*vy/vx) +y)
- x1=int((y_size-1-b)*vx/vy)
- x2=(300-b)*vx/vy
- cv2.line(img,(x1,y_size-1),(x2,300),color, thickness)
- def hough_lines(img, rho, theta, threshold, min_line_len, max_line_gap):
- lines = cv2.HoughLinesP(img, rho, theta, threshold, np.array([]), minLineLength=min_line_len, maxLineGap=max_line_gap)
- line_img = np.zeros((*img.shape, 3), dtype=np.uint8)
- draw_lines(line_img, lines)
- #print(lines)
- return line_img
- # Python 3 has support for cool math symbols.
- def weighted_img(img, initial_img, α=0.8, β=1., λ=0.):
- return cv2.addWeighted(initial_img, α, img, β, λ)
- lower=np.array([0, 100, 100])
- upper=np.array([255, 255, 255])
- ranged_image = cv2.inRange(image, lower, upper)
- gray_image=grayscale(ranged_image)
- gb_image=gaussian_blur(gray_image,ker_gb)
- #roi_image=region_of_interest(gb_image,[vrio])
- canny_image=canny(gb_image,low_threshold,high_threshold)
- lines=hough_lines(canny_image, rho, theta, threshold, min_line_len, max_line_gap)
- roi_image=region_of_interest(lines,[vrio])
- #lines_f=hough_lines(roi_image, rho, theta, threshold, min_line_len, max_line_gap)
- final_image=weighted_img(roi_image, image, α=0.8, β=1., λ=0.)
- plt.imshow(final_image)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement