我如何在实时相机中使用帧数计算非零像素的数量?它可以与图像一起使用,但在框架中不使用。我使用OpenCV
import cv2
import pickle
import cvzone
import numpy as np
#video feed
cap = cv2.VideoCapture(0)
with open("CarParkPos", "rb") as f:
posList = pickle.load(f)
width, height = 100, 50
def checkParkingSpace(frameProcess):
for pos in posList:
x,y = pos
frameCrop = frame[y:y+height, x:x+width]
cv2.imshow(str(x*y), frameCrop)
count = cv2.countNonZero(frameCrop)
cvzone.putTextRect(frame, "asd", (x,y+height-3), scale = 1, thickness=2, offset=0)
CV2.Countnonzero在计数现场摄像机中计数像素时似乎不起作用,但是使用视频文件时可以正常工作。
#converted rgb camera to black and white
while True:
ret, frame = cap.read()
frameGray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
frameBlur = cv2.GaussianBlur(frameGray, (3,3), 1)
frameThreshold = cv2.adaptiveThreshold(frameBlur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY_INV, 25, 16)
frameMedian = cv2.medianBlur(frameThreshold,5)
kernel = np.ones((1,1), np.uint8)
frameDilate = cv2.dilate(frameMedian,kernel, iterations = 1)
checkParkingSpace(frameDilate)
#writing the rectangle shapes in camera
for pos in posList:
cv2.rectangle(frame, pos, (pos[0] + width, pos[1] + height), (0, 200, 0), 2)
cv2.imshow("Frame", frame)
cv2.waitKey(1)
我不完全了解所有代码,因为我只是看了一个教程。我要做的就是视频 https://wwww.youtube。 com/watch?v = caknqlcmiyi ,但是我不想使用视频文件,而要使用实时相机。
import cv2
import pickle
import cvzone
import numpy as np
#video feed
cap = cv2.VideoCapture(0)
with open("CarParkPos", "rb") as f:
posList = pickle.load(f)
width, height = 100, 50
def checkParkingSpace(frameProcess):
for pos in posList:
x,y = pos
frameCrop = frame[y:y+height, x:x+width]
cv2.imshow(str(x*y), frameCrop)
count = cv2.countNonZero(frameCrop)
cvzone.putTextRect(frame, "asd", (x,y+height-3), scale = 1, thickness=2, offset=0)
cv2.countNonZero doesn't seem to work when counting pixels in a live camera, but it works fine when using a video file.
#converted rgb camera to black and white
while True:
ret, frame = cap.read()
frameGray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
frameBlur = cv2.GaussianBlur(frameGray, (3,3), 1)
frameThreshold = cv2.adaptiveThreshold(frameBlur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY_INV, 25, 16)
frameMedian = cv2.medianBlur(frameThreshold,5)
kernel = np.ones((1,1), np.uint8)
frameDilate = cv2.dilate(frameMedian,kernel, iterations = 1)
checkParkingSpace(frameDilate)
#writing the rectangle shapes in camera
for pos in posList:
cv2.rectangle(frame, pos, (pos[0] + width, pos[1] + height), (0, 200, 0), 2)
cv2.imshow("Frame", frame)
cv2.waitKey(1)
I don't fully understand all the codes since I just watched a tutorial. What I'm trying to do is exactly the one in the video https://www.youtube.com/watch?v=caKnQlCMIYI , but instead of using video file I want to use a live camera.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我所做的是框架巧妙的框架,然后计算非黑色像素。
What I did was canny the frame then count the non-black pixel.