wk-labs/lab4.ipynb
2021-11-28 23:19:14 +01:00

1.5 MiB

import cv2 as cv
import numpy as np
import math
import matplotlib.pyplot as plt
%matplotlib inline
road = cv.imread("img/road-lanes.jpg", cv.IMREAD_COLOR)
plt.figure(figsize=(40,50))
plt.subplot(121)
plt.imshow(road[:,:,::-1])
plt.title("Image")
Text(0.5, 1.0, 'Image')
road_gray = cv.cvtColor(park, cv.COLOR_BGR2GRAY)

_, img_bin = cv.threshold(road_gray, 230, 255, cv.THRESH_BINARY)

plt.figure(figsize=(40,50))
plt.subplot(121)
plt.imshow(road_gray, cmap='gray')
plt.title("Image")
plt.subplot(122)
plt.imshow(img_bin, cmap='gray')
plt.title("Binary")
Text(0.5, 1.0, 'Binary')
# lines = cv.HoughLines(img_bin, 1, np.pi / 180, 150, None, 0, 0)
final = road.copy()
linesP = cv.HoughLinesP(img_bin, 1, np.pi / 180, 50, None, 30, 10)
higher_point = final.shape[0]
lower_point = 350
    
if linesP is not None:
    for i in range(0, len(linesP)):
        l = linesP[i][0]

        a = (l[3] - l[1]) / (l[2] - l[0])
        b = l[3] - ( ((l[3] - l[1]) * l[2]) / (l[2] - l[0]) )
        
        
        higher_x = int((higher_point - b) / a)
        
        lower_x = int((lower_point - b) / a)
                
        cv.line(final, (higher_x, higher_point), (lower_x, lower_point), (0,0,255), 3, cv.LINE_AA)

alpha = 0.5
result = cv.addWeighted(final, alpha, road, 1 - alpha, 0)
        

plt.figure(figsize=(40,50))
plt.subplot(121)
plt.imshow(result[:,:,::-1])
plt.title("Image")
Text(0.5, 1.0, 'Image')