Webcam calibration using Python error and OpenCV

Pretty new to all of this, and I'm trying to calibrate for a webcam by following this guide and using the code below. I get the following error.

OpenCV error: statement failed (ni> 0 & ni == ni1) in collectCalibrationData, file / build / buildd / opencv -2.4.8 + dfsg1 / modules / calib3d / src / calibration.cpp, line 3193

cv2.error: /build/buildd/opencv-2.4.8+dfsg1/modules/calib3d/src/calibration.cpp: 3193: error: (-215) ni> 0 && & ni == ni1 in the collectCalibrationData p function >

Can someone explain what this error is and how to fix it?

(Full error below)

import numpy as np
import cv2
import glob


criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
# Arrays to store object points and image points from all the images.
objpoints = [] # 3d point in real world 
imgpoints = [] # 2d points in image plane.
images = glob.glob('*.png')


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


for fname in images:

    img = cv2.imread(fname)
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    ret = False
    # Find the chess board corners
    ret, corners = cv2.findChessboardCorners(gray, (6,9))
    # If found, add object points, image points (after refining them)
    if ret == True:
        objpoints.append(objp)
        cv2.cornerSubPix(gray, corners, (11,11), (-1,-1), criteria)

        imgpoints.append(corners)
        # Draw and display the corners 
        cv2.drawChessboardCorners(img, (6,9), corners, ret)
        cv2.imshow('img',img)
        cv2.waitKey(0)

cv2.waitKey(0)
for i in range (1,5):
    cv2.waitKey(1)
    cv2.destroyAllWindows()
    cv2.waitKey(1)


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

OpenCV: (ni > 0 & ni == ni1) collectCalibrationData, /build/buildd/opencv -2.4.8+dfsg1/modules/calib3d/src/calibration.cpp, 3193 Traceback ( ):    ", 1,   " /usr/lib/python 2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py ", 540,     execfile ( , )   " /home/students/Test/test.py", 49,     ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape [:: - 1], None, None) cv2.error:/build/buildd/opencv-2.4.8+dfsg1/modules/calib3d/src/calibration.cpp:3193: : (-215) ni > 0 && & ni == ni1 collectCalibrationData​​p >

+4
8

, ( , ) , ( 7x6, 6x9),

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

, :

# checkerboard Dimensions
cbrow = 5
cbcol = 7

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

.
.
.
ret, corners = cv2.findChessboardCorners(gray, (cbcol, cbrow), None)
+16

:

