Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Visualizing Calibration Error. #9

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions calib_intrinsic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ vector< vector< Point2f > > left_img_points;
Mat img, gray;
Size im_size;

void setup_calibration(int board_width, int board_height, int num_imgs,
void setup_calibration(int board_width, int board_height, int num_imgs,
float square_size, char* imgs_directory, char* imgs_filename,
char* extension) {
Size board_size = Size(board_width, board_height);
Expand All @@ -38,7 +38,7 @@ void setup_calibration(int board_width, int board_height, int num_imgs,
TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 30, 0.1));
drawChessboardCorners(gray, board_size, corners, found);
}

vector< Point3f > obj;
for (int i = 0; i < board_height; i++)
for (int j = 0; j < board_width; j++)
Expand Down Expand Up @@ -76,7 +76,7 @@ double computeReprojectionErrors(const vector< vector< Point3f > >& objectPoints

int main(int argc, char const **argv)
{
int board_width, board_height, num_imgs;
int board_width, board_height, num_imgs, stat_on;
float square_size;
char* imgs_directory;
char* imgs_filename;
Expand All @@ -92,8 +92,9 @@ int main(int argc, char const **argv)
{ "imgs_filename",'i',POPT_ARG_STRING,&imgs_filename,0,"Image filename","STR" },
{ "extension",'e',POPT_ARG_STRING,&extension,0,"Image extension","STR" },
{ "out_file",'o',POPT_ARG_STRING,&out_file,0,"Output calibration filename (YML)","STR" },
{ "stat_on",'v',POPT_ARG_NONE,&stat_on,0,"Statistical Analysis","NUM" },
POPT_AUTOHELP
{ NULL, 0, 0, NULL, 0, NULL, NULL }
{ NULL, 0, 0, NULL, 0, NULL, NULL, }
};

POpt popt(NULL, argc, argv, options, 0);
Expand All @@ -111,15 +112,20 @@ int main(int argc, char const **argv)
flag |= CV_CALIB_FIX_K4;
flag |= CV_CALIB_FIX_K5;
calibrateCamera(object_points, image_points, img.size(), K, D, rvecs, tvecs, flag);

cout << "Calibration error: " << computeReprojectionErrors(object_points, image_points, rvecs, tvecs, K, D) << endl;
float cerror = computeReprojectionErrors(object_points, image_points, rvecs, tvecs, K, D);
cout << "Calibration error: " << cerror << endl;

FileStorage fs(out_file, FileStorage::WRITE);
fs << "K" << K;
fs << "D" << D;
fs << "board_width" << board_width;
fs << "board_height" << board_height;
fs << "square_size" << square_size;
if(stat_on){
fs << "num_imgs" << num_imgs;
fs << "cerror" << cerror;
printf("Saved");
}
printf("Done Calibration\n");

return 0;
Expand Down
20 changes: 20 additions & 0 deletions scripts/plot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import yaml
import numpy as np
import matplotlib.pyplot as plt

def opencv_matrix(loader, node):
mapping = loader.construct_mapping(node, deep=True)
mat = np.array(mapping["data"])
mat.resize(mapping["rows"], mapping["cols"])
return mat

yaml.add_constructor(u"tag:yaml.org,2002:opencv-matrix", opencv_matrix)
yml_dir = '../build/cam_left.yml'

with open(yml_dir,'r') as infile:
_ = infile.readline()
data = yaml.load(infile)

plt.plot([data['cerror']], [data['num_imgs']], marker='o', markersize=10, color="red")
plt.axis([0, 1, 0, 50])
plt.show()