2013-10-31 100 views
0

我想在opencv中使用python進行立體聲校準。我正在使用python 2.7.1版和openCV 2.4.2版當我運行代碼時,出現錯誤提示需要解壓縮的值太多。下面是我的代碼:opencv(python立體相機)中的攝像機校準。錯誤:太多的值來解壓

objpts=[] 
objPoints = [] 
pointCounts = hor*ver 
R = [8] 
T = [8] 
E=[] 
F=[] 
R1 = [] 
R2 = [] 
P1=[] 
P2 =[] 
objectPoints = [] 
imgPoints1 = [] 
imgPoints2 = [] 
imagePoints2 = [] 
imagePoints1 = [] 

while ((len(imagePoints1) < hor*ver) and (len(imagePoints2)< hor*ver)): 

    ret,imgr = webCamHndlr_r.read(0) 
    ret,imgl = webCamHndlr_l.read(1) 
    grey_imgr = cv2.cvtColor(imgr, cv.CV_BGR2GRAY) 
    grey_imgl = cv2.cvtColor(imgl, cv.CV_BGR2GRAY) 
    ret, cornersr =cv2.findChessboardCorners(grey_imgr,dims) 
    cv2.drawChessboardCorners(grey_imgr,dims,cornersr,0) 
    ret, cornersl =cv2.findChessboardCorners(grey_imgl,dims) 
    cv2.drawChessboardCorners(grey_imgl,dims,cornersl,0) 
    cv2.imshow("chessboard", grey_imgr) 
    cv2.imshow("chessboard1", grey_imgl) 

    objPoints = np.zeros((hor*ver,3), np.float32) 
    objPoints[:,:2] = np.mgrid[0:hor,0:ver].T.reshape(-1,2) 

    if cv.WaitKey(-1) == 32: 

      imagePoints1.append(cornersl) 
      imagePoints2.append(cornersr) 
      print len(imagePoints1) 
      objectPoints.append(objPoints) 
      cv2.imwrite("./test_images/img_r"+str(i)+".jpg",imgr) 
      cv2.imwrite("./test_images/img_l"+str(i)+".jpg",imgl) 
      i = i+1; 


      if cv2.waitKey(10) == 27: 
       break 
objectPoints = [np.asarray(x) for x in objectPoints] 
imagePoints1 = [np.asarray(x) for x in imagePoints1] 
imagePoints2 = [np.asarray(x) for x in imagePoints2] 

if(len(imagePoints1[0])== len(imagePoints2[0]) == len(objectPoints[0]) == len(objectPoints)== len(imagePoints2) == len(imagePoints1)) : print len(imagePoints1[0]) 

     cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2, R, T, E, F = cv2.stereoCalibrate(objectPoints, imagePoints1, imagePoints2, (320,240)) 
     print R 

     cv.StereoRectify(cameraMatrix1, cameraMatrix2, distCoeffs1, distCoeffs2,(imgr.width,imgr.height), R, T, R1, R2, P1, P2, Q, CV_CALIB_ZERO_DISPARITY, -1, (0, 0)) 
     print Q 
     np.savetxt('Q_mat.txt',Q) 

,我得到的錯誤是:

Traceback (most recent call last): File "depth_estimation.py", line 82, in cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2, R, T, E, F = cv2.stereoCalibrate(objectPoints, imagePoints1, imagePoints2, (320,240)) ValueError: too many values to unpack

我給objectpoints陣列每一個又是一個點的數組。

回答

2

這是因爲返回一個更多的值。正如documentationcv2.stereoCalibrate回報說

retval, cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2, R, T, E, F 

,這意味着你的代碼應該是

retval,cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2, R, T, E, F = cv2.stereoCalibrate(objectPoints, imagePoints1, imagePoints2, (320,240)) 

,而不是僅僅

cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2, R, T, E, F = cv2.stereoCalibrate(objectPoints, imagePoints1, imagePoints2, (320,240)) 
+0

Mailerdaimon非常感謝,它的工作 – katri

+0

很高興我能幫上忙。你能接受答案,以便每個人都可以看到你的問題已被回答? – Mailerdaimon

+0

mailerdaimon,似乎有一個更多的錯誤,在stereoRectify中我得到一個錯誤 cv.StereoRectify(c1,c2,d1,d2,(320,240),r,t,r1,r2,p1,p2,q,cv。 CV_CALIB_ZERO_DISPARITY,-1,(0,0)) cv2.error:所有矩陣必須具有相同的數據類型 我已將R,T和其他向量更改爲矩陣 r = cv.fromarray(R) t = cv.fromarray(T) D1 = cv.fromarray(distCoeffs1) D2 = cv.fromarray(distCoeffs2) R1 = cv.CreateMat(r.width,r.height,cv.CV_8FC1)等人在類似的方式 任何建議將是有用的。謝謝 – katri

0

使用CV_64F代替CV_8FC1會解決這個問題:

cv.CreateMat(r.width,r.height, cv.CV_8FC1)