我如何在实时相机中使用帧数计算非零像素的数量?它可以与图像一起使用,但在框架中不使用。我使用OpenCV

发布于 2025-02-02 15:21:51 字数 1572 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回首观望 2025-02-09 15:21:51
for pos in posList:
    x,y = pos

    frameCrop = frame[y:y+height, x:x+width]
    cv2.imshow(str(x*y), frameCrop)
    count = cv2.countNonZero(cv2.Canny(frameCrop, 100, 200))
    cvzone.putTextRect(frame, str(count), (x,y+height-3), scale = 1, thickness=2, offset=0)

我所做的是框架巧妙的框架,然后计算非黑色像素。

for pos in posList:
    x,y = pos

    frameCrop = frame[y:y+height, x:x+width]
    cv2.imshow(str(x*y), frameCrop)
    count = cv2.countNonZero(cv2.Canny(frameCrop, 100, 200))
    cvzone.putTextRect(frame, str(count), (x,y+height-3), scale = 1, thickness=2, offset=0)

What I did was canny the frame then count the non-black pixel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文