OpenCV - 如何获取视频中白色像素的最后位置?

时间:2017-01-30 17:10:28

标签: python opencv image-processing computer-vision

我正在尝试使用python和OpenCV创建一个驾驶辅助系统。我使用了一些二进制阈值来使车道线变白。

如何获取白色像素的最后X值?我只找到了检测面部和线条的指南。

以下是Video

当前代码:

#Video Feed
ret, frame = cap.read()

#Grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

#thresholding
thresh = cv2.threshold(gray, 140, 255, cv2.THRESH_BINARY)[1]

1 个答案:

答案 0 :(得分:2)

您可以使用nonzero()模块的numpy功能。这会为您提供非零像素的诱导,对应于阈值图像中的白色像素。然后,您可以使用whites[0]访问x坐标。例如,最高x和y坐标中最后一个白色像素的值为thresh[whites[0][len(whites[0])-1]][whites[1][len(whites[1])-1]]

import numpy
import cv2

#Video Feed
ret, frame = cap.read()

#Grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

#thresholding
thresh = cv2.threshold(gray, 140, 255, cv2.THRESH_BINARY)[1]

# get indices of all white pixels
whites = numpy.nonzero(thresh)

# print the last white pixel in x-axis, 
# which is obviously white
print thresh[whites[0][len(whites[0])-1]][whites[1][len(whites[1])-1]]