<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jean-Marc Pelletier &#187; Tutorials</title>
	<atom:link href="http://jmpelletier.com/category/tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>http://jmpelletier.com</link>
	<description>Sound, art, media, education</description>
	<lastBuildDate>Thu, 20 Oct 2011 08:20:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>A Simple OpenCV Tutorial</title>
		<link>http://jmpelletier.com/a-simple-opencv-tutorial/</link>
		<comments>http://jmpelletier.com/a-simple-opencv-tutorial/#comments</comments>
		<pubDate>Fri, 08 May 2009 05:39:38 +0000</pubDate>
		<dc:creator>jovansystem</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Computer vision]]></category>
		<category><![CDATA[OpenCV]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/2009/a-simple-opencv-tutorial/</guid>
		<description><![CDATA[Here is the source code for a simple program in C that uses the OpenCV library. The OpenCV package already contains several examples and fairly decent documentation, but the code you&#8217;ll find here is extra commented. It doesn&#8217;t do anything terribly interesting by itself: it captures images from the camera, finds edges and contours and [...]]]></description>
			<content:encoded><![CDATA[<p>Here is the source code for a simple program in C that uses the <a href="http://sourceforge.net/projects/opencvlibrary/">OpenCV</a> library. The OpenCV package already contains several examples and fairly decent documentation, but the code you&#8217;ll find here is <span style="font-style: italic;">extra </span>commented. It doesn&#8217;t do anything terribly interesting by itself: it captures images from the camera, finds edges and contours and displays them. When the user clicks on the window, it tries to find a contour that surrounds the location the user clicked in. If there is one, it prints out the area of this contour.</p>
<div>This example is meant to show how to use the basic GUI support offered by OpenCV, such as creating windows, trackbars (sliders) and responding to mouse and keyboard events. It shows a basic image processing chain (resize, convert to greyscale, find edges, dilate), but most importantly it also shows how one can work with sequences.</div>
<div>Sequences are a type of linked list used extensively in OpenCV, but the documentation is somewhat less than perfectly clear about how they&#8217;re organized. There are utility functions to iterate through these sequences, but accessing the pointers directly, as I do in this example can be just as clear.</div>
<div>In the example, I use the function <span style="color: #2e0d6e; font-family: Monaco; font-size: 10px; ">cvFindContours</span>, which returns sequences of points that are connected together along an image edge. The flag <span style="color: #643820; font-family: Monaco; font-size: 10px; ">CV_RETR_TREE </span>tells the function to organize the contours in a hierarchical tree. What does this mean? The contours are stored in a structure called <span style="color: #5c2699; font-family: Monaco; font-size: 10px; ">CvSeq<span style="color: #333333; font-family: arial; font-size: 13px; ">. You can tell how many points are in the contour stored in a <span style="color: #5c2699; font-family: Monaco; font-size: 10px; ">CvSeq </span>by looking at its <span style="color: #5c2699; font-family: Monaco; font-size: 10px; ">total </span>member. If you have a <span style="color: #5c2699; font-family: Monaco; font-size: 10px; ">CvSeq </span>called <span style="font-family: Monaco; font-size: 10px; ">contour<span style="font-family: arial; font-size: 13px; ">, you can iterate through its points like this:</span></span></span></span></div>
<blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><p><span style="font-family: Monaco; font-size: 10px; "><span style="color: #aa0d91">int</span> i;</span></p></blockquote>
<blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><p><span style="font-family: Monaco; font-size: 10px; "><span style="color: #aa0d91">for</span>(i=<span style="color: #1c00cf">0</span>;i&lt;contour.<span style="color: #5c2699">total</span>;i++){</span></p></blockquote>
<p style="font: normal normal normal 10px/normal Monaco; padding-left: 60px; margin: 0px;"><span style="white-space:pre"> </span><span style="color: #5c2699">CvPoint</span> *point =  (<span style="color: #5c2699">CvPoint</span> *)<span style="color: #643820">CV_GET_SEQ_ELEM</span>(<span style="color: #5c2699">CvPoint</span>,&amp;contour,i);</p>
<blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><p><span style="font-family: Monaco; font-size: 10px; ">}</span></p></blockquote>
<div>You can access other contours using the <span style="color: #5c2699; font-family: Monaco; font-size: 10px;">h_next<span style="color: #333333; font-family: arial; font-size: 13px; "> and<span style="color: #5c2699; font-family: Monaco; font-size: 10px; "> v_next </span>members of <span style="color: #5c2699; font-family: Monaco; font-size: 10px; ">CvSeq<span style="color: #333333; font-family: arial; font-size: 13px; ">. If you specified <span style="color: #643820; font-family: Monaco; font-size: 10px; ">CV_RETR_TREE, <span style="color: #333333; font-family: arial; font-size: 13px; ">the pointer <span style="color: #5c2699; font-family: Monaco; font-size: 10px; ">v_next<span style="color: #333333; font-family: arial; font-size: 13px; "> points to the first contour that is inside the current contour. On the other hand, <span style="color: #5c2699; font-family: Monaco; font-size: 10px; ">h_next<span style="color: #333333; font-family: arial; font-size: 13px; "> points to the first contour that is outside the current contour. For example:</span></span></span></span></span></span></span></span></span></span></div>
<div><span style="color: #5c2699; font-family: Monaco; font-size: 10px;"><span style="color: #333333; font-family: arial; font-size: 13px; "><span style="color: #5c2699; font-family: Monaco; font-size: 10px; "><span style="color: #333333; font-family: arial; font-size: 13px; "><span style="color: #643820; font-family: Monaco; font-size: 10px; "><span style="color: #333333; font-family: arial; font-size: 13px; "><span style="color: #5c2699; font-family: Monaco; font-size: 10px; "><span style="color: #333333; font-family: arial; font-size: 13px; "><span style="color: #5c2699; font-family: Monaco; font-size: 10px; "><span style="color: #333333; font-family: arial; font-size: 13px; "><br />
</span></span></span></span></span></span></span></span></span></span></div>
<div style="text-align: center;"><span class="mt-enclosure mt-enclosure-image" style="display: inline;"><img class="aligncenter size-full wp-image-239" title="Edges" src="/wp-content/uploads/2009/05/Edges.gif" alt="Edges" width="320" height="240" /></span></div>
<div style="text-align: center;"><span class="mt-enclosure mt-enclosure-image" style="display: inline;"><br />
</span></div>
<div style="text-align: left;">If we give the binary image above to <span style="color: #2e0d6e; font-family: Monaco; font-size: 10px; ">cvFindContours, </span>it will find the contours shown on the left below, labeled A to L. You can access A through the <span style="color: #5c2699; font-family: Monaco; font-size: 10px; ">CvSeq </span>structure <span style="color: #2e0d6e; font-family: Monaco; font-size: 10px; ">cvFindContours </span>expects as its third argument. All the other contours can be accessed through the <span style="color: #5c2699; font-family: Monaco; font-size: 10px; ">h_next <span style="color: #333333; font-family: arial; font-size: 13px; ">and <span style="color: #5c2699; font-family: Monaco; font-size: 10px; ">v_next </span>pointers, as shown on the right.</span></span></div>
<div style="text-align: left;"><span style="color: #5c2699; font-family: Monaco; font-size: 10px; "><span style="color: #333333; font-family: arial; font-size: 13px; "><br />
</span></span></div>
<div style="text-align: left;"><span class="mt-enclosure mt-enclosure-image" style="display: inline;"><img class="aligncenter size-full wp-image-240" title="Contours" src="/wp-content/uploads/2009/05/Contours.gif" alt="Contours" width="472" height="240" /></span></div>
<div style="text-align: left;"><span class="mt-enclosure mt-enclosure-image" style="display: inline;"><br />
</span></div>
<div style="text-align: left;">Here is the source file for the tutorial: <a href="/data/OpenCVcontours.c" target="_blank">OpenCVcontours.c</a></div>
<div style="text-align: left;">If you are running this example on OS X, the #include commands expect OpenCV to be packaged as a framework. Alter these lines accordingly if this is not the case.</div>
<div><span style="color: #5c2699; font-family: Monaco; font-size: 10px;"><span style="color: #333333; font-family: arial; font-size: 13px; "><span style="color: #5c2699; font-family: Monaco; font-size: 10px; "><span style="color: #333333; font-family: arial; font-size: 13px; "><span style="color: #5c2699; font-family: Monaco; font-size: 10px; "> </span></span></span></span></span></div>
]]></content:encoded>
			<wfw:commentRss>http://jmpelletier.com/a-simple-opencv-tutorial/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

