2017-01-30 44 views
1

我试图创建一个使用python和OpenCV的驾驶辅助系统。我使用了一些二进制阈值来让车道线变白。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] 
+0

你怎么想的最后一个位置? (一)在每一帧?或(b)在视频流的结尾处? –

+0

在每一帧(实况)我想使用白线的最后一个已知的X值 – Ingmar05

回答

2

可以使用numpy模块的nonzero()功能。这给你非零像素的诱导,对应于你的阈值图像中的白色像素。然后您可以使用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]] 
+0

它肯定会工作:D –

+0

它适用于当我用thresholding但我现在有一个裁剪功能之前:'leftframe = thresh [700:800,250:550]',当我尝试在左边框中使用它'whites = np.nonzero(leftframe)'我在打印行中遇到错误IndexError:index -1超出轴0大小为0' – Ingmar05

+0

我可以定义非零()将要查看的区域吗? – Ingmar05

相关问题