【问题标题】:TypeError: Required argument 'mat' (pos 2) not foundTypeError:找不到所需的参数“mat”(位置 2)
【发布时间】:2019-05-10 07:45:55
【问题描述】:

我有下面的代码和 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))

错误:

    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

【问题讨论】:

    标签: python opencv cv2


    【解决方案1】:

    cv2.imshow 的第一个参数是窗口名称,因此它将第二个输入 mat(图像)视为缺失。如果你不想给窗口命名,你可以给一个空字符串作为第一个输入参数。

    cv2.imshow('', frame) 
    

    【讨论】:

    • 实际上你建议的工作但只出现没有图片的窗口。你知道可能是什么原因吗/
    • cv2.imshow 后面需要跟waitKey 才能显示图像,因此您可以在cv2.imshow('', frame) 之后添加cv2.waitKey(1)
    【解决方案2】:

    我知道这个问题已经回答了,但想给它添加一个通用的答案!

    当我们错过向被调用函数提供第二个参数时,基本上python通过了这个错误。

    当出现此类错误时,只需转到输出部分错误指向的行号并检查您调用的函数是否传递了所有参数。

    【讨论】:

    • 这根本不是一个相关的答案,你应该删除它。
    【解决方案3】:

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

    cv2.imshow(winname, mat)
    

    在大多数情况下,您并不关心窗口名称,所以使用这个:

    cv2.imshow('', frame)
    cv2.waitKey(0)
    

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

    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)
    

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

    【讨论】:

      【解决方案4】:

      我有同样的问题,通过将图像的完整路径提供给imread()来解决

      【讨论】:

        猜你喜欢
        • 2019-06-20
        • 2021-04-09
        • 2017-05-26
        • 2015-10-16
        • 2019-09-03
        • 2011-09-22
        • 2020-08-13
        • 2015-05-03
        • 1970-01-01
        相关资源
        最近更新 更多