立体相机校准错误

时间:2018-05-02 14:31:09

标签: opencv stereo-3d cv2 calibration

我正在尝试对2个USB摄像头进行立体声校准以执行深度贴图,但是由于这个错误并且我不知道如何解决。 我真的很感激谁能提供帮助。

 File "C:/Users/gaetano/stereoCalib.py", line 85, in <module>
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, 
gray.shape[::-1], None, None)

error: OpenCV(3.4.1) D:\Build\OpenCV\opencv- 
3.4.1\modules\calib3d\src\calibration.cpp:3384: error: (-215) nimages > 0 in 
function cv::calibrateCamera

(this error should have been solved using the following code) 



#before here i have collect picture from both the camera, than 
#grey conversion
i=0
while i < img_counter:
img = cv2.imread('colorRight_' + str(i) + '.jpg')
img2 = cv2.imread('colorLeft_' + str(i) + '.jpg')

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)

cv2.imwrite('greyRight_' + str(i) + '.jpg',gray)
cv2.imwrite('greyLeft_' + str(i) + '.jpg',gray2)
i += 1 



nx = 9
ny = 6
chess_imagesL = glob.glob('greyLeft_*.jpg')
chess_imagesR = glob.glob('greyRight_*.jpg')
for i in range(len(chess_imagesL)):
# Read in the image
chess_board_imageL = mpimg.imread(chess_imagesL[i])
chess_board_imageR = mpimg.imread(chess_imagesR[i])
# Convert to grayscale
# gray = cv2.cvtColor(chess_board_image, cv2.COLOR_RGB2GRAY)
# Find the chessboard corners
ret, cornersL = cv2.findChessboardCorners(chess_board_imageL, (nx, ny), 
None)
ret2, cornersR = cv2.findChessboardCorners(chess_board_imageR, (nx, ny), 
None)
# If found, draw corners
if ret == True:
    # Draw and display the corners
    cv2.drawChessboardCorners(chess_board_imageL, (nx, ny), cornersL, ret)
    cv2.drawChessboardCorners(chess_board_imageR, (nx, ny), cornersR, ret2)
    result_nameL = 'boardL'+str(i)+'.png'
    result_nameR = 'boardR'+str(i)+'.png'
    cv2.imwrite(result_nameL, chess_board_imageL)
    cv2.imwrite(result_nameR, chess_board_imageR)

criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)

## prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((6*9,3), np.float32)
objp[:,:2] = np.mgrid[0:9,0:6].T.reshape(-1,2)

objpointsL=[]
objpointsR=[]
imgpointsL=[]
imgpointsR=[]  


for i in range(len(chess_imagesL)):
# Read in the image
chess_board_imageL = mpimg.imread(chess_imagesL[i])
chess_board_imageR = mpimg.imread(chess_imagesR[i])

ret, cornersL = cv2.findChessboardCorners(chess_board_imageL, (nx, ny), 
None)
ret2, cornersR = cv2.findChessboardCorners(chess_board_imageR, (nx, ny), 
None)
# If found, draw corners
if ret == True:
    objpointsL.append(objp)
    objpointsR.append(objp)

    corners2L = cv2.cornerSubPix(gray,cornersL,(11,11),(-1,-1),criteria)
    corners2R = cv2.cornerSubPix(gray,cornersR,(11,11),(-1,-1),criteria)

    imgpointsL.append(corners2L)
    imgpointsR.append(corners2R)



    cv2.drawChessboardCorners(chess_board_imageL, (nx, ny), corners2L, ret)
    cv2.drawChessboardCorners(chess_board_imageR, (nx, ny), corners2R, ret2)
    result_nameL = 'boardL'+str(i)+'.png'
    result_nameR = 'boardR'+str(i)+'.png'
    cv2.imwrite(result_nameL, chess_board_imageL)
    cv2.imwrite(result_nameR, chess_board_imageR)

出现问题的部分

ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpointsL, imgpointsL, 
gray.shape[::-1], None, None)
ret2, mtx2, dist2, rvecs2, tvecs2 = cv2.calibrateCamera(objpointsR, 
imgpointsR, gray2.shape[::-1], None, None)
R = []
T = []
E = []
F = []
flags = cv2.CALIB_FIX_INTRINSIC
ret, mtx, dist, mtx2, dist2, R, T, E, F = cv2.stereoCalibrate(objpointsL, 
imgpointsL ,imgpointsR, mtx, dist, mtx2, dist2, gray.shape[::-1], None, 
None, None, None,flags = flags,
criteria = (cv2.TERM_CRITERIA_EPS, 30, 1e-6))

更新cv2.solvePnP

中的错误

使用您建议的代码,cameracalibration似乎有效。但我在下一步有问题,立体同步校准

文件&#34; C:/Users/gaetano/Desktop/sonido/CALIPROVA2.py" ;,第157行,在 ret,R,T = cv2.solvePnP(objpointsL,imgpointsL,mtx,dist,R,T,0,0)

TypeError:objectPoints不是一个numpy数组,也不是标量

R = []
T = []
E = []
F = []
flags = cv2.CALIB_FIX_INTRINSIC

