类型错误:找不到必需的参数“mat”(位置 2)

par*_*neh 8 python opencv cv2

我有下面的代码和 cv2 。此代码是从https://github.com/dipakkr/3d-cnn-action-recognition下载的。我想使用 cv2.imshow 来可视化它获得的视频的帧。但我收到以下错误。问题是什么?我怀疑这段代码是否真的能够读取视频,因为输出是一个零数组。

def video3d(self, filename, color=False, skip=True):

        cap = cv2.VideoCapture(filename)
        #ret, frame=cap.read()
        #cv2.imshow('frame', frame)
        nframe = cap.get(cv2.CAP_PROP_FRAME_COUNT) #Returns the specified VideoCapture property  ,,Number of frames in the video file

        print (nframe, "nframe")

        if skip:
            frames = [x * nframe / self.depth for x in range(self.depth)]
            print (frames, "frameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeees")

        else:
            frames = [x for x in range(self.depth)]
            print (frames, "frameseeeeeeeeeeeeeeeeeeeeeeeeeeeeeee2")

        framearray = []

        for i in range(self.depth):
            cap.set(cv2.CAP_PROP_POS_FRAMES, frames[i])  #Sets a property in the VideoCapture. ,,0-based index of the frame to be decoded/captured next.
            ret, frame = cap.read()
            cv2.imshow(frame)
            print(ret, "reeeeeeeeeeeeeeeeettttttttt")
            print(frame ,"frame issssssssssss:")
            frame = cv2.resize(frame, (self.height, self.width))
            print(frame, "frame222 isssssssssssssss")
            #cv2.imshow(frame)
            if color:
                framearray.append(frame)
            else:
                framearray.append(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY))

        cap.release()
        return np.array(framearray)


X.append(vid3d.video3d(v_file_path, color=color, skip=skip))
Run Code Online (Sandbox Code Playgroud)

错误:

    main()
  File "3dcnn.py", line 151, in main
    args.output, args.color, args.skip)
  File "3dcnn.py", line 103, in loaddata
    X.append(vid3d.video3d(v_file_path, color=color, skip=skip))
  File "/home/gxa131/Documents/final_project_computationalintelligence/3d-cnn-action-recognition/videoto3d.py", line 34, in video3d
    cv2.imshow(frame)
TypeError: Required argument 'mat' (pos 2) not found
Run Code Online (Sandbox Code Playgroud)

A K*_*ger 14

第一个参数cv2.imshow是窗口名称,因此它认为第二个输入mat(图像)丢失。如果您不想命名窗口,您可以只提供一个空字符串作为第一个输入参数。

cv2.imshow('', frame) 
Run Code Online (Sandbox Code Playgroud)

  • `cv2.imshow` 后面需要跟一个 [`waitKey`](https://docs.opencv.org/2.4/modules/highgui/doc/user_interface.html#waitkey) 才能显示图像,所以您可以在“cv2.imshow('',frame)”之后添加“cv2.waitKey(1)”。 (2认同)

Nic*_*ais 8

cv2没有找到“mat”(矩阵),因为您将图像作为第一个参数传递,但第一个参数应该是窗口名称。

cv2.imshow(winname, mat)
Run Code Online (Sandbox Code Playgroud)

在大多数情况下,您不关心窗口名称,因此使用:

cv2.imshow('', frame)
cv2.waitKey(0)
Run Code Online (Sandbox Code Playgroud)

这是更改窗口名称时的结果:

import numpy as np
import cv2

image = np.full((300, 300, 3), 255).astype(np.uint8)

cv2.putText(image, 'some picture', (20, 60),
            cv2.FONT_HERSHEY_SIMPLEX, 1, [0, 0, 0])

cv2.imshow('custom window name', image)
cv2.waitKey(0)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

您可能想要更改窗口名称的原因之一是在不同的窗口中一次绘制许多图片。