Giter Site home page Giter Site logo

Sample code doesn't run: OpenCV Error: Assertion failed (ni > 0 && ni == ni1) in collectCalibrationData about opencv2-python-tutorials HOT 6 OPEN

abidrahmank avatar abidrahmank commented on July 22, 2024
Sample code doesn't run: OpenCV Error: Assertion failed (ni > 0 && ni == ni1) in collectCalibrationData

from opencv2-python-tutorials.

Comments (6)

SaberD avatar SaberD commented on July 22, 2024 2

I had the same problem. I used a different size checkered board image. Mine was size 6,9 and not 7,6.
I got this error when i did not change the dimension in some, but not all the necessary places.
Below is the code that worked for me, with the checkered board image saved as "test.png" in the same folder as the python file. link 🏁 to the image i used

# termination criteria
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)

# Arrays to store object points and image points from all the images.
imgpoints = [] # 2d points in image plane.
objpoints = [] # 3d point in real world space

img = cv2.imread("test2.png")

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

# Find the chess board corners
ret, corners = cv2.findChessboardCorners(gray, (6,9),None)

# If found, add object points, image points (after refining them)
if ret == True:
    objpoints.append(objp)

    corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
    imgpoints.append(corners2)

# Draw and display the corners
cv2.drawChessboardCorners(img, (6,9), corners2,ret)
cv2.imshow('img',img)
cv2.waitKey(500)

from opencv2-python-tutorials.

mgmacias95 avatar mgmacias95 commented on July 22, 2024

Hi @alexblack

I had the same issue and after a little research I found the problem in stackoverflow

I resolved it changing the shape of objp:

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

Also I had another problem: the number of corners found by findChessboardCorners can be less than 7*6 (pattern size) so I just kept number of corners found 3D points:

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]])

after that the code worked fine 😄

from opencv2-python-tutorials.

rabiafyz avatar rabiafyz commented on July 22, 2024

Hi @mgmacias95

I did not understand what you mean by doing reshape obj.
Do you mean reshap it at the time of initailization?

from opencv2-python-tutorials.

mgmacias95 avatar mgmacias95 commented on July 22, 2024

Hi @rabiafyz

Yes, just before the for loop:

# termination criteria
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*7,3), np.float32)
objp[:,:2] = np.mgrid[0:7,0:6].T.reshape(-1,2)
objp = objp.reshape(-1,1,3)

# Arrays to store object points and image points from all the images.
objpoints = [] # 3d point in real world space
imgpoints = [] # 2d points in image plane.

images = glob.glob('*.jpg')

for fname in images:
    print 'Filename: ', fname
    .
    .
    .

from opencv2-python-tutorials.

petered avatar petered commented on July 22, 2024

I had a similar problem (error: (-215) ni >= 0 in function collectCalibrationData). For me it was that my objp points were float64 instead of float32, which was not exactly obvious from that message.

from opencv2-python-tutorials.

ujwalaparupalli avatar ujwalaparupalli commented on July 22, 2024

Hi @mgmacias95 @petered @alexblack ,

I am also having the same problem. I tried the above suggested changes but still of no help and getting the same error. can someone please help me.

Thank you.

from opencv2-python-tutorials.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.