{"id":366,"date":"2017-10-29T17:57:07","date_gmt":"2017-10-29T17:57:07","guid":{"rendered":"https:\/\/peterklemperer.com\/blog\/?p=366"},"modified":"2017-11-28T04:29:00","modified_gmt":"2017-11-28T04:29:00","slug":"opencv-charuco-camera-calibration","status":"publish","type":"post","link":"https:\/\/peterklemperer.com\/blog\/2017\/10\/29\/opencv-charuco-camera-calibration\/","title":{"rendered":"OpenCV Charuco Camera-Calibration"},"content":{"rendered":"<p>OpenCV is an amazing tool that can provide accurate pose information in real-time, but first you have to provide it with a model of your camera. The opencv_interactive-calibraiton tool nominally provides that capability.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"368\" data-permalink=\"https:\/\/peterklemperer.com\/blog\/2017\/10\/29\/opencv-charuco-camera-calibration\/opencv_interactive-calibration-ok\/\" data-orig-file=\"https:\/\/peterklemperer.com\/blog\/wp-content\/uploads\/2017\/10\/opencv_interactive-calibration-OK.jpeg\" data-orig-size=\"2560,1484\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"opencv_interactive-calibration-OK\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/peterklemperer.com\/blog\/wp-content\/uploads\/2017\/10\/opencv_interactive-calibration-OK-300x174.jpeg\" data-large-file=\"https:\/\/peterklemperer.com\/blog\/wp-content\/uploads\/2017\/10\/opencv_interactive-calibration-OK-1024x594.jpeg\" class=\"aligncenter size-medium wp-image-368\" src=\"https:\/\/peterklemperer.com\/blog\/wp-content\/uploads\/2017\/10\/opencv_interactive-calibration-OK-300x174.jpeg\" alt=\"\" width=\"300\" height=\"174\" srcset=\"https:\/\/peterklemperer.com\/blog\/wp-content\/uploads\/2017\/10\/opencv_interactive-calibration-OK-300x174.jpeg 300w, https:\/\/peterklemperer.com\/blog\/wp-content\/uploads\/2017\/10\/opencv_interactive-calibration-OK-768x445.jpeg 768w, https:\/\/peterklemperer.com\/blog\/wp-content\/uploads\/2017\/10\/opencv_interactive-calibration-OK-1024x594.jpeg 1024w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>Follows is a record of how I obtained calibration settings for my laptop camera. I hope others find it useful, but I&#8217;m no expert and provide no warranty or guarantee. Please comment if you have feedback and let me used if this was helpful to you.<\/p>\n<p><!--more--><\/p>\n<p>Install opencv and verify that opencv_interactive-calibration:<\/p>\n<pre>opencv_interactive-calibration --help<\/pre>\n<p>If you&#8217;re having trouble installing opencv, you might try Anaconda. After installing Anaconda, then installed opencv using the following command:<\/p>\n<pre>conda install -c conda-forge opencv<\/pre>\n<p>Once you have that opencv installed and working, complete the following tasks to get a camera matrix and distance coefficients for your specific camera.<\/p>\n<p><strong>Overview (details follow)<\/strong><\/p>\n<ol>\n<li>Print out a Charucho board<\/li>\n<li>Save the configuration file: defaultConfig.xml<\/li>\n<li>\n<pre>opencv_interactive-calibration -h=7 -w=5 --sz=.032 --t=charuco<\/pre>\n<\/li>\n<\/ol>\n<p><strong>Step 1: Print out a board<\/strong><\/p>\n<p>Print out the (5,11,aruco.DICT_6X6_250) Charucho board:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"367\" data-permalink=\"https:\/\/peterklemperer.com\/blog\/2017\/10\/29\/opencv-charuco-camera-calibration\/charucho-board\/\" data-orig-file=\"https:\/\/peterklemperer.com\/blog\/wp-content\/uploads\/2017\/10\/charucho-board.png\" data-orig-size=\"1160,1560\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"charucho-board\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/peterklemperer.com\/blog\/wp-content\/uploads\/2017\/10\/charucho-board-223x300.png\" data-large-file=\"https:\/\/peterklemperer.com\/blog\/wp-content\/uploads\/2017\/10\/charucho-board-761x1024.png\" class=\"aligncenter size-medium wp-image-367\" src=\"https:\/\/peterklemperer.com\/blog\/wp-content\/uploads\/2017\/10\/charucho-board-223x300.png\" alt=\"\" width=\"223\" height=\"300\" srcset=\"https:\/\/peterklemperer.com\/blog\/wp-content\/uploads\/2017\/10\/charucho-board-223x300.png 223w, https:\/\/peterklemperer.com\/blog\/wp-content\/uploads\/2017\/10\/charucho-board-768x1033.png 768w, https:\/\/peterklemperer.com\/blog\/wp-content\/uploads\/2017\/10\/charucho-board-761x1024.png 761w, https:\/\/peterklemperer.com\/blog\/wp-content\/uploads\/2017\/10\/charucho-board.png 1160w\" sizes=\"auto, (max-width: 223px) 100vw, 223px\" \/><\/p>\n<p>If you want dimensionally accurate pose estimates, you will need to measure the length of a side of one of the bigger black squares. Mine was 3.2 centimeters.<\/p>\n<p><strong>Step 2: Save the interactive calibration file<\/strong><\/p>\n<p>Save the following opencv_interactive-calibration into a file named defaultConfig.xml into a folder:<\/p>\n<pre>&lt;?xml version=\"1.0\"?&gt;\r\n&lt;opencv_storage&gt;\r\n &lt;charuco_dict&gt;10&lt;\/charuco_dict&gt;\r\n &lt;charuco_square_lenght&gt;200&lt;\/charuco_square_lenght&gt;\r\n &lt;charuco_marker_size&gt;100&lt;\/charuco_marker_size&gt;\r\n &lt;calibration_step&gt;1&lt;\/calibration_step&gt;\r\n &lt;max_frames_num&gt;30&lt;\/max_frames_num&gt;\r\n &lt;min_frames_num&gt;10&lt;\/min_frames_num&gt;\r\n &lt;solver_eps&gt;1e-7&lt;\/solver_eps&gt;\r\n &lt;solver_max_iters&gt;30&lt;\/solver_max_iters&gt;\r\n &lt;fast_solver&gt;0&lt;\/fast_solver&gt;\r\n &lt;frame_filter_conv_param&gt;0.1&lt;\/frame_filter_conv_param&gt;\r\n &lt;camera_resolution&gt;1280 720&lt;\/camera_resolution&gt;\r\n&lt;\/opencv_storage&gt;<\/pre>\n<p><strong>Step 3: Interactive calibration<\/strong><\/p>\n<p>Run the opencv_interactive-calibration app from the directory that you stored the configuration xml file. Set the &#8211;sz according to the size of the board you printed and measured in step 1. For mine, the black square was 3.2 centimeters.<\/p>\n<pre>opencv_interactive-calibration -h=7 -w=5 --sz=.032 --t=charuco<\/pre>\n<p>Move the calibration target around the cameras field of view until you collect enough frames to cover the field. The app will display the word OK on the overlay to indicate that a good fit has been acquired. See the image at the top.<\/p>\n<p>When a good fit is acquired, press the s key while in the frame window to save an XML file with the opencv camera configuration parameters.<\/p>\n<p><strong>If things aren&#8217;t working:<\/strong><\/p>\n<p>When I first started trying to calibrate my camera, the interactive tool wouldn&#8217;t do anything and just showed me the video feed and no overlay.<\/p>\n<p>For me, I had to basically hand-write a calibration tool before I could figure out which configurations were incorrectly set. Try following the instructions above faithfully and hopefully you&#8217;ll have better luck!<\/p>\n<p><strong>Resources:<\/strong><\/p>\n<ul>\n<li>https:\/\/docs.opencv.org\/trunk\/d7\/d21\/tutorial_interactive_calibration.html<\/li>\n<li>https:\/\/docs.opencv.org\/3.2.0\/dc\/dbb\/tutorial_py_calibration.html<\/li>\n<li>https:\/\/docs.opencv.org\/3.1.0\/df\/d4a\/tutorial_charuco_detection.html<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>OpenCV is an amazing tool that can provide accurate pose information in real-time, but first you have to provide it with a model of your camera. The opencv_interactive-calibraiton tool nominally provides that capability. Follows is a record of how I &hellip; <a href=\"https:\/\/peterklemperer.com\/blog\/2017\/10\/29\/opencv-charuco-camera-calibration\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[65,6,10],"tags":[],"class_list":["post-366","post","type-post","status-publish","format-standard","hentry","category-mobot","category-projects","category-toys"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1VqWo-5U","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/peterklemperer.com\/blog\/wp-json\/wp\/v2\/posts\/366","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/peterklemperer.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/peterklemperer.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/peterklemperer.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/peterklemperer.com\/blog\/wp-json\/wp\/v2\/comments?post=366"}],"version-history":[{"count":0,"href":"https:\/\/peterklemperer.com\/blog\/wp-json\/wp\/v2\/posts\/366\/revisions"}],"wp:attachment":[{"href":"https:\/\/peterklemperer.com\/blog\/wp-json\/wp\/v2\/media?parent=366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/peterklemperer.com\/blog\/wp-json\/wp\/v2\/categories?post=366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/peterklemperer.com\/blog\/wp-json\/wp\/v2\/tags?post=366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}