{"id":1044,"date":"2015-09-22T11:31:48","date_gmt":"2015-09-22T18:31:48","guid":{"rendered":"http:\/\/visualgdb.com\/w\/?p=1044"},"modified":"2015-09-30T11:16:17","modified_gmt":"2015-09-30T18:16:17","slug":"using-opencv-library-on-raspberry-pi","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/raspberry\/opencv\/","title":{"rendered":"Using OpenCV library on Raspberry Pi"},"content":{"rendered":"<p>This tutorial shows how to use the OpenCV 2 library in cross-compiled projects for Raspberry Pi 2. Before you begin, download the <a href=\"http:\/\/gnutoolchains.com\/raspberry\/jessie\/\">Raspberry Pi Jessie image<\/a> and write it to the SD card. Then get a cross-toolchain compatible with the image from the <a href=\"http:\/\/gnutoolchains.com\/raspberry\/\">gnutoolchains.com<\/a> website.<\/p>\n<ol>\n<li>Connect to your Raspberry Pi via SSH and run the &#8220;sudo apt-get install libopencv-dev&#8221; command to install OpenCV:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/01-inst.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1048\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/01-inst.png\" alt=\"01-inst\" width=\"833\" height=\"506\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/01-inst.png 833w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/01-inst-300x182.png 300w\" sizes=\"(max-width: 833px) 100vw, 833px\" \/><\/a><\/li>\n<li>Start Visual Studio and open the VisualGDB Linux Project Wizard: <a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/02-linuxprj.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1049\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/02-linuxprj.png\" alt=\"02-linuxprj\" width=\"800\" height=\"450\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/02-linuxprj.png 800w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/02-linuxprj-300x169.png 300w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/a><\/li>\n<li>OpenCV comes with configuration files for CMake that simplify including the library from CMake projects. Hence we will use CMake to build our demo application. Select it in the wizard:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/03-newcmake.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1050\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/03-newcmake.png\" alt=\"03-newcmake\" width=\"696\" height=\"628\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/03-newcmake.png 696w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/03-newcmake-300x271.png 300w\" sizes=\"(max-width: 696px) 100vw, 696px\" \/><\/a><\/li>\n<li>Select &#8220;Build the project with a cross-compiler&#8221; and choose your Raspberry Pi board in the &#8220;deployment computer&#8221; field:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/04-cross.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1051\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/04-cross.png\" alt=\"04-cross\" width=\"696\" height=\"628\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/04-cross.png 696w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/04-cross-300x271.png 300w\" sizes=\"(max-width: 696px) 100vw, 696px\" \/><\/a><\/li>\n<li>Press &#8220;Finish&#8221; to create your project. Then open VisualGDB Project Properties, go to the CMake page and click &#8220;Synchronize sysroot&#8221;. Ensure that <strong>\/opt\/vc<\/strong> and <strong>\/usr\/share\/OpenCV<\/strong> (<span style=\"text-decoration: underline;\"><strong>not<\/strong><\/span> lowercase <strong>\/usr\/share\/opencv<\/strong>) are selected:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/05-resync.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1052\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/05-resync.png\" alt=\"05-resync\" width=\"817\" height=\"705\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/05-resync.png 817w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/05-resync-300x259.png 300w\" sizes=\"(max-width: 817px) 100vw, 817px\" \/><\/a><\/li>\n<li>Open the CMakeLists.txt file in the newly created project and add the following line after add_executable():\n<pre class=\"\">find_package(OpenCV REQUIRED)<\/pre>\n<p>Also add ${OpenCV_LIBS} to target_libraries() statement: <a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/06-findpkg1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1055\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/06-findpkg1.png\" alt=\"06-findpkg\" width=\"699\" height=\"603\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/06-findpkg1.png 699w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/06-findpkg1-300x259.png 300w\" sizes=\"(max-width: 699px) 100vw, 699px\" \/><\/a><\/li>\n<li>Open VisualGDB Project Properties and set the CMAKE_PREFIX_PATH variable for the CMake command to point at &lt;sysroot&gt;\\usr\\share\\OpenCV:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/07-prefix1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1054\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/07-prefix1.png\" alt=\"07-prefix\" width=\"817\" height=\"706\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/07-prefix1.png 817w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/07-prefix1-300x259.png 300w\" sizes=\"(max-width: 817px) 100vw, 817px\" \/><\/a><\/li>\n<li>Replace the contents of the main source file with the following code from the find_contours sample in OpenCV:\n<pre class=\"\">\/**\r\n\u00a0* @function findContours_Demo.cpp\r\n\u00a0* @brief Demo code to find contours in an image\r\n\u00a0* @author OpenCV team\r\n\u00a0*\/\r\n\r\n#include \"opencv2\/highgui\/highgui.hpp\"\r\n#include \"opencv2\/imgproc\/imgproc.hpp\"\r\n#include &lt;iostream&gt;\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nusing namespace cv;\r\nusing namespace std;\r\n\r\nMat src; Mat src_gray;\r\nint thresh = 100;\r\nint max_thresh = 255;\r\nRNG rng(12345);\r\n\r\n\/\/\/ Function header\r\nvoid thresh_callback(int, void*);\r\n\r\n\/**\r\n\u00a0* @function main\r\n\u00a0*\/\r\nint main(int, char** argv)\r\n{\r\n\u00a0 \/\/\/ Load source image and convert it to gray\r\n\u00a0\u00a0 \u00a0src = imread(argv[1], 1);\r\n\r\n\u00a0\u00a0 \u00a0\u00a0 \/\/\/ Convert image to gray and blur it\r\n\u00a0\u00a0 \u00a0cvtColor(src, src_gray, COLOR_BGR2GRAY);\r\n\u00a0\u00a0 \u00a0blur(src_gray, src_gray, Size(3, 3));\r\n\r\n\u00a0\u00a0 \u00a0\u00a0 \/\/\/ Create Window\r\n\u00a0\u00a0 \u00a0const char* source_window = \"Source\";\r\n\u00a0\u00a0 \u00a0namedWindow(source_window, WINDOW_AUTOSIZE);\r\n\u00a0\u00a0 \u00a0imshow(source_window, src);\r\n\r\n\u00a0\u00a0 \u00a0createTrackbar(\" Canny thresh:\", \"Source\", &amp;thresh, max_thresh, thresh_callback);\r\n\u00a0\u00a0 \u00a0thresh_callback(0, 0);\r\n\r\n\u00a0\u00a0 \u00a0waitKey(0);\r\n\u00a0\u00a0 \u00a0return (0);\r\n}\r\n\r\n\/**\r\n\u00a0* @function thresh_callback\r\n\u00a0*\/\r\nvoid thresh_callback(int, void*)\r\n{\r\n\u00a0\u00a0 \u00a0Mat canny_output;\r\n\u00a0\u00a0 \u00a0vector&lt;vector&lt;Point&gt; &gt; contours;\r\n\u00a0\u00a0 \u00a0vector&lt;Vec4i&gt; hierarchy;\r\n\r\n\u00a0\u00a0 \u00a0\u00a0 \/\/\/ Detect edges using canny\r\n\u00a0\u00a0 \u00a0Canny(src_gray, canny_output, thresh, thresh * 2, 3);\r\n\u00a0\u00a0 \u00a0\/\/\/ Find contours\r\n\u00a0\u00a0 \u00a0findContours(canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));\r\n\r\n\u00a0\u00a0 \u00a0\u00a0 \/\/\/ Draw contours\r\n\u00a0\u00a0 \u00a0Mat drawing = Mat::zeros(canny_output.size(), CV_8UC3);\r\n\u00a0\u00a0 \u00a0for (size_t i = 0; i &lt; contours.size(); i++)\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0drawContours(drawing, contours, (int)i, color, 2, 8, hierarchy, 0, Point());\r\n\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0\u00a0 \/\/\/ Show in a window\r\n\u00a0\u00a0 \u00a0namedWindow(\"Contours\", WINDOW_AUTOSIZE);\r\n\u00a0\u00a0 \u00a0imshow(\"Contours\", drawing);\r\n}<\/pre>\n<\/li>\n<li>Upload any PNG image that will be used to check out contour detection to the Raspberry Pi.\u00a0 In this tutorial we will use the Lena.png file from the OpenCV package:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/18-lena.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1011\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/18-lena.png\" alt=\"18-lena\" width=\"657\" height=\"140\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/18-lena.png 657w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/18-lena-300x64.png 300w\" sizes=\"(max-width: 657px) 100vw, 657px\" \/><\/a><\/li>\n<li>Specify the path to the uploaded image in the command-line arguments to your application via VisualGDB Project Properties:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/08-args.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1056\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/08-args.png\" alt=\"08-args\" width=\"817\" height=\"705\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/08-args.png 817w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/08-args-300x259.png 300w\" sizes=\"(max-width: 817px) 100vw, 817px\" \/><\/a><\/li>\n<li>Press F5 to start debugging the application. You will see two windows: one with the original image and another one with the contours:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/09-windows.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1057\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/09-windows.png\" alt=\"09-windows\" width=\"1215\" height=\"672\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/09-windows.png 1215w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/09-windows-300x166.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2015\/09\/09-windows-1024x566.png 1024w\" sizes=\"(max-width: 1215px) 100vw, 1215px\" \/><\/a>You can move the trackbar in the left window to change the threshold value for the edge detection algorithm.<\/li>\n<\/ol>\n<p>To learn how to cross-compile OpenCV 3 for Raspberry Pi and step through its source code, follow <a href=\"http:\/\/visualgdb.com\/tutorials\/raspberry\/opencv\/build\/\">this tutorial<\/a>.<\/p>\n<p>Read <a href=\"http:\/\/visualgdb.com\/tutorials\/raspberry\/opencv\/camera\/\">this tutorial<\/a> for detailed instructions on using OpenCV with the Raspberry Pi camera module.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to use the OpenCV 2 library in cross-compiled projects for Raspberry Pi 2. Before you begin,<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[33,85],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/1044"}],"collection":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/comments?post=1044"}],"version-history":[{"count":6,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/1044\/revisions"}],"predecessor-version":[{"id":1087,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/1044\/revisions\/1087"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=1044"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=1044"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=1044"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}