<jupyter_start><jupyter_text>**Masking the region of interest**<jupyter_code># Import the required libraries:

import cv2
import numpy as np
import matplotlib.pyplot as plt
# Next, write a function for canny edge detection
def canny_edge(image):
    gray_conversion = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    blur_conversion = cv2.GaussianBlur(gray_conversion, (5,5), 0)
    canny_conversion = cv2.Canny(blur_conversion, 50, 150)
    return canny_conversion
def reg_of_interest(image):
    image_height = image.shape[0]
    polygons = np.array([[(200, image_height), (1100, image_height), (500, 200)]])
    image_mask = np.zeros_like(image)
    cv2.fillPoly(image_mask, polygons, 200)
    return image_mask
image = cv2.imread('resources/road-marking/test_image.jpg')
lanelines_image = np.copy(image)
canny_conversion = canny_edge(lanelines_image)
plt.imshow(canny_conversion, cmap = 'gray')
# We will apply region-of-interest masking 
cv2.imshow('result', reg_of_interest(canny_conversion))
cv2.waitKey()
cv2.destroyAllWindows()<jupyter_output><empty_output><jupyter_text>**Applying bitwise_and**<jupyter_code>def ROI_bitwise_and(image):
    image_height = image.shape[0]
    polygons = np.array([[(200, image_height), (1100, image_height), (500, 200)]])
    image_mask = np.zeros_like(image)
    cv2.fillPoly(image_mask, polygons, 200)
    masking_image = cv2.bitwise_and(image, image_mask)
    return masking_image
image = cv2.imread('resources/road-marking/test_image.jpg')
landlines_image = np.copy(image)
canny_conversion = canny_edge(landlines_image)
cropped_image = ROI_bitwise_and(canny_conversion)
cv2.imshow('result', cropped_image)
cv2.waitKey()
cv2.destroyAllWindows()<jupyter_output><empty_output><jupyter_text>**Applying the Hough transform**<jupyter_code>import cv2
import numpy as np
import matplotlib.pyplot as plt
def canny_edge(image):
    gray_conversion = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    blur_conversion = cv2.GaussianBlur(gray_conversion, (5,5), 0)
    canny_conversion = cv2.Canny(blur_conversion, 50, 150)
    return canny_conversion
def ROI_bitwise_and(image):
    image_height = image.shape[0]
    polygons = np.array([[(200, image_height), (1100, image_height), (510, 250)]])
    image_mask = np.zeros_like(image)
    cv2.fillPoly(image_mask, polygons, 250)
    masking_image = cv2.bitwise_and(image, image_mask)
    return masking_image
def show_lines(image, lines):
    lines_image = np.zeros_like(image)
    if lines is not None:
        for line in lines:
            X1, Y1, X2, Y2 = line.reshape(4)
            cv2.line(lines_image, (X1, Y1), (X2, Y2), (255,0,0), 10)
    return lines_image        
image = cv2.imread('resources/road-marking/test_image.jpg')
lanelines_image = np.copy(image)
canny_conv = canny_edge(lanelines_image)
cropped_image = ROI_bitwise_and(canny_conv)
lane_lines = cv2.HoughLinesP(cropped_image, 2, np.pi/180, 100, np.array([]), minLineLength= 40, maxLineGap=5)
landlines_image = show_lines(lanelines_image, lane_lines)
plt.imshow(linelines_image)<jupyter_output><empty_output><jupyter_text>Above the image after applying the Hough transformThe image we detected is displayed on a black background. Therefore, the final step is to combine this with our original image.<jupyter_code>#Combining with actual image

image = cv2.imread('resources/road-marking/test_image.jpg')
lane_image = np.copy(image)
canny = canny_edge(lane_image)
cropped_image = ROI_bitwise_and(canny)
lines = cv2.HoughLinesP(cropped_image, 2, np.pi/180, 100, np.array([]), minLineLength= 40, maxLineGap=5)
line_image = show_lines(lane_image, lines)
combine_image = cv2.addWeighted(lane_image, 0.8, line_image, 1, 1)


