#!/usr/bin/env python3
 
# 识别的是中线为白色
 
import cv2
import numpy as np
# center定义
center = 320
# 打开摄像头,图像尺寸640*480(长*高),opencv存储值为480*640(行*列)
cap = cv2.VideoCapture(0)
while (1):
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    # 大津法二值化
    retval, dst = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU)
    #cv2.imshow("dst", dst)
    # 膨胀,白区域变大
    dst = cv2.dilate(dst, None, iterations=2)
    #cv2.imshow("dst2", dst)
    # # 腐蚀,白区域变小 #
    dst = cv2.erode(dst, None, iterations=6)
    cv2.imshow("dst3", dst)
    # 单看第400行的像素值v
    color = dst[400]
    # 找到白色的像素点个数,如寻黑色,则改为0
    white_count = np.sum(color == 0)
    # 找到白色的像素点索引,如寻黑色,则改为0
    white_index = np.where(color == 0)
    # 防止white_count=0的报错
    if white_count == 0:
        white_count = 1
    # 找到黑色像素的中心点位置
    # 计算方法应该是边缘检测,计算黑色边缘的位置和/2,即是白色的中央位置。
    center = (white_index[0][white_count - 1] + white_index[0][0]) / 2
    # 计算出center与标准中心点的偏移量,因为图像大小是640,因此标准中心是320,因此320不能改。
    direction = center - 320
    param = str(direction)+'\r\n'
    print(direction)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()  # 释放cap
cv2.destroyAllWindows()  # 销毁所有窗口


声明:实验室学弟学妹们用代码时,要注意具体赛道情况,本代码适用于如下图所示的赛道(两条黑线构成的赛道)