2014-12-03 99 views
0

我在OpenCV的初學者,我試圖在運行教程代碼:http://docs.opencv.org/trunk/doc/py_tutorials/py_calib3d/py_table_of_contents_calib3d/py_table_of_contents_calib3d.htmlPython的姿態估計示例錯誤

我的代碼和問題:

import scipy.io 
import numpy as np 
import cv2 
import time 

# data comes from calibration program same as tutorial 
data=scipy.io.loadmat('distm.mat') 
dist=data['dist000'] 
mtx=data['mtx'] 
data=None 

#takes the corners in the chessboard 
def draw(img, corners, imgpts): 
    corner = tuple(corners[0].ravel()) 
    img = cv2.line(img, corner, tuple(imgpts[0].ravel()), (255,0,0), 5) 
    img = cv2.line(img, corner, tuple(imgpts[1].ravel()), (0,255,0), 5) 
    img = cv2.line(img, corner, tuple(imgpts[2].ravel()), (0,0,255), 5) 
    return img 

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) 

axis = np.float32([[3,0,0], [0,3,0], [0,0,-3]]).reshape(-1,3) 

# cam open and wait a second to deny black or dark images                    
cap = cv2.VideoCapture(2) 
ret, frame = cap.read() 
print "Checking camera read:"+str(ret) 
while ret ==False: 
    cap = cv2.VideoCapture(2) 
    ret, frame = cap.read() 
time.sleep(1) 

# takes live photos from camera 
for i in range(0,20): 
    print i 

    ret, img = cap.read() 
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 
    ret, corners = cv2.findChessboardCorners(gray, (7,6),None) 
    print ret 

    if ret == True: 

     corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria) 
     # 'corners2' returns "none" and below codes not working so I use 'corners' 
     print corners2 
     # Find the rotation and translation vectors. 
     rvecs, tvecs, inliers = cv2.solvePnPRansac(objp, corners, mtx, dist) 

     # project 3D points to image plane 
     imgpts, jac = cv2.projectPoints(axis, rvecs, tvecs, mtx, dist) 

     img = draw(img,corners,imgpts) # img returns None 
     print img      
     cv2.imshow('pose',img)   # error line 
     k = cv2.waitKey(0) & 0xff 

,並在提示錯誤「 cv2.imshow( '姿態',IMG)」爲:

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in 
cv::imshow, file ..\..\..\modules\highgui\src\window.cpp, line 269 
Traceback (most recent call last): File "<stdin>", line 1, in 
<module> File 
"C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", 
line 540, in runfile 
    execfile(filename, namespace) File "C:/Users/ACS/Documents/Python/Pose Estimation.py", line 49, in 
<module> 
    cv2.imshow('pose',img) cv2.error: ..\..\..\modules\highgui\src\window.cpp:269: error: (-215) 
size.width>0 && size.height>0 in function cv::imshow 

感謝您的關注。

回答

1

可能會造成問題的一件事是,cap.read()的成功未被檢查:變量ret被重新分配給一個新值,而未經過第一次檢查。

cap.read()下面的代碼周圍可能會有幫助if ret:。您會注意到在Python中沒有必要使用== True

+0

在上面定義的img「draw」函數上繪製三維座標軸不工作?或錯誤的代碼 – acs 2014-12-04 10:57:53

+0

你說得對,我應該指出opencv 3文檔,在這個版本'cv2.line'確實會返回圖像。我會更新我的答案。 – 2014-12-04 11:08:06

+0

我有插入此代碼,但沒有改變 帽= cv2.VideoCapture(2) RET,幀= cap.read() 打印 「檢查相機讀取:」 + STR(RET) 而保留==題: cap = cv2.VideoCapture(2) ret,frame = cap.read() time.sleep(1) – acs 2014-12-04 11:48:53

0

感謝Arnaud的興趣。

我解決了這個問題。工作代碼:

import scipy.io 
import numpy as np 
import cv2 
import time 

data=scipy.io.loadmat('distm.mat') 
dist=data['dist000'] 
mtx=data['mtx'] 
data=None 

#takes the corners in the chessboard 
def draw(img, corners, imgpts): 
    corner = tuple(corners[0].ravel()) 
    cv2.line(img, corner, tuple(imgpts[0].ravel()), (255,0,0), 5) 
    cv2.line(img, corner, tuple(imgpts[1].ravel()), (0,255,0), 5) 
    cv2.line(img, corner, tuple(imgpts[2].ravel()), (0,0,255), 5) 
    return img 

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) 

axis = np.float32([[3,0,0], [0,3,0], [0,0,-3]]).reshape(-1,3) 

cap = cv2.VideoCapture(2) 
ret, frame = cap.read() 
print "Checking camera read:"+str(ret) 
while ret ==False: 
    cap = cv2.VideoCapture(2) 
    ret, frame = cap.read() 
time.sleep(1) 

for i in range(0,20): 
    time.sleep(1) 
    ret, img = cap.read() 
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 
    ret, corners = cv2.findChessboardCorners(gray, (7,6),None) 
    print str(i)+"'th image check board corners "+str(ret) 
    if ret : 
     corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria) 
     print corners2 
     # Find the rotation and translation vectors. 
     rvecs, tvecs, inliers = cv2.solvePnPRansac(objp, corners, mtx, dist) 

     # project 3D points to image plane 
     imgpts, jac = cv2.projectPoints(axis, rvecs, tvecs, mtx, dist) 

     img = draw(img,corners,imgpts) 
     cv2.imshow('pose',img) 
     k = cv2.waitKey(0) & 0xff