-2

I have a problem with the code below. After compilation, the message "ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera (objectpoints, framepoints, fsize, None, None) appears TypeError: function takes exactly 2 arguments (3 given) ". I want the image to be taken in real time from my webcam

import numpy as np
import cv2


objp = np.zeros((9 * 6, 3), np.float32)
objp[:, :2] = np.mgrid[0:6, 0:9].T.reshape(-1, 2)
size = (9, 6)

objectpoints = []
framepoints = []

cv2.namedWindow("video preview")
vc = cv2.VideoCapture(0)

fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (1080, 720))

if vc.isOpened():
    rval, frame = vc.read()
else:
rval = False

while rval:
    cv2.imshow("preview", frame)
    rval, frame = vc.read()
    ret, corners = cv2.findChessboardCorners(frame, size, None)
    cv2.drawChessboardCorners(frame, (9, 6), corners, ret)
    print(ret)
    print(frame.shape[::-1])
    fsize = frame.shape[::-1]

    if ret == True:
        criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
    objectpoints.append(objp)
    corners2 = cv2.cornerSubPix(frame, corners, (11, 11), (-1, -1), criteria)
    framepoints.append(corners2)

ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objectpoints, framepoints, fsize, None, None)
print('ret:', ret)
print('mtx:', mtx)
print('dist:', dist)
print('rvecs:', rvecs)
print('tvecs:', tvecs)

out.write(frame)

key = cv2.waitKey(20)
if key == 27:
    break

vc.release()
out.release()
cv2.destroyWindow("preview")`
sto0osik
  • 11
  • 1
  • 2
  • 1
    You haven't shown the code. – Daniel Roseman Aug 13 '19 at 12:29
  • 1
    Please post all (relevant) code and the full stacktrace. Check [\[SO\]: How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) or [\[SO\]: How to create a Minimal, Reproducible Example (reprex (mcve))](https://stackoverflow.com/help/mcve). – CristiFati Aug 13 '19 at 12:33

1 Answers1

5

I run into the same problem and found the problem:

You pass fsize as the img size parameter which expects a 2 element tuple (width,height)

But you read fsize from an RGB img which results in (width,height,3) for the 3 colour channels. (If you print(fsize) you will see that)

So you pass a 3-tuple where a 2-tuple is expected which results in the error

change your lines from:

fsize = frame.shape[::-1]

to:

fsize = frame.shape[1::-1]

and it should work.

You could also convert frame to grayscale by:

frame = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)

which might work better with the chessboardcornerDetector (in the example https://docs.opencv.org/4.1.1/dc/dbb/tutorial_py_calibration.html it is used that way)

I hope this helps.

Leaderchicken
  • 171
  • 1
  • 2