0

I was trying to find out the camera matrix and distortion coefficients using cvCalibrateCamera2. There were no compilation errors, but when I am trying to execute the program it gives:

OpenCV error: Sizes of input arguments do not match( both matrices must have the same number of points) in cvConvertPointsHomogenous, file /build/buildd/opencv-2.3.1/modules/calib3d/src/fundam.cpp

size of the matrix storing object points in 4 x 3 and that of the matrix storing image points in 4 X2, what could be wrong?

Now I made certain changes to my code. This is the code that I am using:

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/calib3d/calib3d.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//function to call cvCalibrateCamera2()
void calibrate(CvMat* object_points, CvMat* image_points, CvMat* intrinsic,CvMat* distortion)
{
const int point_count= object_points->rows;
const int image_count=object_points->rows/point_count;
CvMat* const full_object_points = cvCreateMat(image_count*point_count,3,CV_32FC1);
CvMat* const point_counts= cvCreateMat(image_count,1,CV_32SC1);
for(int i =0; i<image_count;i++)
{
CV_MAT_ELEM(*point_counts,float , i,0)= point_count;
    for(int j=0;j<point_count;j++)
    {
        for(int k=0; k<3;k++){
        CV_MAT_ELEM(*full_object_points,float,i*point_count+j,k)=CV_MAT_ELEM(*object_points,float,j,k);
        }
    }
}
cvCalibrateCamera2(full_object_points,image_points,point_counts,cvSize(1,1),intrinsic, distortion,NULL,NULL,0);
}
int main()
{
const float points[][2]={{1,2},{0,0},{3,5},{5,2}};
const int image_count=5;
const int point_count=sizeof(points)/sizeof(points[1]);
CvMat* const object_points=cvCreateMat(point_count,3,CV_32FC1);
for(int i=0; i<point_count;i++)
{
CV_MAT_ELEM(*object_points, float, i,0)=points[i][0];
CV_MAT_ELEM(*object_points, float, i,1)=points[i][1];
CV_MAT_ELEM(*object_points, float, i,2)=0;
}
CvMat* const image_points=cvCreateMat(image_count*point_count,2,CV_32FC1);
CvMat* const intrinsic=cvCreateMat(3,3,CV_32FC1);
CvMat* const distortion=cvCreateMat(5,1,CV_32FC1);
calibrate(object_points,image_points,intrinsic,distortion);
}

On execution I am getting the following error:

OpenCV Error: Bad argument (The total number of matrix elements is not divisible by the new number of rows) in cvReshape, file /build/buildd/opencv-2.3.1/modules/core/src/array.cpp, line 2755 terminate called after throwing an instance of 'cv::Exception' what(): /build/buildd/opencv-2.3.1/modules/core/src/array.cpp:2755: error: (-5) The total number of matrix elements is not divisible by the new number of rows in function cvReshape

Aborted (core dumped)

jabaldonedo
  • 25,822
  • 8
  • 77
  • 77
Mojo Jojo
  • 369
  • 3
  • 11
  • 31

1 Answers1

0

Exactly what the error message means. It needs an equal amount of points for the object as for the image points. If you would try to calculate with say 10 sets of image points, but with 9 sets of object points, this error is returned.

I'd recommend using the c++ openCV, as this accepts vectors with points and matrices as input. Checking the length of such vectors is far easier than for matrices.

Nallath
  • 2,100
  • 20
  • 37
  • I am doing that, taking 4 coplanar (not collinear) points (object and image). Still getting same error, should I increase the number of points ? – Mojo Jojo Jun 13 '13 at 09:19
  • Calibration should be done with at least 10 sets of image and object points. More points per set is better. I usually calibrate with 48 points per set and use 20> sets. There are several tutorials on how to perform this calibration (even with working code provided). – Nallath Jun 13 '13 at 10:42