尝试将 bgr 图像转换为灰度时出现错误

时间:2021-05-19 11:29:26

标签: opencv image-processing

我正在这里阅读视频:

VideoCap = cv2.VideoCapture("E:\Omar's Stuff\College related\Forth Year\Project\Kalman Filter 
                                                                      code\video_randomball.avi")
ret, frame = VideoCap.read()

并将帧发送到此处的检测功能:

# Detect object
centers = detect(frame,debugMode)

尝试在此处转换:

定义检测(帧,调试模式): # 将帧从 BGR 转换为灰色 #frame = frame.astype('uint8') 灰色 = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

并收到此错误:

<块引用>

OpenCV(3.4.2) c:\projects\opencv-python\opencv\modules\imgproc\src\color.hpp:253: 错误: (-215:断言失败)VScn::contains(scn) && VDcn::contains(dcn) && VDepth::contains(depth) in 函数 'cv::CvtHelper,struct cv::Set<1,-1,-1>,struct cv::Set<0,2,5>,2>::CvtHelper'

如何解决?

1 个答案:

答案 0 :(得分:0)

您在 cv2.VideoCapture() 方法中传递的视频文件路径有问题,因此请尝试更正路径,如果无法更正,则只需将视频文件与 python 脚本文件夹一起拖放即可工作。

这里是读取python脚本所在目录下的视频的示例代码

import cv2
import numpy as np

# Create a VideoCapture object and read from input file
cap = cv2.VideoCapture('tree.mp4')

# Check if camera opened successfully
if (cap.isOpened()== False):
print("Error opening video file")

# Read until video is completed
while(cap.isOpened()):
    
# Capture frame-by-frame
ret, frame = cap.read()
if ret == True:

    # Display the resulting frame
    cv2.imshow('Frame', frame)

    # Press Q on keyboard to exit
    if cv2.waitKey(25) & 0xFF == ord('q'):
    break

# Break the loop
else:
    break

# When everything done, release
# the video capture object
cap.release()

# Closes all the frames
cv2.destroyAllWindows()