Path: blob/master/CameraCalibration/cameraCalibrationWithUndistortion.cpp
3118 views
#include <opencv2/opencv.hpp>1#include <opencv2/calib3d/calib3d.hpp>2#include <opencv2/highgui/highgui.hpp>3#include <opencv2/imgproc/imgproc.hpp>4#include <stdio.h>5#include <iostream>67// Defining the dimensions of checkerboard8int CHECKERBOARD[2]{6,9};910int main()11{12// Creating vector to store vectors of 3D points for each checkerboard image13std::vector<std::vector<cv::Point3f> > objpoints;1415// Creating vector to store vectors of 2D points for each checkerboard image16std::vector<std::vector<cv::Point2f> > imgpoints;1718// Defining the world coordinates for 3D points19std::vector<cv::Point3f> objp;20for(int i{0}; i<CHECKERBOARD[1]; i++)21{22for(int j{0}; j<CHECKERBOARD[0]; j++)23objp.push_back(cv::Point3f(j,i,0));24}252627// Extracting path of individual image stored in a given directory28std::vector<cv::String> images;29// Path of the folder containing checkerboard images30std::string path = "./images/*.jpg";3132cv::glob(path, images);3334cv::Mat frame, gray;35// vector to store the pixel coordinates of detected checker board corners36std::vector<cv::Point2f> corner_pts;37bool success;3839// Looping over all the images in the directory40for(int i{0}; i<images.size(); i++)41{42frame = cv::imread(images[i]);43cv::cvtColor(frame,gray,cv::COLOR_BGR2GRAY);4445// Finding checker board corners46// If desired number of corners are found in the image then success = true47success = cv::findChessboardCorners(gray,cv::Size(CHECKERBOARD[0],CHECKERBOARD[1]), corner_pts, cv::CALIB_CB_ADAPTIVE_THRESH | cv::CALIB_CB_FAST_CHECK | cv::CALIB_CB_NORMALIZE_IMAGE);4849/*50* If desired number of corner are detected,51* we refine the pixel coordinates and display52* them on the images of checker board53*/54if(success)55{56cv::TermCriteria criteria(cv::TermCriteria::EPS | cv::TermCriteria::MAX_ITER, 30, 0.001);5758// refining pixel coordinates for given 2d points.59cv::cornerSubPix(gray,corner_pts,cv::Size(11,11), cv::Size(-1,-1),criteria);6061// Displaying the detected corner points on the checker board62cv::drawChessboardCorners(frame, cv::Size(CHECKERBOARD[0],CHECKERBOARD[1]), corner_pts,success);6364objpoints.push_back(objp);65imgpoints.push_back(corner_pts);66}6768cv::imshow("Image",frame);69cv::waitKey(0);70}7172cv::destroyAllWindows();7374cv::Mat cameraMatrix,distCoeffs,R,T;7576/*77* Performing camera calibration by78* passing the value of known 3D points (objpoints)79* and corresponding pixel coordinates of the80* detected corners (imgpoints)81*/82cv::calibrateCamera(objpoints, imgpoints,cv::Size(gray.rows,gray.cols),cameraMatrix,distCoeffs,R,T);8384std::cout << "cameraMatrix : " << cameraMatrix << std::endl;85std::cout << "distCoeffs : " << distCoeffs << std::endl;86std::cout << "Rotation vector : " << R << std::endl;87std::cout << "Translation vector : " << T << std::endl;888990// Trying to undistort the image using the camera parameters obtained from calibration9192cv::Mat image;93image = cv::imread(images[0]);94cv::Mat dst, map1, map2,new_camera_matrix;95cv::Size imageSize(cv::Size(image.cols,image.rows));9697// Refining the camera matrix using parameters obtained by calibration98new_camera_matrix = cv::getOptimalNewCameraMatrix(cameraMatrix, distCoeffs, imageSize, 1, imageSize, 0);99100// Method 1 to undistort the image101cv::undistort( frame, dst, new_camera_matrix, distCoeffs, new_camera_matrix );102103// Method 2 to undistort the image104cv::initUndistortRectifyMap(cameraMatrix, distCoeffs, cv::Mat(),cv::getOptimalNewCameraMatrix(cameraMatrix, distCoeffs, imageSize, 1, imageSize, 0),imageSize, CV_16SC2, map1, map2);105106cv::remap(frame, dst, map1, map2, cv::INTER_LINEAR);107108//Displaying the undistorted image109cv::imshow("undistorted image",dst);110cv::waitKey(0);111112return 0;113}114115116