Ask Your Question

Revision history [back]

fisheye camea calibration

hello,

I can't get correct image from following code. Could you find where are problems in my code.

thanks,

int main(int argc, char** argv) {

Mat frame = imread("fisheye_img.jpg");
Size boardSize;                                                                                 
boardSize.width = 8;                                                                            
boardSize.height = 5;                                                                           
int sq_sz = 30;  

vector<vector<Point2f> > img_points;
vector<vector<Point3f> > obj_points(1);                                                         

int width  = boardSize.width;                                                                                  
int height = boardSize.height;                                                                                 




for (int i = 0; i < width; i++) {                                                          
    for (int j = 0; j < height; j++) {                        
        obj_points[0].push_back(Point3f(float(j * sq_sz), float(i * sq_sz), 0));                
    }                                                                                           
}        

vector<Point2f> corners;                                                                
bool found = findChessboardCorners(frame, boardSize, corners,                           
             CALIB_CB_ADAPTIVE_THRESH|CALIB_CB_NORMALIZE_IMAGE|CALIB_CB_FAST_CHECK);   

if(found)
    drawChessboardCorners(frame, boardSize, corners, found);   
else 
    cout << "can't find corners" << endl;
    return -1;

vector<Point2f> img_temp;
for (int i = 0; i < width*height; i++) {           
    Point2d temp = corners[i];      
    img_temp.push_back(temp);            
}        
img_points.push_back(img_temp); 

Mat _cameraMatrix;
Mat _distCoeffs;

_cameraMatrix = Mat::eye(3, 3, CV_64F);
_distCoeffs   = Mat::zeros(4, 1, CV_64F);


std::vector<cv::Vec3d> rvecs(1);
std::vector<cv::Vec3d> tvecs(1);

Size imagesize = frame.size();
obj_points.resize(img_points.size(),obj_points[0]);

int flags = cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC | cv::fisheye::CALIB_FIX_SKEW | //cv::fisheye::CALIB_CHECK_COND |
            cv::fisheye::CALIB_FIX_K3 | cv::fisheye::CALIB_FIX_K4;
double rms = cv::fisheye::calibrate(obj_points, img_points, imagesize, 
                        _cameraMatrix, _distCoeffs, rvecs, tvecs, flags, cv::TermCriteria(3, 20, 1e-6));
                        //cv::TermCriteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, 100, DBL_EPSILON));

cout << "RMS : " << rms << endl; 
cout << "_cameraMatrix : " << _cameraMatrix << endl; 
cout << "_distCoeffs : " << _distCoeffs << endl; 

 Mat new_frame, map1, map2;
 Mat newCamMat;

 fisheye::estimateNewCameraMatrixForUndistortRectify(_cameraMatrix, _distCoeffs, imagesize,
                                                            Matx33d::eye(), newCamMat, 1);

 fisheye::initUndistortRectifyMap(_cameraMatrix, _distCoeffs, Matx33d::eye(), newCamMat, imagesize,
                                          CV_16SC2, map1, map2);
 remap(frame, new_frame, map1, map2, INTER_LINEAR);


imshow("input image", frame);  
imshow("correct image", new_frame);  
cvWaitKey ( 0 ) ;

return 0; }