def lane_detection(self, image_bgr): # Convert to HSV image_hsv = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2HSV) # Mask global mask_lower_threshold, mask_upper_threshold mask = cv2.inRange(image_hsv, mask_lower_threshold, mask_upper_threshold) # Publish mask image if self.mask_pub.get_num_connections() > 0: self.mask_pub.publish( self.bridge.cv2_to_imgmsg(mask, encoding='passthrough')) # Canny edge detection edges = cv2.Canny(mask, 200, 400) # Clear the top half of the edges image edges[0:int(float(edges.shape[0]) / 2.), :] = 0. # Publish edges image if self.edges_image_pub.get_num_connections() > 0: self.edges_image_pub.publish( self.bridge.cv2_to_imgmsg(edges, encoding='passthrough')) # Detect line segments line_segments_tmp = cv2.HoughLinesP(edges, 1, np.pi / 180., 10, None, 8, 4) if line_segments_tmp is None: print('') return [None, None] # Remove extra array layer from line_segments_tmp line_segments = [] for line_segment in line_segments_tmp: line_segments.append(line_segment[0]) # Publish line segments image if self.line_segments_image_pub.get_num_connections() > 0: line_segments_image = plot_line_segments(image_bgr, line_segments) self.line_segments_image_pub.publish( self.bridge.cv2_to_imgmsg(line_segments_image, encoding='bgr8')) # Combine line segments [left_lane, right_lane] = functions.average_slope_intercept(line_segments) if self.lanes_image_pub.get_num_connections() > 0: lanes_image = plot_lanes(image_bgr, left_lane, right_lane) self.lanes_image_pub.publish( self.bridge.cv2_to_imgmsg(lanes_image, encoding='bgr8')) return [left_lane, right_lane]
""" canny_frm = functions.canny(frame) # 4. cropped frame/image with the mask cropped_frm = functions.region_of_interest(canny_frm) # 5. Apply hough transform (r , teta) : shall be small for better precision # HoughLinesP parameters : r = 2 pixels , teta=1 degree, threshold = 100, empty array, ... lines = cv2.HoughLinesP(cropped_frm, 2, np.pi / 180, 100, np.array([]), minLineLength=40, maxLineGap=5) # 6. optimizing # average multiples slope in the lines to a single slope line averaged_lines = functions.average_slope_intercept(frame, lines) # display line frame/image line_frm = functions.display_lines(frame, averaged_lines) # merged the gradient frame/image into the colored frame/image combo_frm = cv2.addWeighted(frame, 0.8, line_frm, 1, 1) # Creates a window to display the line frame/image cv2.imshow('result', combo_frm) # Display the frame/image during 1msn and abort the system if the a keyboard is pressed if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()