{"id":2628,"date":"2020-05-22T12:21:11","date_gmt":"2020-05-22T10:21:11","guid":{"rendered":"https:\/\/www.kurokesu.com\/main\/?p=2628"},"modified":"2020-05-22T12:55:52","modified_gmt":"2020-05-22T10:55:52","slug":"uvc-camera-exposure-timing-in-opencv","status":"publish","type":"post","link":"https:\/\/www.kurokesu.com\/main\/2020\/05\/22\/uvc-camera-exposure-timing-in-opencv\/","title":{"rendered":"UVC camera exposure timing in OpenCV"},"content":{"rendered":"\n<p>USB cameras can have the exposure set to manual (cheaper ones have only full auto mode). It\u2019s a pretty straight forward function in OpenCV, but insufficiently documented thus hard to find exact times and calculations. Also there are two major implementations for Linux and Windows.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Linux \u2013 exposure times are set in units 1\/s (all clear here)<\/li><li>Windows \u2013 exposure times are selected from a table where index ranges typically from 0 to -13. Value 0 means the longest exposure and -13 is the shortest time (fastest shutter).<\/li><\/ul>\n\n\n\n<p>Windows indexed exposure values are a logarithmic function of time. The equation is very simple&nbsp;<strong>EXP_TIME = 2^(-EXP_VAL)<\/strong>&nbsp;and exact values listed in the table below.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\"><strong>CAP_PROP_EXPOSURE<\/strong><\/td><td class=\"has-text-align-center\" data-align=\"center\"><strong>Actual exposure time<\/strong><\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">0<\/td><td class=\"has-text-align-center\" data-align=\"center\">1s<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">-1<\/td><td class=\"has-text-align-center\" data-align=\"center\">500ms<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">-2<\/td><td class=\"has-text-align-center\" data-align=\"center\">250ms<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">-3<\/td><td class=\"has-text-align-center\" data-align=\"center\">125ms<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">-4<\/td><td class=\"has-text-align-center\" data-align=\"center\">62.5ms<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">-5<\/td><td class=\"has-text-align-center\" data-align=\"center\">31.3ms<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">-6<\/td><td class=\"has-text-align-center\" data-align=\"center\">15.6ms<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">-7<\/td><td class=\"has-text-align-center\" data-align=\"center\">7.8ms<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">-8<\/td><td class=\"has-text-align-center\" data-align=\"center\">3.9ms<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">-9<\/td><td class=\"has-text-align-center\" data-align=\"center\">2ms<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">-10<\/td><td class=\"has-text-align-center\" data-align=\"center\">976.6\u00b5s<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">-11<\/td><td class=\"has-text-align-center\" data-align=\"center\">488.3\u00b5s<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">-12<\/td><td class=\"has-text-align-center\" data-align=\"center\">244.1\u00b5s<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">-13<\/td><td class=\"has-text-align-center\" data-align=\"center\">122.1\u00b5s<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>This results in a straight graph on the logarithmic scale.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"900\" height=\"557\" src=\"https:\/\/www.kurokesu.com\/main\/wp-content\/uploads\/2020\/05\/log2.png\" alt=\"\" class=\"wp-image-2632\" srcset=\"https:\/\/www.kurokesu.com\/main\/wp-content\/uploads\/2020\/05\/log2.png 900w, https:\/\/www.kurokesu.com\/main\/wp-content\/uploads\/2020\/05\/log2-300x186.png 300w, https:\/\/www.kurokesu.com\/main\/wp-content\/uploads\/2020\/05\/log2-768x475.png 768w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/figure>\n\n\n\n<p>And simple python code example is listed below. It Opens the camera, prepares fro MJPEG stream, and displays the real-time view on the screen.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import cv2\n\nprint(\"Initializing camera\")\n\ncamera = cv2.VideoCapture(0)\nprint(\"Setting camera mode\")\n\nexp_val = -6\n\ncodec = 0x47504A4D # MJPG\ncamera.set(cv2.CAP_PROP_FPS, 30.0)\ncamera.set(cv2.CAP_PROP_FOURCC, codec)\ncamera.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)\ncamera.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)\ncamera.set(cv2.CAP_PROP_EXPOSURE, exp_val)\n\nprint(\"Starting capture\")\nwhile(1):\n\tcamera.grab()\n\tretval, im = camera.retrieve(0)\n\tcv2.imshow(\"image\", im)\n\n\tk = cv2.waitKey(1) &amp; 0xff\n\tif k == 27:\n\t\tprint(\"exit\")\n\t\tbreak\n\ncamera.release()\ncv2.destroyAllWindows()\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>USB cameras can have the exposure set to manual (cheaper ones have only full auto mode). It\u2019s a pretty straight forward function in OpenCV, but insufficiently documented thus hard to find exact times and calculations. Also there are two major implementations for Linux and Windows. Linux \u2013 exposure times are set in units 1\/s (all [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2632,"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":[157],"tags":[62,423,49,424,87,422],"class_list":["post-2628","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-how-to","tag-camera","tag-exposure","tag-python","tag-time","tag-usb","tag-uvc"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/www.kurokesu.com\/main\/wp-content\/uploads\/2020\/05\/log2.png","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6VSmB-Go","_links":{"self":[{"href":"https:\/\/www.kurokesu.com\/main\/wp-json\/wp\/v2\/posts\/2628","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kurokesu.com\/main\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kurokesu.com\/main\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kurokesu.com\/main\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kurokesu.com\/main\/wp-json\/wp\/v2\/comments?post=2628"}],"version-history":[{"count":11,"href":"https:\/\/www.kurokesu.com\/main\/wp-json\/wp\/v2\/posts\/2628\/revisions"}],"predecessor-version":[{"id":2723,"href":"https:\/\/www.kurokesu.com\/main\/wp-json\/wp\/v2\/posts\/2628\/revisions\/2723"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kurokesu.com\/main\/wp-json\/wp\/v2\/media\/2632"}],"wp:attachment":[{"href":"https:\/\/www.kurokesu.com\/main\/wp-json\/wp\/v2\/media?parent=2628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kurokesu.com\/main\/wp-json\/wp\/v2\/categories?post=2628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kurokesu.com\/main\/wp-json\/wp\/v2\/tags?post=2628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}