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

2024-05-14 03:39:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用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]

Tags: 系统video指南二进制阈值像素cv2frame
1条回答
网友
1楼 · 发布于 2024-05-14 03:39:05

您可以使用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]]

相关问题 更多 >