ret, mtx, dist, mtx2, dist2, R, T, E, F = cv2.stereoCalibrate(objpointsL, 
imgpointsL ,imgpointsR, mtxL, distL, mtxR, distR,
grayL.shape[::-1], None, None, None, None,
flags = flags,
criteria = (cv2.TERM_CRITERIA_EPS, 30, 1e-6))



ret, R, T = cv2.solvePnP(objpointsL,imgpointsL,mtx,dist,R,T,0,0)
print(R)
print(T)

调整大小

调整大小时出错 SystemError:新样式的getargs格式,但参数不是元组

print(roi1)
print(roi2)
# crop the image
x, y, w, h = roi1
dst = dst[y:y + h, x:x + w]
#
dst2 = dst2[y:y + h, x:x + w]
dst = cv2.resize(dst, 0, dst, 2, 2, interpolation = cv2.INTER_LINEAR)

1 个答案:

答案 0 :(得分:1)

所以错误说:

  

错误:OpenCV(3.4.1)   d:\构建\的OpenCV \的OpenCV-3.4.1 \模块\ calib3d \ SRC \ calibration.cpp:3384:   错误:( - 215)nimages&gt;函数cv :: calibrateCamera中的0

这意味着您将空列表变量作为对象点或图像点传递。

在您的代码中,您有:

objpoints=[]
imgpoints=[]

然后只在行中使用:

#objpoints.append(objp)

并在行中:

ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)

这意味着两者都是空的!那些随后在函数中使用的2个列表中没有任何内容。在下一行:

ret2, mtx2, dist2, rvecs2, tvecs2 = cv2.calibrateCamera(objpoints, imgpoints2, gray2.shape[::-1], None, None)

在这里你使用存在且有点的imgpoints2,但它没有objpoints(因为它被注释掉了),你会得到类似的错误。

解决方案:

你必须决定你想要在这里实现什么。如果它是2个校准,则可能是一个以帧为输入的函数,并且所有过程都会更好。这样您就可以确保变量名称正确。

如果某项功能对您不利,那么您始终可以执行与imgpoints2imgpoints相同的功能,并且该变量将可以使用。此外,请确保取消注释objpoints.append(objp)以使该功能再次运行。

为了完整起见,这就是我对函数的意思:

def getObjPoints():
  objp = np.zeros((6*7,3), np.float32)
  objp[:,:2]=np.mgrid[0:7,0:6].T.reshape(-1,2)
  return objp

def calibrateCamWithFrame( frame ):
  objpoints=[getObjPoints]
  imgpoints=[]
  gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  ret, corners = cv2.findChessboardCorners(gray,(7,6),None)
  # if no corners where found return None
  if !ret:
    return None
  criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
  corners = cv2.cornerSubPix(gray, corners, (11,11), (-1,-1), criteria)
  imgpoints.append(corners)
  frameWithCorners=cv2.drawChessboardCorners(frame,(7,6), corners, ret)
  cv2.imshow('img',frameWithCorners)
  cv2.waitKey(500)
  return cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)

然后从外面打电话:

ret, mtx, dist, rvecs, tvecs = calibrateCamWithFrame(frame)
ret2, mtx2, dist2, rvecs2, tvecs2 = calibrateCamWithFrame(frame2)

<强>更新

由于您需要校准2个摄像头,并对代码进行适当的更改以识别它,因此解决方案是读取图像,找到角点并在主循环中追加数据。类似的东西:

nx = 9
ny = 6
imgpointsL = []
imgpointsR = []
objpointsL = []
objpointsR = []
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
objp = np.zeros((6*7,3), np.float32)
objp[:,:2] = np.mgrid[0:7,0:6].T.reshape(-1,2)

for i in range(img_counter):
  grayR = cv2.imread('colorRight_' + str(i) + '.jpg', cv2.CV_LOAD_IMAGE_GRAYSCALE )
  grayL = cv2.imread('colorLeft_' + str(i) + '.jpg', cv2.CV_LOAD_IMAGE_GRAYSCALE )

  retR, cornersR = cv2.findChessboardCorners(grayR, (nx, ny), None)
  if retR:
     cornersR = cv2.cornerSubPix(gray, cornersR, (11,11), (-1,-1), criteria)
     imgpointsR.append(cornersR)
     objpointsR.append(objp)

  retL, cornersL = cv2.findChessboardCorners(grayL, (nx, ny), None)

  if retL:
     cornersL = cv2.cornerSubPix(gray, cornersL, (11,11), (-1,-1), criteria)
     imgpointsL.append(cornersR)
     objpointsL.append(objp)

现在您可以校准每个相机,请注意我为每个相机使用imgpoints和objpoints ...这样您就可以确定尺寸匹配,因为不一定所有相机中的所有图像都会找到正确的角点。 / p>

然后你可以校准,确保你有图像:

  if len(imgpointsL) >0:
    retL, mtxL, distL, rvecsL, tvecsL = cv2.calibrateCamera(objpointsL, imgpointsL,grayL.shape[::-1], None, None)

  if len(imgpointsL) >0:
    retR, mtxR, distR, rvecsR, tvecsR = cv2.calibrateCamera(objpointsR, imgpointsR,grayR.shape[::-1], None, None)