def canny_edge(image):
# Grayscale
gray_conversion = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Gaussian 블러
blur_conversion = cv2.GaussianBlur(gray_conversion,(5,5), 0)
# Canny 엣지
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), (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
canny와 RoI가 동시에 실행한 모습
lines = cv2.HoughLinesP(roi_conversion, 1, np.pi/180, 100, minLineLength=40,maxLineGap=5)
def show_lines(image, lines):
lines_image = np.zeros_like(image)
if lines is not None :
for i in range(len(lines)):
for x1,y1,x2,y2 in lines[i]:
cv2.line(lines_image,(x1,y1),(x2,y2),(255,0,0),10) # 선 색 , 선굵기
return lines_image
def make_coordinates(image, line_parameters):
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)
# 기울기와 y절편을 가져올 수 있다.
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_avg =np.average(left_fit, axis= 0)
right_fit_avg = np.average(right_fit, axis=0)
left_line = make_coordinates(image, left_fit_avg)
right_line = make_coordinates(image, right_fit_avg)
return np.array( [[left_line, right_line]] )
6. 선이미지와 원본 합치기
combine_image = cv2.addWeighted(lanelines_image, 0.8, lines_image,1,1) # 비율
[OpenCV]YOLO object detection (0) | 2021.04.30 |
---|---|
OpenCV lane Detection 동영상 (0) | 2021.04.22 |
OpenCV 끊어진 윤곽선 연결 Contours (0) | 2021.04.19 |
OpenCV Trackbar를 이용한 Canny (0) | 2021.04.19 |
OpenCV filter를 이용하여 sharp한 이미지 (0) | 2021.04.19 |