TypeError: Required argument 'mat' (pos 2) not found
Asked Answered
M

5

18

I have the code below with cv2 . This code is downloaded from https://github.com/dipakkr/3d-cnn-action-recognition. I want to use cv2.imshow to visualize the frames of the video it get. But I get the following error. What is the problem? I am doubtful of whether this code is really able to read the video as what is returns as the output is an array of zeros.

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))

Error:

    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
Malcah answered 9/12, 2018 at 0:4 Comment(0)
T
26

The first argument to cv2.imshow is the window name, so it's considering the second input mat (the image) as missing. If you don't want to name the window, you can just give an empty string as the first input parameter.

cv2.imshow('', frame) 
Titleholder answered 9/12, 2018 at 3:18 Comment(2)
Actually what you suggested works but only window that has no pics appears. Do you know what could be the reason/Malcah
A cv2.imshow needs to be followed by a waitKey for the image to be displayed, so you could add cv2.waitKey(1) right after the cv2.imshow('', frame).Titleholder
H
9

cv2 didn't the find the "mat" (matrix), because you passed the image as the first argument, but the first argument is supposed to be the window name.

cv2.imshow(winname, mat)

In most cases, you don't care about the window name, so use this:

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

This is the result when you change the window name:

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)

enter image description here

One of the reasons you might want to change window names is to plot many pictures at once, in different windows.

Hedgepeth answered 24/5, 2020 at 15:11 Comment(0)
V
0

I know that this question is already answered but would like to add a generic ans to it!

Basically python through this error, when we miss to provide the 2nd parameter to the called function.

When such a error comes just go to the line number pointed in error at output section and check that the function you have called, have all the parameter passed.

Vaporimeter answered 13/7, 2019 at 21:29 Comment(1)
That's not at all a relevant answer and you should delete it.Hedgepeth
E
0

I had the same problem, Solved by giving the full path of the image to imread()

Expiable answered 16/5, 2021 at 8:42 Comment(0)
S
-1

Another suggestion is to use plt.imshow() instead of cv2_imshow or cv2.imshow:

from matplotlib import pyplot as plt

    def display_frame_in_notebook(frame):
    
        # Convert the image from BGR to RGB (since OpenCV loads images 
        in BGR format)
    
        frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        plt.imshow(frame_rgb)
        plt.axis('off')  # Hide the axis
        plt.show()

Then you should be able to change from:

cv2.imshow('frame', frame) or cv2_imshow(frame)

to:

display_frame_in_notebook(frame).
Steele answered 17/7 at 21:49 Comment(1)
Honestly, just calling the function correctly seems like a much better option compared to pulling in yet another huge dependency (which is also unlikely to perform any better and carries a few snags along the way).Achromaticity

© 2022 - 2024 — McMap. All rights reserved.