0

For my thesis project, I need to determine the K-distortion parameters of a camera. I decided to use python and opencv to do is. So you can see I'm stell a beginner with Python and Opencv. I use this standard code:

import numpy as np
import cv2
import glob


#termination criteria
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 70, 0.001)

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

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

images = glob.glob('C:/tmp/pixelgrootte/*.jpg')
counter = int(x=1)

for fname in images:
    img = cv2.imread(fname)
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)


    #Find the chess board corners
    ret, corners = cv2.findChessboardCorners(gray, (5,7),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
        img = cv2.drawChessboardCorners(img, (5,7), corners2,ret)
        cv2.imshow('img',img)
        cv2.waitKey(500)
        counter += 1
    else:
        print("No corners found on Picture " + str(counter))
        counter += 1


cv2.destroyAllWindows()

#Calibration
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)
print("Camera Matrix: " + str(mtx))
print("Distortion Factors: " + str(dist))

When I run this script, this is the error i receive:

No corners found on Picture 1
Traceback (most recent call last):
  File "C:\Users\Lode\Documents\School\2014-2015\Thesis\Camera\Camera Calibration.py", line 37, in <module>
cv2.imshow('img',img)
error: ..\..\..\..\opencv\modules\highgui\src\window.cpp:261: error: (-215) size.width>0 && size.height>0 in function cv::imshow

As you can see I used this standard code:https://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_calib3d/py_calibration/py_calibration.html#calibration. The only differences are the counter (I used it before so I could easily determine if a certain pecture was clear enough), the size of the squares (=7cm) and the shape of the checkerboard(= 8hor and 6vert squares).

My first guess was that the imshow function didn't work but this is not the case. When I run this script, everything works just fine:

import numpy as np
import cv2
import glob

images = glob.glob('C:/tmp/pixelgrootte/*.jpg')

for fname in images:
    img = cv2.imread(fname)
    cv2.imshow('img',img)
    cv2.waitKey(500)

cv2.destroyAllWindows()

So then i decided to run the debugger. The debugger showed that img, imgpoints en corners2 were empty. So maybe the function cornerSubPix doesn't work in my case? But I really don't have any idea why this wouldn't work.

Strange part about this code is that it worked perfectly two weeks ago, but since then I reinstalled my computer because it was running slow. Now, i have current versions installed:

Windows 7 professional N 32bit

Python 2.7.9

Numpy 1.9.2

Opencv 2.4.11

So my guess is that I have a part of software needed to run this script not installed but I can't figure out what it is.

I already did some research on this topic but I couldn't find any solution on this forum that suited my problem. It seems like a common error but i can't figure out what causes this error.

Is there somebody that can help me with this? I would be really grateful!

1 Answers1

0

The error stems from this line:

img = cv2.drawChessboardCorners(img, (5,7), corners2,ret)

According to the docs, cv2.drawChessboardCorners returns None. So don't rebind img.

drawChessboardCorners's first parameter is the destination image (once again, from the docs), to which it will render the detected chessboard corners.

The tutorial you link to is intended for opencv version 3.x, as mentioned in this github issue, where the author encounters the same issue as you. Working code (tested with opencv 2.3.1.7, which mentions its __version__ is $Rev: 4557 $) is provided in this SO answer.

Community
  • 1
  • 1
Oliver W.
  • 13,169
  • 3
  • 37
  • 50
  • Thanks a lot for your help. I feel kinda stupid right now because this issue was already answered. But still, thanks for replying so fast. – Lode Vermeulen Apr 07 '15 at 11:28
  • @Lode, you could not have known: as far as I found, no post on stackoverflow mentions that it is a version issue. I only found out about it myself after trying to figure out what the correct way was then because Python functions typically do not change the parameters passed to it (unless explicitly mentioned in the docs). You don't need to upvote this answer (the other one I linked to might be more deserving of that), but perhaps you could consider clicking the checkmark to mark your question as solved. – Oliver W. Apr 07 '15 at 11:46
  • @OliverW. I am facing the exact same problem right now. corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria) Your solution worked fine and no errors were shown but corners2 still remains empty for me. It doesn't make sense because I'm using the exact same code from the docs. – Rakshit Kothari Oct 24 '15 at 19:24
  • @RakshitKothari, I'm not sure if your comment is about a similar and *unresolved* issue. If so, please consider making a new question altogether and possibly referring to this question to give a quick basis to start from to whoever helps you in resolving this. – Oliver W. Oct 24 '15 at 22:36