{"id":587,"date":"2023-02-06T22:32:20","date_gmt":"2023-02-06T11:32:20","guid":{"rendered":"https:\/\/www.samontab.com\/web\/?p=587"},"modified":"2023-02-09T01:12:51","modified_gmt":"2023-02-08T14:12:51","slug":"how-to-use-the-oak-d-spatial-camera-c-interface-with-opencv-in-ubuntu-22-04-lts","status":"publish","type":"post","link":"https:\/\/www.samontab.com\/web\/2023\/02\/how-to-use-the-oak-d-spatial-camera-c-interface-with-opencv-in-ubuntu-22-04-lts\/","title":{"rendered":"How to use the Oak-D spatial camera C++ interface with OpenCV in Ubuntu 22.04 LTS"},"content":{"rendered":"\n<p>The <a rel=\"noreferrer noopener\" href=\"https:\/\/www.amazon.com\/Luxonis-Oak-D-Spatial-Camera-Detection\/dp\/B09B316YZS?crid=O0G4B1RZZVRB&amp;keywords=oak-d&amp;qid=1675681230&amp;sprefix=o%2Caps%2C1246&amp;sr=8-2&amp;linkCode=ll1&amp;tag=sebmonmypasan-20&amp;linkId=9e2206d3100107e96da919fcb1eda428&amp;language=en_US&amp;ref_=as_li_ss_tl\" target=\"_blank\">Luxonis Oak-D<\/a>(affiliate link) is a great infrared stereo camera. It also comes with an RGB camera, and the Intel Movidius MyriadX chip to perform neural inference inside the device. They&#8217;ve named it a &#8220;spatial camera&#8221; because of all these features. It&#8217;s quite a bargain for its price, and it&#8217;s also quite compact, so it&#8217;s perfect for robotic and computer vision applications in general. If you don&#8217;t have one of these cameras and would like to get one, check out <a href=\"https:\/\/www.samontab.com\/web\/2023\/02\/overview-of-current-luxonis-oak-cameras-or-which-one-you-should-get-for-your-next-computer-vision-application\/\" target=\"_blank\" rel=\"noreferrer noopener\">this blog post<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2023\/02\/cam.jpg\" alt=\"\" class=\"wp-image-589\" srcset=\"https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2023\/02\/cam.jpg 1024w, https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2023\/02\/cam-300x200.jpg 300w, https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2023\/02\/cam-768x512.jpg 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">The Oak-D camera. It has two IR cameras and one RGB camera. It has a USB-C connector, and an extra power connector in case the host cannot provide enough power.<\/figcaption><\/figure>\n\n\n\n<p>In this post I&#8217;ll show you how to start using it in Ubuntu to make your own computer vision applications with OpenCV. First, let&#8217;s make sure you have an updated system:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nsudo apt-get update\nsudo apt-get upgrade\n<\/pre><\/div>\n\n\n<p>Now let&#8217;s get some dependencies that are needed to build the Oak-D library and the examples:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nsudo apt-get install cmake build-essential git libopencv-dev\n<\/pre><\/div>\n\n\n<p>Now we&#8217;re ready to grab the source code, compile it, and install it:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\ncd ~\nmkdir oakd\ncd oakd\nwget https:\/\/github.com\/luxonis\/depthai-core\/releases\/download\/v2.20.2\/depthai-core-v2.20.2.tar.gz\ntar -xzvf depthai-core-v2.20.2.tar.gz\ncd depthai-core-v2.20.2\/\nmkdir build\ncd build\ncmake -D BUILD_SHARED_LIBS=OFF -D DEPTHAI_BUILD_EXAMPLES=ON -D DEPTHAI_OPENCV_SUPPORT=ON -DCMAKE_INSTALL_PREFIX=\/usr\/local ..\nmake -j10\nsudo make install\n<\/pre><\/div>\n\n\n<p>Now the camera library is installed, but it is only accessible using sudo, so let&#8217;s set the udev rules to allow normal users to access it(<strong>make sure to disconnect the camera now<\/strong>):<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\necho &#039;SUBSYSTEM==&quot;usb&quot;, ATTRS{idVendor}==&quot;03e7&quot;, MODE=&quot;0666&quot;&#039; | sudo tee \/etc\/udev\/rules.d\/80-movidius.rules\nsudo udevadm control --reload-rules &amp;&amp; sudo udevadm trigger\n<\/pre><\/div>\n\n\n<p>And now <strong>connect the camera again<\/strong>, the new rules should apply, and a normal user should be able to access it. Try running some of the demos(you can press <strong>q<\/strong> to exit):<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\ncd ~\/oakd\/depthai-core-v2.20.2\/build\/examples\n.\/depth_preview\n.\/rgb_preview\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"634\" height=\"510\" src=\"https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2023\/02\/a.png\" alt=\"\" class=\"wp-image-590\" srcset=\"https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2023\/02\/a.png 634w, https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2023\/02\/a-300x241.png 300w\" sizes=\"auto, (max-width: 634px) 100vw, 634px\" \/><figcaption class=\"wp-element-caption\">Depth frame obtained from the camera. Whiter pixels mean they are closer to the camera.<\/figcaption><\/figure>\n\n\n\n<p>In the depth preview you&#8217;ll see gray scale pixels representing depth. The closer the pixel is to the camera, the higher value(whiter) it would be. The RGB preview simply shows the RGB camera. Make sure to explore the rest of the examples to see the capabilities of the camera.<\/p>\n\n\n\n<p>The simplest way to build your own application is to copy one example and use it as the starting point, using the same building pipeline. For example, let&#8217;s write an app that gets the depth data like the depth_preview example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\ncd ~\/oakd\/depthai-core-v2.20.2\/examples\nmkdir myapp\ncd myapp\ncp ..\/StereoDepth\/depth_preview.cpp main.cpp\n<\/pre><\/div>\n\n\n<p>We now have a main.cpp file with the code for our application. Make a tiny change to the code so that you know it&#8217;s a different file, open main.cpp and make this change:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n\/\/cv::imshow(&quot;disparity&quot;, frame)\ncv::imshow(&quot;mydisparity&quot;, frame)\n<\/pre><\/div>\n\n\n<p>Now let&#8217;s add our application to the building pipeline, alongside the other examples. Edit this file:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nnano ~\/oakd\/depthai-core-v2.20.2\/examples\/CMakeLists.txt\n<\/pre><\/div>\n\n\n<p>At the end of the file, simply write this one line and save the file:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\ndai_add_example(my_oak_app myapp\/main.cpp ON)\n<\/pre><\/div>\n\n\n<p><strong>my_oak_app<\/strong> is the name of your executable, and myapp\/main.cpp is where the source code is. Let&#8217;s build it and run it:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\ncd ~\/oakd\/depthai-core-v2.20.2\/build\/\nmake\ncd examples\n.\/my_oak_app\n<\/pre><\/div>\n\n\n<p>You should now see the same depth_preview application but the window name should be &#8220;mydisparity&#8221; instead. <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"635\" height=\"522\" src=\"https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2023\/02\/b.png\" alt=\"\" class=\"wp-image-591\" srcset=\"https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2023\/02\/b.png 635w, https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2023\/02\/b-300x247.png 300w\" sizes=\"auto, (max-width: 635px) 100vw, 635px\" \/><figcaption class=\"wp-element-caption\">Same application as depth_preview but now we have full control of the source code.<\/figcaption><\/figure>\n\n\n\n<p>That&#8217;s it, remember to explore the other examples, and also check out the <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.luxonis.com\/projects\/api\/en\/latest\/references\/cpp\/#\" target=\"_blank\">Luxonis C++ API Reference<\/a> to see all that you can do with these incredible cameras.<\/p>\n\n\n\n<p>Enjoyed the tutorial?<\/p>\n\n\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Luxonis Oak-D(affiliate link) is a great infrared stereo camera. It also comes with an RGB camera, and the Intel Movidius MyriadX chip to perform neural inference inside the device. They&#8217;ve named it a &#8220;spatial camera&#8221; because of all these features. It&#8217;s quite a bargain for its price, and it&#8217;s also quite compact, so it&#8217;s [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29,22,21,30,6,4],"tags":[],"class_list":["post-587","post","type-post","status-publish","format-standard","hentry","category-computer-vision","category-iot","category-open-source","category-opencv","category-photography","category-programming"],"_links":{"self":[{"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/posts\/587","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/comments?post=587"}],"version-history":[{"count":0,"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/posts\/587\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/media?parent=587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/categories?post=587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/tags?post=587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}