要基于灰度处理识别黄颜色和灰颜色的地图,并描绘灰色赛道的赛道边缘线和中心位置点,可以按照以下步骤进行操作:

  1. 导入必要的库和模块:
import cv2
import numpy as np
  1. 读取地图图像并转换为灰度图像:
img = cv2.imread('map.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  1. 设置黄色和灰色的阈值范围:
lower_yellow = np.array([20, 100, 100], dtype=np.uint8)
upper_yellow = np.array([30, 255, 255], dtype=np.uint8)

lower_gray = np.array([0, 0, 0], dtype=np.uint8)
upper_gray = np.array([180, 50, 150], dtype=np.uint8)
  1. 对图像进行颜色过滤,提取黄色和灰色部分:
yellow_mask = cv2.inRange(img_hsv, lower_yellow, upper_yellow)
gray_mask = cv2.inRange(img_hsv, lower_gray, upper_gray)
  1. 对灰色部分进行形态学处理,以去除噪点:
kernel = np.ones((5, 5), np.uint8)
gray_mask = cv2.morphologyEx(gray_mask, cv2.MORPH_OPEN, kernel)
  1. 寻找灰色赛道的边缘线:
edges = cv2.Canny(gray_mask, 50, 150)
  1. 根据边缘线,找到灰色赛道的中心位置点:
lines = cv2.HoughLinesP(edges, 1, np.pi/180, threshold=100, minLineLength=100, maxLineGap=10)
center_points = []
for line in lines:
    x1, y1, x2, y2 = line[0]
    center_x = int((x1 + x2) / 2)
    center_y = int((y1 + y2) / 2)
    center_points.append((center_x, center_y))
  1. 绘制灰色赛道的边缘线和中心位置点:
for line in lines:
    x1, y1, x2, y2 = line[0]
    cv2.line(img, (x1, y1), (x2, y2), (0, 255, 0), 2)

for point in center_points:
    cv2.circle(img, point, 5, (0, 0, 255), -1)
  1. 显示结果图像:
cv2.imshow('Result', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

以上是一个基本的实现流程,具体的阈值和参数可以根据实际情况进行调整。

OpenCV 灰度图像处理识别黄灰地图赛道并绘制边缘线和中心点

原文地址: https://www.cveoy.top/t/topic/o73M 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录