From 386bef46da2a5d2484f317b92a3ffed260d9fab1 Mon Sep 17 00:00:00 2001 From: Pushkal Katara Date: Mon, 11 Dec 2017 17:11:05 +0530 Subject: [PATCH 1/4] Visualizing Error --- visualize.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 visualize.py diff --git a/visualize.py b/visualize.py new file mode 100644 index 0000000..2249a0f --- /dev/null +++ b/visualize.py @@ -0,0 +1,34 @@ +from subprocess import Popen, PIPE +from sys import argv +import numpy as np +import matplotlib.pyplot as plt + +def getopts(argv): + opts = {} + while argv: + if argv[0][0] == '-': + opts[argv[0]] = argv[1] + argv = argv[1:] + return opts + +myargs = getopts(argv) +args = ("./build/calibrate", + "-w", myargs["-w"], + "-h", myargs["-h"], + "-n", myargs["-n"], + "-s", myargs["-s"], + "-d", myargs["-d"], + "-i", myargs["-i"], + "-o", myargs["-o"], + "-e", myargs["-e"]) + +p = Popen(args, stdout=PIPE) +p.wait() +output = p.stdout.read().split('\n') +for line in output: + if(line.startswith('Calibration error:')): + print(line + " At n = " + myargs["-n"] + ", Plotting") + error = float(line[19:]) + plt.plot([error], [int(myargs['-n'])], marker='o', markersize=10, color="red") + plt.axis([0, 1, 0, 50]) + plt.show() From b774b44760b13260e1053ac3a6e7c226a105ab3d Mon Sep 17 00:00:00 2001 From: Pushkal Katara Date: Thu, 14 Dec 2017 23:28:32 +0530 Subject: [PATCH 2/4] YAML read Yaml read --- calib_intrinsic.cpp | 17 +++++++++++------ scripts/plot.py | 20 ++++++++++++++++++++ scripts/visualize.py | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 scripts/plot.py create mode 100644 scripts/visualize.py diff --git a/calib_intrinsic.cpp b/calib_intrinsic.cpp index c02bfda..5d3573e 100644 --- a/calib_intrinsic.cpp +++ b/calib_intrinsic.cpp @@ -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); @@ -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++) @@ -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; @@ -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_INT,&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); @@ -111,8 +112,8 @@ 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; @@ -120,6 +121,10 @@ int main(int argc, char const **argv) 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("Done Calibration\n"); return 0; diff --git a/scripts/plot.py b/scripts/plot.py new file mode 100644 index 0000000..c005c7f --- /dev/null +++ b/scripts/plot.py @@ -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() diff --git a/scripts/visualize.py b/scripts/visualize.py new file mode 100644 index 0000000..a5d10c3 --- /dev/null +++ b/scripts/visualize.py @@ -0,0 +1,35 @@ +from subprocess import Popen, PIPE +from sys import argv +import numpy as np +import matplotlib.pyplot as plt + +def getopts(argv): + opts = {} + while argv: + if argv[0][0] == '-': + opts[argv[0]] = argv[1] + argv = argv[1:] + return opts + +myargs = getopts(argv) +args = ("./build/calibrate", + "-w", myargs["-w"], + "-h", myargs["-h"], + "-n", myargs["-n"], + "-s", myargs["-s"], + "-d", myargs["-d"], + "-i", myargs["-i"], + "-o", myargs["-o"], + "-e", myargs["-e"], + "-v", myargs["-v"]) + +p = Popen(args, stdout=PIPE) +p.wait() +output = p.stdout.read().split('\n') +for line in output: + if(line.startswith('Calibration error:')): + print(line + " At n = " + myargs["-n"] + ", Plotting") + error = float(line[19:]) + plt.plot([error], [int(myargs['-n'])], marker='o', markersize=10, color="red") + plt.axis([0, 1, 0, 50]) + plt.show() From eb3610aef2d3842e36c1e445432f864e0de8a2c6 Mon Sep 17 00:00:00 2001 From: Pushkal Katara Date: Sat, 6 Jan 2018 21:45:47 +0530 Subject: [PATCH 3/4] added popt_arg_none --- calib_intrinsic.cpp | 3 ++- scripts/visualize.py | 35 ----------------------------------- 2 files changed, 2 insertions(+), 36 deletions(-) delete mode 100644 scripts/visualize.py diff --git a/calib_intrinsic.cpp b/calib_intrinsic.cpp index 5d3573e..61145c3 100644 --- a/calib_intrinsic.cpp +++ b/calib_intrinsic.cpp @@ -92,7 +92,7 @@ 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_INT,&stat_on,0,"Statistical Analysis","NUM" }, + { "stat_on",'v',POPT_ARG_NONE,&stat_on,0,"Statistical Analysis","NUM" }, POPT_AUTOHELP { NULL, 0, 0, NULL, 0, NULL, NULL, } }; @@ -124,6 +124,7 @@ int main(int argc, char const **argv) if(stat_on){ fs << "num_imgs" << num_imgs; fs << "cerror" << cerror; + printf("Saved"); } printf("Done Calibration\n"); diff --git a/scripts/visualize.py b/scripts/visualize.py deleted file mode 100644 index a5d10c3..0000000 --- a/scripts/visualize.py +++ /dev/null @@ -1,35 +0,0 @@ -from subprocess import Popen, PIPE -from sys import argv -import numpy as np -import matplotlib.pyplot as plt - -def getopts(argv): - opts = {} - while argv: - if argv[0][0] == '-': - opts[argv[0]] = argv[1] - argv = argv[1:] - return opts - -myargs = getopts(argv) -args = ("./build/calibrate", - "-w", myargs["-w"], - "-h", myargs["-h"], - "-n", myargs["-n"], - "-s", myargs["-s"], - "-d", myargs["-d"], - "-i", myargs["-i"], - "-o", myargs["-o"], - "-e", myargs["-e"], - "-v", myargs["-v"]) - -p = Popen(args, stdout=PIPE) -p.wait() -output = p.stdout.read().split('\n') -for line in output: - if(line.startswith('Calibration error:')): - print(line + " At n = " + myargs["-n"] + ", Plotting") - error = float(line[19:]) - plt.plot([error], [int(myargs['-n'])], marker='o', markersize=10, color="red") - plt.axis([0, 1, 0, 50]) - plt.show() From 443270d6597f62d309f399e6e3041df64c5c00ab Mon Sep 17 00:00:00 2001 From: Pushkal Katara Date: Sat, 6 Jan 2018 21:47:04 +0530 Subject: [PATCH 4/4] Removed old script --- visualize.py | 34 ---------------------------------- 1 file changed, 34 deletions(-) delete mode 100644 visualize.py diff --git a/visualize.py b/visualize.py deleted file mode 100644 index 2249a0f..0000000 --- a/visualize.py +++ /dev/null @@ -1,34 +0,0 @@ -from subprocess import Popen, PIPE -from sys import argv -import numpy as np -import matplotlib.pyplot as plt - -def getopts(argv): - opts = {} - while argv: - if argv[0][0] == '-': - opts[argv[0]] = argv[1] - argv = argv[1:] - return opts - -myargs = getopts(argv) -args = ("./build/calibrate", - "-w", myargs["-w"], - "-h", myargs["-h"], - "-n", myargs["-n"], - "-s", myargs["-s"], - "-d", myargs["-d"], - "-i", myargs["-i"], - "-o", myargs["-o"], - "-e", myargs["-e"]) - -p = Popen(args, stdout=PIPE) -p.wait() -output = p.stdout.read().split('\n') -for line in output: - if(line.startswith('Calibration error:')): - print(line + " At n = " + myargs["-n"] + ", Plotting") - error = float(line[19:]) - plt.plot([error], [int(myargs['-n'])], marker='o', markersize=10, color="red") - plt.axis([0, 1, 0, 50]) - plt.show()