for( i = 0; i < nimages; i++, j += ni )
{
    Mat objpt = objectPoints.getMat(i);
    Mat imgpt1 = imagePoints1.getMat(i);
    ni = objpt.checkVector(3, CV_32F);
    int ni1 = imgpt1.checkVector(2, CV_32F);
    CV_Assert( ni > 0 && ni == ni1 );
    ...

: Assertion failed (ni > 0 && ni == ni1) , 0 .

: calibrateCamera() , . (, , ), :

obj = [[x, y, z], [x1, y1, z1]] # wrong
img = [[x, y], [x1, y1]]        # wrong

obj = [[[x, y, z], [x1, y1, z1]]] # right
img = [[[x, y], [x1, y1]]]        # right

, , , , , ( jpg png), , , , , , . , , /, , , . calibrateCamera , , , /samples/cpp

+3

, .

objp:

objp = objp.reshape(-1,1,3)

: , findChessboardCorners, 7 * 6 ( ), , 3D-:

corners2 = cv2.cornerSubPix(image=gray, corners=corners, 
               winSize=(11,11), zeroZone=(-1,-1),
               criteria=(cv2.TERM_CRITERIA_EPS + 
                         cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001))
imgpoints.append(corners2)
objpoints.append(objp[0:corners2.shape[0]])

: D

EDIT: , , retval (True False) , corners None.

+2

, , , imgpoints 1 , , objpoints. , , imgpoints . , - .

( , , )

import numpy as np
import cv2
import glob

# termination criteria
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
# Arrays to store object points and image points from all the images.

imgpoints = [] # 2d points in image plane.
images = glob.glob('*.png')

for fname in images:

    img = cv2.imread(fname)
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    ret = False
    # Find the chess board c  orners
    ret, corners = cv2.findChessboardCorners(gray, (6,9))
    # If found, add object points, image points (after refining them)
    if ret == True:
        cv2.cornerSubPix(gray, corners, (11,11), (-1,-1), criteria)
        imgpoints.append(corners)
        # Draw and display the corners 
        cv2.drawChessboardCorners(img, (6,9), corners, ret)
        cv2.imshow('img',img)
        cv2.waitKey(0)

cv2.waitKey(0)
for i in range (1,5):
    cv2.waitKey(1)
    cv2.destroyAllWindows()
    cv2.waitKey(1)
imgpoints = np.array(imgpoints,'float32')
print len(corners), len(pattern_points)



pattern_size = (9, 6)
pattern_points = np.zeros( (np.prod(pattern_size), 3), np.float32)
pattern_points[:, :2] = np.indices(pattern_size).T.reshape(-1, 2).astype(np.float32)
pattern_points = np.array(pattern_points,dtype=np.float32)

ret, matrix, dist_coef, rvecs, tvecs = cv2.calibrateCamera([pattern_points], [corners], gray.shape[::-1], flags=cv2.CALIB_USE_INTRINSIC_GUESS)
0

@s-low! .

- objp.

objp = np.zeros((6*7,3), np.float32), . python 2.7 dtype float64. , "cv2.calibrateCamera", :

OpenCV Error: Assertion failed (ni >= 0) in collectCalibrationData, file /Users/jhelmus/anaconda/conda-bld/work/opencv-2.4.8/modules/calib3d/src/calibration.cpp, line 3169

, ni = objpt.checkVector(3, CV_32F) , objp float32.

0

:

opencvdoc

, :

Corners2 = cv2.cornerSubPix(, , (11,11), (-1,1), )

opencv Null

:

Img = cv2.drawChessboardCorners(img, (7.6), 2, ret)

, , , , :

from webcam import Webcam
import cv2
from datetime import datetime
import numpy as np

webcam = Webcam()
webcam.start()

criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
objp = np.zeros((6*9,3), np.float32)
objp[:,:2] = np.mgrid[0:9,0:6].T.reshape(-1,2)
objpoints = []
imgpoints = []
i = 0

while i < 10:
    image = webcam.get_current_frame()
    gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
    ret, corners = cv2.findChessboardCorners(gray, (9,6), None)    
    print ret

    if ret == True:
        cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)       
        imgpoints.append(corners)
        objpoints.append(objp)
        cv2.drawChessboardCorners(image, (9,6), corners,ret)
        i += 1


    cv2.imshow('grid', image)
    cv2.waitKey(1000)

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

-:

import cv2
from threading import Thread

class Webcam:

    def __init__(self):
        self.video_capture = cv2.VideoCapture(0)
        self.current_frame = self.video_capture.read()[1]

    # create thread for capturing images
    def start(self):
        Thread(target=self._update_frame, args=()).start()

    def _update_frame(self):
        while(True):
            self.current_frame = self.video_capture.read()[1]

    # get the current frame
    def get_current_frame(self):
        return self.current_frame

, 6 9 , dimmensión 10x7.

0

, jpg-, . , , , , rect FALSE. , , - . , 8X6 8X6 , , ,

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

- , 1 ( , , 7X5), , .

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

, OpenCV 3.0 beta doc, , ,

0

I had the same problem that led me to the statement failed (ni> 0 & ni == ni1); I think you need to know the real meaning of ni and ni1, which was mentioned in the first answer. the object point and image points must be exactly the same size. regardless of the amount of the mat or the size of the mat. for me, my problem is that I have 5 mats for image points and object points, and for the mat size, image points 36 * 2, and object points 48 * 3. Therefore, I said that they should be exactly the same except one point2f and the other point3f.

0
source

All Articles