错误:( - 215)nimages>使用Python和OpenCV在函数calibrateCamera中为0

时间:2014-05-23 00:07:41

标签: python opencv ubuntu

我试图根据opencv示例中给出的示例来校准我的网络摄像头,但是当我运行此处提供的代码时:

def caliLeftCam():    
    args, img_mask = getopt.getopt(sys.argv[1:], '', ['save=', 'debug=', 'square_size='])
    args = dict(args)
    try: img_mask = img_mask[0]
    except: img_mask = '../cpp/img*.jpg'
    img_names = glob(img_mask)
    debug_dir = args.get('--debug')
    square_size = float(args.get('--square_size', 1.0))

    pattern_size = (7, 5)
    pattern_points = np.zeros( (np.prod(pattern_size), 3), np.float32 )
    pattern_points[:,:2] = np.indices(pattern_size).T.reshape(-1, 2)
    pattern_points *= square_size

    obj_points = []
    img_pointsL = []
    h, w = 0, 0
    for fn in img_names:
        print "processing %s..." % fn,
        imgL = cv2.imread(fn, 0)
        h, w = imgL.shape[:2]
        found, corners = cv2.findChessboardCorners(imgL, pattern_size)
        if found:
            term = ( cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1 )
            cv2.cornerSubPix(imgL, corners, (5, 5), (-1, -1), term)
        if debug_dir:
            vis = cv2.cvtColor(imgL, cv2.COLOR_GRAY2BGR)
            cv2.drawChessboardCorners(vis, pattern_size, corners, found)
            path, name, ext = splitfn(fn)
            cv2.imwrite('%s/%s_chess.bmp' % (debug_dir, name), vis)
        if not found:
            print "chessboard not found"
            continue
        img_pointsL.append(corners.reshape(-1, 2))
        obj_points.append(pattern_points)

        print 'ok'

    rmsL, cameraL_matrix, dist_coefsL, rvecsL, tvecsL = cv2.calibrateCamera(obj_points, img_pointsL, (w, h))

我收到了这个错误:

Traceback (most recent call last):
File "/home/sabrine/Downloads/opencv-2.4.9/samples/python2/Memo.py", line 293, in <module>
Img_pointsL, Cam_MatL, DisL = caliLeftCam()
File "/home/sabrine/Downloads/opencv-2.4.9/samples/python2/Memo.py", line 124, in caliLeftCam
rmsL, cameraL_matrix, dist_coefsL, rvecsL, tvecsL = cv2.calibrateCamera(obj_points, img_pointsL, (w, h))
error: /build/buildd/opencv-2.4.8+dfsg1/modules/calib3d/src/calibration.cpp:3415: 
error: (-215) nimages > 0 in function calibrateCamera

这个错误是什么意思? 我该如何解决呢?

5 个答案:

答案 0 :(得分:1)

错误表明作为参数提供的一个向量是空的。

如果不满足所有条件,则该函数具有阻止您使用它的断言。这次它检查是否有足够的图像点(nimages > 0必须为真)。

答案 1 :(得分:0)

重新检查函数“ cv2.findChessboardCorners(image,(nx,ny))/ cv2.drawChessboardCorners(image,pattern_size)”的nx和ny(pattern_size)的值,该值应为内角行数和列数在棋盘上。它对我有用。

答案 2 :(得分:0)

我知道我为时已晚,但希望它对其他人有帮助。替换以下行:

except: img_mask = '../cpp/img*.jpg'

具有:

except: img_mask = './cpp/img*.jpg'

答案 3 :(得分:0)

在我的情况下,示例图片中没有使用足够大的棋盘格。打印7x10的棋盘而不是6x7即可解决。

答案 4 :(得分:-2)

import numpy as np
import cv2

cap = cv2.VideoCapture('output.avi')
i=1

while(cap.isOpened()):

    ret, img = cap.read()

    print img

    if img==None:#sale el error por que ya termino los frames del video
        break             #si no hay frames, terminarel programa

    cv2.imshow('img2',img)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    elif cv2.waitKey(1) & 0xFF==ord('p'):
        cv2.imwrite('image'+str(i)+'.jpg',img)
        i=i+1

cap.release()
cv2.destroyAllWindows()