{"id":29,"date":"2010-03-20T03:34:49","date_gmt":"2010-03-20T03:34:49","guid":{"rendered":"http:\/\/grandmaster.student.utwente.nl\/?page_id=29"},"modified":"2010-03-20T03:34:49","modified_gmt":"2010-03-20T03:34:49","slug":"opencv-2-0-c","status":"publish","type":"page","link":"https:\/\/www.grandmaster.nu\/blog\/?page_id=29","title":{"rendered":"OpenCV 2.0 Tutorial 01"},"content":{"rendered":"<p>For research purpouses I needed to get to know the <a href=\"http:\/\/opencv.willowgarage.com\/wiki\/\">openCV<\/a> 2.0 library in detail, and they&#8217;ve recently introduced a C++ interface. There was no real tutorial, so I&#8217;m starting one myself. Keep in mind that I am still learning the computer vision stuff myself, but I&#8217;ll be as clear as I can be at everything I use.<\/p>\n<p>I&#8217;m using the <a href=\"http:\/\/www.microsoft.com\/express\/Windows\/\">Visual Studio 9<\/a> compiler and the focus for now is the <a href=\"http:\/\/opencv.willowgarage.com\/wiki\/\">openCV<\/a> library, although there will be some work done with the <a href=\"http:\/\/muonics.net\/school\/spring05\/videoInput\/\">VideoInput <\/a>library and <a href=\"http:\/\/www.openscenegraph.org\/\">OpenSceneGraph<\/a>.<\/p>\n<p>I prefer to work with stuff that you can see working, so let&#8217;s start with a trivial program that reads and displays a single image:<\/p>\n<pre lang=\"c++\">#include <cv.h>\n#include <highgui.h>\n\nvoid main() {\n cv::Mat& loadedImage = cv::imread(\"picture.jpg\", 1);\n cv::namedWindow(\"picture window\", CV_WINDOW_AUTOSIZE);\n cv::imshow(\"picture window\", loadedImage);\n cv::waitKey(0);\n}<\/pre>\n<p>Nice and simple, although some details may be of interest. The picture is stored in <em>loadedImage<\/em> through the use of the <em>imread<\/em> function. The function takes a filename and a flag and produces a <em>Mat<\/em> object. There are three possible flags, which are used to determine the color type of the resulting <em>Mat<\/em>. A flag of 0 results in a grayscale image, anything above 0 results in a 3-channel color image, and anything below 0 retains the original image format. Note that if your image has an additional alpha channel (RGBA format or similar), you will need to use a negative flag to actually load it correctly. If the loading fails for whatever reason, it returns an empty <em>Mat<\/em>, which has a NULL data field. By default openCV supports many common file formats, such as .bmp, .jpg, .png, .ppm and more.<\/p>\n<p>Also, the <em>waitKey<\/em> function is slightly misleading, as it does more than just wait for keys. It also handles the window message pump, enabling &#8216;default behaviour&#8217; for the window. The argument indicates the number of milliseconds the function waits for a key, with 0 indicating an indefinate wait. The return value of waitKey is equal to the keycode of the pressed key, or -1 if no key was pressed in the alotted time. This means that for example a result of 27 means that the escape key was pressed. This type of setup makes it convenient to create simple keyboard interactions, but some key (combinations) may give you trouble (for example pressing the shift alone doesn&#8217;t register anything).<\/p>\n<p>Let&#8217;s leave it at that for this one. If you get this to work, you&#8217;re ready to go and explore a bit. Next time, live video input.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For research purpouses I needed to get to know the openCV 2.0 library in detail, and they&#8217;ve recently introduced a C++ interface. There was no real tutorial, so I&#8217;m starting one myself. Keep in mind that I am still learning the computer vision stuff myself, but I&#8217;ll be as clear <a class=\"more-link\" href=\"https:\/\/www.grandmaster.nu\/blog\/?page_id=29\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":25,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"ngg_post_thumbnail":0,"footnotes":""},"_links":{"self":[{"href":"https:\/\/www.grandmaster.nu\/blog\/index.php?rest_route=\/wp\/v2\/pages\/29"}],"collection":[{"href":"https:\/\/www.grandmaster.nu\/blog\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.grandmaster.nu\/blog\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.grandmaster.nu\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.grandmaster.nu\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=29"}],"version-history":[{"count":0,"href":"https:\/\/www.grandmaster.nu\/blog\/index.php?rest_route=\/wp\/v2\/pages\/29\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.grandmaster.nu\/blog\/index.php?rest_route=\/wp\/v2\/pages\/25"}],"wp:attachment":[{"href":"https:\/\/www.grandmaster.nu\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}