Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First off, your camera matrix is wrong. If you read the <a href="http://opencv.itseez.com/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html#calibratecamera" rel="noreferrer">documentation</a>, it should look like:</p> <pre><code>fx 0 cx 0 fy cy 0 0 1 </code></pre> <p>If you look at yours, you've got it the wrong way round:</p> <pre><code>fx 0 0 0 fy 0 cx cy 1 </code></pre> <p>So first, set <code>camera_matrix</code> to <code>camera_matrix.T</code> (or change how you construct <code>camera_matrix</code>. Remember that <code>camera_matrix[i,j]</code> is <em>row</em> <code>i</code>, <em>column</em> <code>j</code>).</p> <pre><code>camera_matrix = camera_matrix.T </code></pre> <p>Next, I ran your code and I see that "can't seem to get it to work" means the following error (by the way - always say what you mean by "can't seem to get it to work" in your questions - if it's an error, post the error. If it runs but gives you weirdo numbers, say so):</p> <pre><code>OpenCV Error: Assertion failed (ni &gt;= 0) in collectCalibrationData, file /home/cha66i/Downloads/OpenCV-2.3.1/modules/calib3d/src/calibration.cpp, line 3161 Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; cv2.error: /home/cha66i/Downloads/OpenCV-2.3.1/modules/calib3d/src/calibration.cpp:3161: error: (-215) ni &gt;= 0 in function collectCalibrationData </code></pre> <p>I then read the <a href="http://opencv.itseez.com/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html#calibratecamera" rel="noreferrer">documentation</a> (very useful by the way) and noticed that <code>obj_points</code> and <code>img_points</code> have to be <strong>vectors of vectors</strong>, because it is possible to feed in sets of object/image points for multiple images of the same chessboard(/calibration points).</p> <p>Hence:</p> <pre><code>cv2.calibrateCamera([obj_points], [img_points],size, camera_matrix, dist_coefs) </code></pre> <p>What? I still get the same error?!</p> <p>Then, I had a look at the OpenCV python2 samples (in the folder <code>OpenCV-2.x.x/samples/python2</code>), and noticed a <code>calibration.py</code> showing me how to use the calibration functions (never underestimate the samples, they're often better than the documentation!).</p> <p>I tried to run <code>calibration.py</code> but it doesn't run because it doesn't supply the <code>camera_matrix</code> and <code>distCoeffs</code> arguments, which are necessary. So I modified it to feed in a dummy <code>camera_matrix</code> and <code>distCoeffs</code>, and hey, it works!</p> <p>The only difference I can see between my <code>obj_points</code>/<code>img_points</code> and theirs, is that theirs has <code>dtype=float32</code>, while mine doesn't.</p> <p>So, I change my <code>obj_points</code> and <code>img_points</code> to also have dtype float32 (the python2 interface to OpenCV is funny like that; often functions don't work when matrices don't have a <code>dtype</code>):</p> <pre><code>obj_points = obj_points.astype('float32') img_points = img_points.astype('float32') </code></pre> <p>Then I try again:</p> <pre><code>&gt;&gt;&gt; cv2.calibrateCamera([obj_points], [img_points],size, camera_matrix, dist_coefs) OpenCV Error: Bad argument (For non-planar calibration rigs the initial intrinsic matrix must be specified) in cvCalibrateCamera2, file .... </code></pre> <p>What?! A different error at least. But I <em>did</em> supply an initial intrinsic matrix!</p> <p>So I go back to the documentation, and notice the <code>flags</code> parameter:</p> <blockquote> <p>flags – Different flags that may be zero or a combination of the following values:</p> <p><code>CV_CALIB_USE_INTRINSIC_GUESS</code> cameraMatrix contains valid initial values of fx, fy, cx, cy that are optimized further</p> <p>...</p> </blockquote> <p>Aha, so I have to tell the function <em>explicitly</em> to use the initial guesses I provided:</p> <pre><code>cv2.calibrateCamera([obj_points], [img_points],size, camera_matrix.T, dist_coefs, flags=cv2.CALIB_USE_INTRINSIC_GUESS) </code></pre> <p>Hurrah! It works!</p> <p>(Moral of the story - read the OpenCV documentation carefully, and use the newest version (i.e. on opencv.itseez.com) if you're using the Python <code>cv2</code> interface. Also, consult the examples in the <code>samples/python2</code> directory to supplement the documentation. With these two things you should be able to work out most problems.)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload