opencv 为什么Python会给出错误cvtColor missing src [已关闭]

dw1jzc5e  于 2022-11-15  发布在  Python
关注(0)|答案(1)|浏览(166)

**已关闭。**此问题为not reproducible or was caused by typos。目前不接受答案。

这个问题是由一个打字错误或一个无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
7天前关闭。
Improve this question
我正在创建一个警报系统,当你按t键时,它会打开警报,如果检测到太多的移动,它会发出警报。当我按t键激活警报时,它会发出以下错误:

frame_bw = cv2.cvtColor(frame = cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.6.0) :-1: error: (-5:Bad argument) in function 'cvtColor'
> Overload resolution failed:
>  - cvtColor() missing required argument 'src' (pos 1)
>  - cvtColor() missing required argument 'src' (pos 1)

有人知道发生了什么吗?
下面是我代码:

import winsound
import threading
import cv2
import imutils

cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)

cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 480)

_, start_frame = cap.read()
start_frame = imutils.resize(start_frame, width=500)
start_frame = cv2.cvtColor(start_frame , cv2.COLOR_BGR2GRAY)
start_frame = cv2.GaussianBlur(start_frame, (21, 21), 0)

alarm = False
alarm_mode = False
alarm_counter = 0

def beep_alarm():
    global alarm
    for _ in range(5):
        if not alarm_mode:
            break
        print("ALARM!")
        winsound.Beep(2500, 1000)
    alarm = False

while True:

    _, frame = cap.read()
    frame = imutils.resize(frame, width=500)

    if alarm_mode:
        frame_bw = cv2.cvtColor(frame = cv2.COLOR_BGR2GRAY)  
        frame_bw = cv2.GaussianBlur(frame_bw, (5, 5), 0)

        difference = cv2.absdiff(frame_bw, start_frame)
        threshold = cv2.threshold(difference, 25, 255, cv2.THRESH_BINARY)[1]
        start_frame = frame_bw

        if threshold.sum() > 300:
            alarm_counter +=1
        else:
            if alarm_counter > 0:
                alarm_counter -=1
        
        cv2.imshow("Cam", threshold)
    else:
        cv2.imshow("Cam", frame)

    if alarm_counter > 20:
        if not alarm:
            alarm = True
            threading.Thread(target=beep_alarm).start()

    key_pressed = cv2.waitKey(30)
    if key_pressed == ord("t"):
        alarm_mode = not alarm_mode
        alarm_counter = 0
    if key_pressed == ord("q"):
        alarm_mode = False
        break

cap.release()
cv2.destroyAllWindows()
yacmzcpb

yacmzcpb1#

请检查此行:

frame_bw = cv2.cvtColor(frame = cv2.COLOR_BGR2GRAY)

它应该是:

frame_bw = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

相关问题