[英]Efficient way to know if any pixel is white in image?
So basically I m trying to use OpenCV-Python to do motion detection.所以基本上我正在尝试使用 OpenCV-Python 进行运动检测。 I used this tutorial to do so and here is my code.
我使用本教程来做到这一点,这是我的代码。
import cv2
def diffImg(t0, t1, t2):
d1 = cv2.absdiff(t2, t1)
d2 = cv2.absdiff(t1, t0)
return cv2.bitwise_and(d1, d2)
cap = cv2.VideoCapture(0)
t = cv2.cvtColor(cap.read()[1], cv2.COLOR_BGR2GRAY)
tp = cv2.cvtColor(cap.read()[1], cv2.COLOR_BGR2GRAY)
tpp = cv2.cvtColor(cap.read()[1], cv2.COLOR_BGR2GRAY)
while cap.isOpened():
img = cv2.cvtColor(cap.read()[1], cv2.COLOR_BGR2GRAY)
img2 = diffImg(t,tp,tpp)
cv2.imshow("Motion", img2)
t=tp
tp=tpp
tpp=img
key = cv2.waitKey(10)
if key == 27 :
cv2.destroyAllWindows()
break
The I want to print on the console when there is a motion detection or not.当有运动检测与否时,我想在控制台上打印。 When there is a motion, there are white pixels in the input image.
当有运动时,输入图像中有白色像素。 But I don't know how to find white pixels in the input image.
但我不知道如何在输入图像中找到白色像素。 Can anyone tell me how to find if there are white pixels in the image returned by diffImg or not ?
谁能告诉我如何查找 diffImg 返回的图像中是否有白色像素?
You could take a look to the countNonZero
function fom OpenCV.您可以查看 OpenCV 的
countNonZero
函数。
Example provided by Baqir Khan: Baqir Khan 提供的示例:
if cv2.countNonZero(img2) > 29700:
print("Motion")
else:
print("No Motion")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.