例如,在这段代码中,我过滤了视频源以显示白色区域。我怎么知道他们的位置/坐标?(x,y)
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while(1):
_, frame = cap.read()
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# define range of white color in HSV
# change it according to your need !
lower_white = np.array([0,0,0], dtype=np.uint8)
upper_white = np.array([0,0,255], dtype=np.uint8)
# Threshold the HSV image to get only white colors
mask = cv2.inRange(hsv, lower_white, upper_white)
# Bitwise-AND mask and original image
res = cv2.bitwise_and(frame,frame, mask= mask)
cv2.imshow('frame',frame)
cv2.imshow('mask',mask)
cv2.imshow('res',res)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()
最佳答案
跟进Amitay的回答,你也可以使用OpenCV的函数findNonZero
。我不知道它与 numpy 的 nonzero
的实现方式有何不同,但如果给出相同的结果并且它更快(这对大循环或图像很有用)
import cv2
import numpy as np
import time
so=cv2.imread(your_image,0)
start1=time.clock()
coord=cv2.findNonZero(so)
end1=time.clock()
start2=time.clock()
coord2=np.nonzero(so)
end2=time.clock()
print("cv2.findNonZeros() takes "+str(end1-start1)+" seconds.")
print("np.nonzero() takes "+str(end2-start2)+" seconds.")
>>> cv2.findNonZeros() takes 0.003266 seconds.
>>> np.nonzero() takes 0.021132 seconds.
关于python - 我如何知道在 python 中使用 OpenCV 检测到的白色区域的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41919319/