plt.imshow(combine_image)
cv2.imshow('result', combine_image)
cv2.waitKey()
cv2.destroyAllWindows()<jupyter_output><empty_output><jupyter_text>**Optimization the detected road markings** **Detecting road markings in video**<jupyter_code>import cv2
import numpy as np
import matplotlib.pyplot as plt
def make_coordinates(image, line_parameters):
          try:
            slope, intercept = line_parameters
          except TypeError:
            slope, intercept = 0.001,0
          #slope, intercept = line_parameters
          y1 = image.shape[0]
          y2 = int(y1*(3/5))
          x1 = int((y1- intercept)/slope)
          x2 = int((y2 - intercept)/slope)
          return np.array([x1, y1, x2, y2])
def average_slope_intercept(image, lines):
          left_fit = []
          right_fit = []
          for line in lines:
            x1, y1, x2, y2 = line.reshape(4)
            parameter = np.polyfit((x1, x2), (y1, y2), 1)
            slope = parameter[0]
            intercept = parameter[1]
            if slope < 0:
              left_fit.append((slope, intercept))
            else:
              right_fit.append((slope, intercept))
          left_fit_average =np.average(left_fit, axis=0)
          right_fit_average = np.average(right_fit, axis =0)
          left_line =make_coordinates(image, left_fit_average)
          right_line = make_coordinates(image, right_fit_average)
  
          return np.array([left_line, right_line])



def canny_edge(image):
         gray_conversion= cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
         blur_conversion = cv2.GaussianBlur(gray_conversion, (5,5),0)
         canny_conversion = cv2.Canny(blur_conversion, 50,150)
         return canny_conversion




def show_lines(image, lines):
          line_image = np.zeros_like(image)
          if lines is not None:
            for line in lines:
              x1, y1, x2, y2 = line.reshape(4)
              cv2.line(line_image, (x1, y1), (x2, y2), (255,0,0), 10)
          return line_image


def reg_of_interest(image):
          image_height = image.shape[0]
          polygons = np.array([[(200, image_height), (1100, image_height), (550, 250)]])
          image_mask = np.zeros_like(image)
          cv2.fillPoly(image_mask, polygons, 255)
          masking_image = cv2.bitwise_and(image,image_mask)
          return masking_image
cap = cv2.VideoCapture("resources/road-marking/test2.mp4")


while(cap.isOpened()):
            _, frame = cap.read()
            canny_image = canny_edge(frame)
            cropped_canny = reg_of_interest(canny_image)
            lines = cv2.HoughLinesP(cropped_canny, 2, np.pi/180, 100,             np.array([]), minLineLength=40,maxLineGap=5)
            averaged_lines = average_slope_intercept(frame, lines)
            line_image = show_lines(frame, averaged_lines)
            combo_image = cv2.addWeighted(frame, 0.8, line_image, 1, 1)
            cv2.imshow("result", combo_image)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
cap.release()
cv2.waitKey(0)
cv2.destroyAllWindows()<jupyter_output>/home/dongchan/.local/lib/python3.8/site-packages/numpy/lib/function_base.py:380: RuntimeWarning: Mean of empty slice.
  avg = a.mean(axis)
/home/dongchan/.local/lib/python3.8/site-packages/numpy/core/_methods.py:170: RuntimeWarning: invalid value encountered in double_scalars
  ret = ret.dtype.type(ret / rcount)
{'fraction_non_alphanumeric': 0.11491477272727273, 'fraction_numerical': 0.037073863636363634, 'mean_word_length': 3.7574324324324326, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '102144', 'license': 'Apache-2.0', 'n_tokens_mistral': 2627, 'n_tokens_neox': 2438, 'n_words': 482, 'path': 'ComputerVision/05_findind_road_marking_using_opencv.ipynb', 'repo_name': 'dongchan-year/SelfDrivingCarEngineer', 'size': '7040'}