如何检测OpenCV绘制的两条线或像素的中点?车道检测需求
Got it, let’s walk through how to find those midpoints for your lane detection system—this is a common step for lane keeping or lane change detection, so I’ve got a few solid approaches for you:
Since you’re using cv2.fillPoly() to draw the lanes, you either already have the vertex arrays for each lane polygon (like left_lane_vertices and right_lane_vertices) or you’ll need to extract the lane contours from the drawn image. Here’s how to handle both cases:
If you saved the fillPoly vertices:
You’re already halfway there—those vertex lists are your source of lane coordinates. Skip to step 2.
If you need to extract contours from the image:
First, isolate the lane lines from the background, then pull out their contours:
# Assume your image with drawn lanes is called lane_img gray = cv2.cvtColor(lane_img, cv2.COLOR_BGR2GRAY) # Threshold to turn lane pixels white (255) and background black (0) _, binary_lanes = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY) # Extract outer contours only contours, _ = cv2.findContours(binary_lanes, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # Filter contours to get left and right lanes (adjust based on your setup) # For example, sort contours by x-position to separate left/right contours_sorted = sorted(contours, key=lambda c: cv2.boundingRect(c)[0]) left_lane_contour = contours_sorted[0] right_lane_contour = contours_sorted[1]
Pick the method that fits your lane shape (straight vs. curved):
Method 1: Endpoint Midpoints (Great for Straight Lanes)
Find the top and bottom points of each lane, then compute their midpoints to draw a straight center line:
import numpy as np # Process left lane endpoints left_y = left_lane_contour[:, :, 1] left_top_idx = np.argmin(left_y) left_top = left_lane_contour[left_top_idx][0] # (x, y) left_bottom_idx = np.argmax(left_y) left_bottom = left_lane_contour[left_bottom_idx][0] # Process right lane endpoints right_y = right_lane_contour[:, :, 1] right_top_idx = np.argmin(right_y) right_top = right_lane_contour[right_top_idx][0] right_bottom_idx = np.argmax(right_y) right_bottom = right_lane_contour[right_bottom_idx][0] # Compute midpoints mid_top = ((left_top[0] + right_top[0]) // 2, (left_top[1] + right_top[1]) // 2) mid_bottom = ((left_bottom[0] + right_bottom[0]) // 2, (left_bottom[1] + right_bottom[1]) // 2)
Method 2: Sampled Midpoints (Better for Curved Lanes)
If your lanes are curved, sample points along the vertical axis and compute midpoints for each position to get a smooth center line:
# Get the full vertical range of your lanes y_min = min(left_y.min(), right_y.min()) y_max = max(left_y.max(), right_y.max()) # Sample every 20 pixels (adjust density as needed) sample_ys = np.arange(y_max, y_min, -20) mid_points = [] for y in sample_ys: # Find left lane x-coordinate near this y left_x_matches = left_lane_contour[np.abs(left_y - y) < 5] left_x = left_x_matches[0][0][0] if len(left_x_matches) > 0 else -1 # Find right lane x-coordinate near this y right_x_matches = right_lane_contour[np.abs(right_y - y) < 5] right_x = right_x_matches[0][0][0] if len(right_x_matches) > 0 else -1 if left_x != -1 and right_x != -1: mid_x = (left_x + right_x) // 2 mid_points.append( (mid_x, y) ) # Convert to numpy array for OpenCV drawing mid_points = np.array(mid_points, dtype=np.int32)
Now use OpenCV to visualize the midpoints/center line—perfect for lane change detection:
# Draw a smooth center line (for curved lanes) cv2.polylines(lane_img, [mid_points], isClosed=False, color=(0, 0, 255), thickness=3) # Or draw a bottom midpoint marker (great for tracking vehicle position relative to lane center) cv2.circle(lane_img, mid_bottom, radius=6, color=(0, 255, 0), thickness=-1)
- If you’re using polynomial fitting to generate your lane lines (instead of raw contours), skip contour extraction entirely! Just use your fit equations to calculate left/right x-values for any y, then average them to get midpoints. It’s way more precise.
- When filtering contours, add a minimum area check to ignore noise (e.g.,
if cv2.contourArea(contour) > 500). - For curved lanes, adjust the sample interval (the
20innp.arange) to make the center line smoother or faster to compute.
内容的提问来源于stack exchange,提问作者RalfiBahar

