<?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>Xavi Gimenez</title>
	<atom:link href="http://www.xavigimenez.net/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.xavigimenez.net/blog</link>
	<description>Data visualization and interactive stuff</description>
	<lastBuildDate>Tue, 29 Nov 2011 22:04:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Visualizing a train timetable using a stem-and-leaf plot</title>
		<link>http://www.xavigimenez.net/blog/2011/11/visualizing-a-train-timetable-using-a-stem-and-leaf-plot-2/</link>
		<comments>http://www.xavigimenez.net/blog/2011/11/visualizing-a-train-timetable-using-a-stem-and-leaf-plot-2/#comments</comments>
		<pubDate>Fri, 25 Nov 2011 22:57:36 +0000</pubDate>
		<dc:creator>Xavi Gimenez</dc:creator>
				<category><![CDATA[Data Visualization]]></category>
		<category><![CDATA[plot]]></category>
		<category><![CDATA[statistics]]></category>

		<guid isPermaLink="false">http://www.xavigimenez.net/blog/?p=599</guid>
		<description><![CDATA[Which is the best way to visualize effectively a train timetable? Well probably there is a lot of ways to visualize this kind of data, and one of them is using the stem-and-leaf plot (called also stemplot). Using this technique the amount of data to display (hours and minutes) can be reduced.
Why are the stem-and-leaf [...]]]></description>
			<content:encoded><![CDATA[<p>Which is the best way to visualize effectively a train timetable? Well probably there is a lot of ways to visualize this kind of data, and one of them is using the stem-and-leaf plot (called also stemplot). Using this technique the amount of data to display (hours and minutes) can be reduced.</p>
<p>Why are the stem-and-leaf plots useful in that case? This kind of plot is a method for showing the frequency with which certain classes of values occur. You could achieve the same by making a frequency distribution table or a histogram for the values, or you can use a stem-and-leaf plot and let the numbers themselves to show pretty much the same information.<br />
<span id="more-599"></span><br />
For our case, we have a train timetable that can be displayed in that way:<br />
<img title="train_timetable_1" src="http://www.xavigimenez.net/blog/wp-content/uploads/2011/11/Captura-de-pantalla-2011-11-24-a-las-23.10.46.png" alt="" width="444" height="198" /><br />
<br clear="all"/><br />
Pretty boring right? Are you able to see how frequently trains leave at a given hour? which are the rush hours? how many trains leave at 11pm? In fact, it&#8217;s really hard to detect any pattern here.</p>
<p>Using a stem-and-leaf plot we can show the train schedule below, where for each departure time, the hour-digits are the stem (left column) and the minute-digits are the leaves (right columns). So hour-digits are not repeated over and over and minute-digits can be stacked, so we obtain kind of an histogram of the departure times.<br />
<img class="aligncenter size-full wp-image-594" title="train_timetable_2" src="http://www.xavigimenez.net/blog/wp-content/uploads/2011/11/Captura-de-pantalla-2011-11-24-a-las-23.29.32.png" alt="" width="196" height="356" /><br />
<br clear="all"/><br />
We could reduce more the amount of data to display by grouping the departure-hours that have the same minute-departures frequency, obtaining this plot:<br />
<img class="alignnone size-full wp-image-601" title="train_timetable_3" src="http://www.xavigimenez.net/blog/wp-content/uploads/2011/11/Captura-de-pantalla-2011-11-24-a-las-23.36.321.png" alt="" width="350" height="182" /><br />
<br clear="all"/><br />
A great example of this technique can be found at <a href="http://en.wikipedia.org/wiki/File:Stem-and-leaf_time_tables_in_Japanese_train_stations.jpg" target="_blank">many train stations in Japan</a>, where this plot is widely used to display the timetables. The resulting plot is useful, as it gives a quick overview of the departures distributions and it lets also to find an specific departure really fast, which is the main purpose of a train timeable.</p>
<p><img src="http://www.xavigimenez.net/blog/wp-content/uploads/2011/11/Stem-and-leaf_time_tables_in_Japanese_train_stations-e1322259088755.jpg" alt="" title="Stem-and-leaf_time_tables_in_Japanese_train_stations" width="549" height="381" class="size-full wp-image-645" /></p>
<p><em>Time table at Yokohama\&#8217;s Minato Mirai train station in Japan illustrating the widespread use of stem-and-leaf graphs in the country. Author: <a href="http://en.wikipedia.org/wiki/User:Eliazar" target="_blank">Eliazar</a></em></p>
<p>However, the data could be displayed in a different way, so I&#8217;ve spent some time playing with the plot to find another way to visualize the data. Looking at the plot, there are a lot of departure hours that share several departure minutes, so a lot of minute-digits are repeated over and over&#8230; Also, with the stem-and-plot is easy to detect the density of departures based on hours, but, what about the density of departures based on the minutes? Maybe that does not seems really useful, but who knows? So after playing some time with these questions, I end up with this table below:<br />
<img src="http://www.xavigimenez.net/blog/wp-content/uploads/2011/11/Captura-de-pantalla-2011-11-25-a-las-00.46.36-e1322178404903.png" alt="" title="train_timetable_4" width="548" height="259" class="alignnone size-full wp-image-628" /><br />
<br clear="all"/><br />
The graphic is nothing new, and the result is not a stem-and-leaf plot anymore, but it has its roots on it. As you can see, hour-digits and minute-digits are displayed only once, so here the black dots are the departure times. Also the color is used as a visual clue to communicate the density of the departures for each hour/minute departures. Althought the distance between the points is not precise at all (the spatial distance between the minutes is not proportional) , the distance between dots gives you and idea about the time to wait until the next departure.</p>
<p>Still some improvements can be added to the graphic, but it illustrates how data can be displayed in a graphical format in order to communicate better the information contained in the data itself.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.xavigimenez.net/blog/2011/11/visualizing-a-train-timetable-using-a-stem-and-leaf-plot-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Open Data for Catalonia and Data Visualization from Barcelona</title>
		<link>http://www.xavigimenez.net/blog/2010/11/open-data-for-catalonia-and-data-visualization-from-barcelona/</link>
		<comments>http://www.xavigimenez.net/blog/2010/11/open-data-for-catalonia-and-data-visualization-from-barcelona/#comments</comments>
		<pubDate>Mon, 29 Nov 2010 16:00:11 +0000</pubDate>
		<dc:creator>Xavi Gimenez</dc:creator>
				<category><![CDATA[Data Visualization]]></category>
		<category><![CDATA[Open Data]]></category>
		<category><![CDATA[Actionscript]]></category>

		<guid isPermaLink="false">http://www.xavigimenez.net/blog/?p=444</guid>
		<description><![CDATA[Lately I&#8217;m pleasantly surprised due to two great initiatives which demonstrate the interest that has Catalunya for the world of the open data and the data visualization.
The first one demonstrates how the catalan government is sensible about the value of providing data to the general public, businesses and other organisations so they would be able [...]]]></description>
			<content:encoded><![CDATA[<p>Lately I&#8217;m pleasantly surprised due to two great initiatives which demonstrate the interest that has Catalunya for the world of the open data and the data visualization.</p>
<p>The first one demonstrates how the catalan government is sensible about the value of providing data to the general public, businesses and other organisations so they would be able to re-use public sector data and create value. </p>
<p>The second one is from my point of view such an awesome project that needs an entire post but for the moment, remember that name and its concept: Impure, a visual programming interface designed to give non-programmers access to professional tools for data visualization. Users can use it to process and display data from social media feeds, financial information and more.</p>
<p>Let&#8217;s go back to the first initiative:</p>
<h3><a href="http://dadesobertes.gencat.cat/en/index.html" target="_blank">Government of Catalonia Open Data Project</a></h3>
<p><a href="http://www.xavigimenez.net/blog/wp-content/uploads/2010/11/opendata_gencat.jpg" target="_blank"><img src="http://www.xavigimenez.net/blog/wp-content/uploads/2010/11/opendata_gencat.jpg" alt="Government of Catalonia Open Data Project" title="opendata_gencat" width="550" height="66" class="alignnone size-full wp-image-465" /></a><br />
The government of Catalonia has launched <a href="http://dadesobertes.gencat.cat/en/index.html" target="_blank">Gencat Open Data</a>, an open government data portal where information of a public nature is published with the goal of fostering the use and reuse of information that comes from the administration. This portal groups together all the Government’s open data initiatives into a single catalogue, and adds the most important information associated with them for reuse purposes.<br />
<span id="more-444"></span><br />
The offered data set comprises the database of the 26,000 official facilities of Catalonia, the 1,400 procedures handled in the Government’s offices and some of its multimedia archives.</p>
<p>The catalogue is divided into this categories:</p>
<ul>
<li>Administrative procedures and services</li>
<li>Statistical data</li>
<li>Geographic and cartographic data</li>
<li>Visual data</li>
<li>Language data</li>
<li>Weather data</li>
<li>News</li>
<li>Transport and mobility</li>
</ul>
<p>Also delivers the data in different types of <a href="http://dadesobertes.gencat.cat/en/formats.html" target="_blank">formats </a>(doc, xls, shp, pdf, odf, csv, xml, tmx, json, rdf-xml, etc&#8230;). The data is available under <a href="http://dadesobertes.gencat.cat/en/com-utilitzar-dades.html" target="_blank">4 possible licences</a>, among which two creative commons licences.</p>
<p>So, what can we do with this data? you can plug it to Impure!<br />
<a href="http://www.impure.com/" target="_blank"><br />
<h3>Impure</h3>
<p></a><a href="http://www.xavigimenez.net/blog/wp-content/uploads/2010/11/impure.jpg" target="_blank"><img src="http://www.xavigimenez.net/blog/wp-content/uploads/2010/11/impure.jpg" alt="" title="impure" width="550" height="356" class="alignnone size-full wp-image-501" /></a><br />
<a href="<a href="http://www.impure.com/" target="_blank">Impure</a> has been created by <a href="http://bestiario.org/" target="_blank">Bestiario</a>, a Barcelona Spain based start-up, has been exploring and inventing new interactive visualization paradigms over the past 5 years.</p>
<p><a href="http://www.impure.com/" target="_blank">Impure</a> is a visual programming language aimed to gather, process and visualize information. With impure it is possible to obtain information from many different sources; from user owned data to diverse feeds in Internet, including social media data, real time or historical financial information, images, news, search queries and many more. It is a tool to be in touch with data around the Internet, to deeply understand it. Within a modular logic interface you can quickly link information to operators, controls and visualization methods, bringing all the power of the comprehension of information and knowledge to the non programmers that want to work with information in a professional way. </p>
<p>Among other possibilities, impure allows you to:</p>
<ul>
<li>Easily read data from diverse sources and repositories </li>
<li>Load your own data locally or remotely </li>
<li>Visualize it in a wide range of ways (more than 100 visualization methods so far) </li>
<li>Process it, compare it, mix it, filter it, (more than 300 controls and operations so far) </li>
<li>Publish and share your projects</li>
</ul>
<p>The application provides a set of examples to getting started, as well as good <a href="http://docs.impure.com/" target="_blank">documentation</a> and a <a href="http://wiki.impure.com/" target="_blank">wiki</a>. Don&#8217;t forget to visit also its <a href="http://www.flickr.com/photos/53824152@N08/" target="_blank">impure gallery&#8217;s photostream</a> on flickr.</p>
<p>Definitively, a great tool built by the guys of Bestiario. Oh! did I mention that it&#8217;s built in flash? ;)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.xavigimenez.net/blog/2010/11/open-data-for-catalonia-and-data-visualization-from-barcelona/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Peer 2 peer and the flash platform</title>
		<link>http://www.xavigimenez.net/blog/2010/08/peer-2-peer-and-the-flash-platform/</link>
		<comments>http://www.xavigimenez.net/blog/2010/08/peer-2-peer-and-the-flash-platform/#comments</comments>
		<pubDate>Mon, 16 Aug 2010 22:02:34 +0000</pubDate>
		<dc:creator>Xavi Gimenez</dc:creator>
				<category><![CDATA[AIR]]></category>
		<category><![CDATA[Actionscript]]></category>

		<guid isPermaLink="false">http://www.xavigimenez.net/blog/?p=406</guid>
		<description><![CDATA[Today I&#8217;ve attended the  MadeInFlex onsite IV, an event where some Adobe Platform evangelists have been talking about the last features of AIR 2.5, Flash Player 10.1 for Android, the integration between Flex 4 and Flash professional and other interesting stuff.
I&#8217;ve really got impressed about the posibilities that Flash and P2P can offer together. [...]]]></description>
			<content:encoded><![CDATA[<p>Today I&#8217;ve attended the <a href="http://www.madeinflex.com/2010/08/10/mas-sobre-el-mif-onsite-iv/" target="_blank"> MadeInFlex onsite IV</a>, an event where some Adobe Platform evangelists have been talking about the last features of AIR 2.5, Flash Player 10.1 for Android, the integration between Flex 4 and Flash professional and other interesting stuff.</p>
<p>I&#8217;ve really got impressed about the posibilities that Flash and P2P can offer together. Specially interesting it has been the demo of Mark Doherty, showing how comunicate via p2p a laptop with an Android mobile device, using a second Android mobile device as a net access point. It was just a demo built with the AIR 2.5 beta but it shows what this technology can offer in reponse to other similiar technologies (the video chat <a href="http://www.apple.com/iphone/features/facetime.html" target="_blank">FaceTime</a> for Iphone). Mark shows this in a video on his <a href="http://www.flashmobileblog.com/2010/07/17/p2p-video-calls-on-android/" target="_blank">Flash Mobile Blog</a>.</p>
<p><object width="400" height="225"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=13410620&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=1&amp;color=&amp;fullscreen=1&amp;autoplay=0&amp;loop=0" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=13410620&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=1&amp;color=&amp;fullscreen=1&amp;autoplay=0&amp;loop=0" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="225"></embed></object>
<p><a href="http://vimeo.com/13410620">P2P Video Demo &#8211; AIR2.5 on Android</a> from <a href="http://vimeo.com/flashmobileblog">Mark Doherty</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<p><span id="more-406"></span></p>
<p>Another Adobe Platform evangelist, Tom Krcha, also has spoken about P2P and Flash, explaining what is peer-to-peer, differences from a centralized server-side model, using <a href="http://labs.adobe.com/technologies/stratus/" target="_blank">Stratus</a> as a hosted rendezvous service, etc. Tom Krach&#8217;s blog contains <a href="http://www.flashrealtime.com/e-seminar-materials-p2p-programming-in-flash/" target="_blank">tons of information</a> and <a href="http://www.flashrealtime.com/category/video-tutorial/" target="_blank">tutorials</a> about these technologies and is a perfect starting point if you are interested in real time RIA&#8217;s.</p>
<p>Finally, Mihai Corlan have shown another <a href="http://corlan.org/2010/07/02/creating-multi-screen-apps-for-android-and-desktop-using-air/" target="_blank">nice example</a> of what can be done using the peer to peer features. This time using several Android mobile phones to manage a desktop MP3 player.</p>
<p><object width="400" height="225"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=13033633&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=1&amp;color=&amp;fullscreen=1&amp;autoplay=0&amp;loop=0" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=13033633&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=1&amp;color=&amp;fullscreen=1&amp;autoplay=0&amp;loop=0" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="225"></embed></object>
<p><a href="http://vimeo.com/13033633">Multi-screen apps for Android Phones and Desktops with Adobe AIR</a> from <a href="http://vimeo.com/user1552538">Mihai Corlan</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.xavigimenez.net/blog/2010/08/peer-2-peer-and-the-flash-platform/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating jagged arrays in Actionscript</title>
		<link>http://www.xavigimenez.net/blog/2010/03/creating-jagged-arrays-in-actionscript/</link>
		<comments>http://www.xavigimenez.net/blog/2010/03/creating-jagged-arrays-in-actionscript/#comments</comments>
		<pubDate>Tue, 02 Mar 2010 20:00:56 +0000</pubDate>
		<dc:creator>Xavi Gimenez</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://www.xavigimenez.net/blog/?p=68</guid>
		<description><![CDATA[First things first, what&#8217;s a jagged array?
A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes. A jagged array is sometimes called an &#8220;array of arrays.&#8221;
Recently I&#8217;ve been playing with FLARTooKit and I found this function into the class ArrayUtils.as.


public function createJaggedArray(len:int, [...]]]></description>
			<content:encoded><![CDATA[<p>First things first, what&#8217;s a jagged array?<br />
A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes. A jagged array is sometimes called an &#8220;array of arrays.&#8221;</p>
<p>Recently I&#8217;ve been playing with <a href="http://www.libspark.org/wiki/saqoosha/FLARToolKit/en" target="_blank">FLARTooKit</a> and I found this function into the class ArrayUtils.as.<br />
<span id="more-68"></span></p>
<pre class="brush: as3;">
public function createJaggedArray(len:int, ...args):Array
{
     var arr:Array = new Array(len);
     while (len--)
     {
          arr[len] = args.length ? createJaggedArray.apply(null, args) : 0;
     }
     return arr;
}
</pre>
<p>The function made me smile due to his simplicity. 5 lines of code, use of the <a href="http://livedocs.adobe.com/flex/201/html/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Book_Parts&#038;file=03_Language_and_Syntax_160_19.html" target="_blank">&#8230; (rest) parameter</a> and a bit of <a href="http://en.wikipedia.org/wiki/Recursion_(computer_science)" target="_blank">recursion </a>and you have it, a useful function to create arrays with as many dimensions as you want. </p>
<p>So, do you want a 3 x 5 x 2 array? Just call the function as follows:</p>
<pre class="brush: as3;">
var jaggedArray:Array = createJaggedArray(3,5,2);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.xavigimenez.net/blog/2010/03/creating-jagged-arrays-in-actionscript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting the picture of data visualization: Visualizing data by Ben Fry</title>
		<link>http://www.xavigimenez.net/blog/2010/02/getting-the-picture-of-data-visualization-visualizing-data-by-ben-fry/</link>
		<comments>http://www.xavigimenez.net/blog/2010/02/getting-the-picture-of-data-visualization-visualizing-data-by-ben-fry/#comments</comments>
		<pubDate>Sun, 28 Feb 2010 20:00:34 +0000</pubDate>
		<dc:creator>Xavi Gimenez</dc:creator>
				<category><![CDATA[Data Visualization]]></category>
		<category><![CDATA[Book]]></category>

		<guid isPermaLink="false">http://www.xavigimenez.net/blog/?p=39</guid>
		<description><![CDATA[That&#8217;s the book I&#8217;m currently reading and if you are interested in the field of the Data Visualization, this book is a good starting point.
The author is Ben Fry, the founder (with Casey Reas) of Processing, a programming language, and development environment. Initially created to serve as a software sketchbook and to teach fundamentals of [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://oreilly.com/catalog/9780596514556" target="_blank"><img title="Visualizing Data" src="http://www.xavigimenez.net/blog/wp-content/uploads/2009/11/lrg-228x300.jpg" alt="Visualizing Data by Ben Fry" width="228" height="300" /></a>That&#8217;s the book I&#8217;m currently reading and if you are interested in the field of the Data Visualization, this book is a good starting point.</p>
<p>The author is <a href="http://benfry.com/" target="_blank">Ben Fry</a>, the founder (with <a href="http://reas.com/" target="_blank">Casey Reas</a>) of <a href="http://processing.org/" target="_blank">Processing</a>, a programming language, and development environment. Initially created to serve as a software sketchbook and to teach fundamentals of computer programming within a visual context, Processing quickly developed into a tool for creating finished professional work as well.<br />
<span id="more-39"></span><br />
Don&#8217;t expect to find awesome graphics or great programming lessons to represent huge sets of data. It&#8217;s not a technical book from the point of view of a programmer.</p>
<p> The book is targeted at a wide range of backgrounds, giving the chance the understand the process of getting data from to presenting it usefully, in a way that can be understood and interacted with. The examples in this book are simple enough to be understood for people with a minimal technical knowledge, and for those who have a solid programming background puts some light on the process of manage a data set in order to be assimilated and interacted, and more important, be able to communicate something with it.</p>
<p>More than the technical details, it&#8217;s pretty interesenting the way as how data sets have to be managed:</p>
<ul>
<li>Each data has particular display needs, and the purpose for which you are using the data set has just as much of an effect on those need as the data itself.</li>
<li>Lots of data is out there, but it&#8217;s not being used to its greatest potential because it&#8217;s not being visualized as well as it could be.</li>
<li>The most important part of understing data is identifying the question that you want to answer.</li>
</ul>
<p>And so on&#8230; Just the ideal way to work when managing data in a creative way, far from just using the existing software to generate typicall pie charts ;)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.xavigimenez.net/blog/2010/02/getting-the-picture-of-data-visualization-visualizing-data-by-ben-fry/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Face detection: How to find faces with openCV</title>
		<link>http://www.xavigimenez.net/blog/2010/02/face-detection-how-to-find-faces-with-opencv/</link>
		<comments>http://www.xavigimenez.net/blog/2010/02/face-detection-how-to-find-faces-with-opencv/#comments</comments>
		<pubDate>Sun, 28 Feb 2010 20:00:19 +0000</pubDate>
		<dc:creator>Xavi Gimenez</dc:creator>
				<category><![CDATA[OpenCV]]></category>

		<guid isPermaLink="false">http://www.xavigimenez.net/blog/?p=137</guid>
		<description><![CDATA[Due to my attempt to create a perceptual interface using OpenCV, this post is the first in a series to explain briefly some of the capabilities of the OpenCV library, an open-source computer-vision library.
OpenCV comes with over 500 functions that cover many areas in vision, and its goal is to provide a simple-to-use computer vision [...]]]></description>
			<content:encoded><![CDATA[<p>Due to my attempt to <a href="http://www.xavigimenez.net/blog/projects/">create a perceptual interface using OpenCV</a>, this post is the first in a series to explain briefly some of the capabilities of the <a href="http://sourceforge.net/projects/opencvlibrary/" target="_blank">OpenCV</a> library, an open-source computer-vision library.</p>
<p>OpenCV comes with over 500 functions that cover many areas in vision, and its goal is to provide a simple-to-use computer vision infraestructure to build fairly sophisticated vision application quickly. The library is written in C and C++ and runs under Linux, Windows and MAC OS X. There is active development on interfaces for Phyton, Ruby, Matlab, and other languages.</p>
<h3>How to find faces</h3>
<p>Finding faces means finding complex objects, so OpenCV uses a statistical model (often called classifier), which is trained to find the object we are looking for. The training consists in a set of images, divided into &#8220;positive&#8221; samples and &#8220;negative&#8221; samples. The positive samples are instances of the object class of interest and the &#8220;negative&#8221;, images that don&#8217;t contain the object of interest.<br />
<span id="more-137"></span><br />
From training the statistical model with the images explained above, a set of features are extracted and distinctive features that can be used to classify the object are selected. At the end you have a detector with a set of features that give that chance to find the desired object. The detector used by openCV is called Haar Cascade Classifier and uses simple rectangular features, called Haar features. The word &#8220;cascade&#8221; in the classifier means that the resultant classifier consists of several simpler classifiers that are applied subsequently to a region of interest until at some stage the candidate is rejected or all stages are passed. These classifiers use Haar features as these:</p>
<p><a href="http://www.xavigimenez.net/blog/wp-content/uploads/2009/11/haarclassifiers.gif"><img src="http://www.xavigimenez.net/blog/wp-content/uploads/2009/11/haarclassifiers.gif" alt="haarclassifiers" title="haarclassifiers" width="550" height="166" class="alignnone size-full wp-image-366" /></a></p>
<p>The feature used in a particular classifiers is specified by its shape, position within the region of interest and the scale and for a basic understanding the idea is that these features encode the existence of oriented constrasts between regions in the image so a set of these features can be used to encode the constrast exhibited by a human face and their spacial relationships.</p>
<p>Basically, the process of face detection slides a &#8220;search window&#8221; through the image, checking whether an image region can be considered as a &#8220;face object&#8221; or not. The detector asumes a fixed scale for the object, but since face in an image can be different from the asumed scale, the &#8220;search window&#8221; goes trough the image several times, searching for the object across a ranges of sizes.</p>
<p><a href="http://www.xavigimenez.net/blog/wp-content/uploads/2009/11/windowsearch.gif"><img src="http://www.xavigimenez.net/blog/wp-content/uploads/2009/11/windowsearch.gif" alt="windowsearch" title="windowsearch" width="550" height="166" class="alignnone size-full wp-image-371" /></a></p>
<p>The last openCV download offers a completes set of classifiers as XML files, which include, among others, classifiers for frontal faces, profile faces, eyes, mouth, nose, upper body, lower body, etc&#8230;</p>
<p>If you have downloaded OpenCV 2.0, you will find a nice example to start playing with, it&#8217;s located at <em>[installation_directory/OpenCV2.0/samples/c/facedetect.exe</em>, and the C source code at <em>[installation_directory/OpenCV2.0/samples/c/facedetect.c</em>.</p>
<p>For the beginners, the code is not enough simple as it should be, so before digging into the code, I recommend start looking at the <a href="http://opencv.willowgarage.com/wiki/FaceDetection" target="_blank">face detection page in the openCV Wiki</a>. Anyway, it's worth to add some comments to the original example, so let's have a look at the function detectAndDraw of facedetect.c, where all the stuff regarding face detections happens:</p>
<pre class="brush: as3;">
void detectAndDraw( Mat&amp; img, CascadeClassifier&amp; cascade, CascadeClassifier&amp; nestedCascade, double scale)
{
    int i = 0;
    double t = 0;
    vector&lt;Rect&gt; faces;
    const static Scalar colors[] =  { CV_RGB(0,0,255),CV_RGB(0,128,255),CV_RGB(0,255,255),CV_RGB(0,255,0), CV_RGB(255,128,0),CV_RGB(255,255,0),CV_RGB(255,0,0),CV_RGB(255,0,255)} ;
    Mat gray, smallImg( cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1 );
</pre>
<p>The classifier works on on grey scale images, so the incoming BGR image <i>img</i> is converted to greyscale and then optionally resized.</p>
<pre class="brush: as3;">
    cvtColor( img, gray, CV_BGR2GRAY );
    resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR );
</pre>
<pre class="brush: as3;">
    equalizeHist( smallImg, smallImg );
</pre>
<p>Now the histogram is equalized, that means to spread out the intensity values (the brightness) of the image histogram. This image will help to get the idea:<br />
<a href="http://www.xavigimenez.net/blog/wp-content/uploads/2009/11/histogram01.jpg"><img src="http://www.xavigimenez.net/blog/wp-content/uploads/2009/11/histogram01.jpg" alt="" title="histogram01" width="550" height="284" class="alignnone size-full wp-image-384" /></a></p>
<p>Now it's time to detect the faces in the input image, so the function detectMultiscale does exactly that. This function returns the detected objects as a list of rectangles. In this case, the vector faces will save the returned data. This function also receives some params to configure the detection:</p>
<ul>
<li>scaleFactor: how much the image size is reduced at each image scale</li>
<li>minNeighbors: how many neighbors should each candidate rectangle have to retain it</li>
<li>the flag CV_HAAR_SCALE_IMAGE, which tells the algorithm to scale the image rather than the detector</li>
<li>minSize(the minmum possible object size, object smaller are ignored).</li>
</ul>
<pre class="brush: as3;">
t = (double)cvGetTickCount();
    cascade.detectMultiScale( smallImg, faces,
        1.1, 2, 0
        //|CV_HAAR_FIND_BIGGEST_OBJECT
        //|CV_HAAR_DO_ROUGH_SEARCH
        |CV_HAAR_SCALE_IMAGE
        ,
        Size(30, 30) );
    t = (double)cvGetTickCount() - t;
    printf( &quot;detection time = %g ms\n&quot;, t/((double)cvGetTickFrequency()*1000.) );
</pre>
<p>From here on, everythings is pretty straight forward, we loop through each face and drawing a circle in each face location.</p>
<pre class="brush: as3;">
    for( vector&lt;Rect&gt;::const_iterator r = faces.begin(); r != faces.end(); r++, i++ )
    {
        Mat smallImgROI;
        vector&lt;Rect&gt; nestedObjects;
        Point center;
        Scalar color = colors[i%8];
        int radius;
        center.x = cvRound((r-&gt;x + r-&gt;width*0.5)*scale);
        center.y = cvRound((r-&gt;y + r-&gt;height*0.5)*scale);
        radius = cvRound((r-&gt;width + r-&gt;height)*0.25*scale);
        circle( img, center, radius, color, 3, 8, 0 );
</pre>
<p>Here we check if the we have loaded a second classifier. In our case, facedetect.c defines a second classifiers, which has been trained to detect eyeglasses, so what we are doing here is to repeat the process explained above, but instead of looking for faces in the input image, we are looking for eyeglasses only in the regions of the images that are considered as faces.</p>
<pre class="brush: as3;">
        if( nestedCascade.empty() )
            continue;
        smallImgROI = smallImg(*r);
        nestedCascade.detectMultiScale( smallImgROI, nestedObjects,
            1.1, 2, 0
            //|CV_HAAR_FIND_BIGGEST_OBJECT
            //|CV_HAAR_DO_ROUGH_SEARCH
            //|CV_HAAR_DO_CANNY_PRUNING
            |CV_HAAR_SCALE_IMAGE
            ,
            Size(30, 30) );
        for( vector&lt;Rect&gt;::const_iterator nr = nestedObjects.begin(); nr != nestedObjects.end(); nr++ )
        {
            center.x = cvRound((r-&gt;x + nr-&gt;x + nr-&gt;width*0.5)*scale);
            center.y = cvRound((r-&gt;y + nr-&gt;y + nr-&gt;height*0.5)*scale);
            radius = cvRound((nr-&gt;width + nr-&gt;height)*0.25*scale);
            circle( img, center, radius, color, 3, 8, 0 );
        }
    }
    cv::imshow( &quot;result&quot;, img );
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.xavigimenez.net/blog/2010/02/face-detection-how-to-find-faces-with-opencv/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

