<?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>Shawn Lankton Online &#187; Matlab</title>
	<atom:link href="http://www.shawnlankton.com/category/matlab/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.shawnlankton.com</link>
	<description>computer vision, science, business, and life</description>
	<lastBuildDate>Thu, 22 Apr 2010 12:59:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Sparse Field Active Contours</title>
		<link>http://www.shawnlankton.com/2009/04/sfm-and-active-contours/</link>
		<comments>http://www.shawnlankton.com/2009/04/sfm-and-active-contours/#comments</comments>
		<pubDate>Tue, 21 Apr 2009 15:04:15 +0000</pubDate>
		<dc:creator>Shawn Lankton</dc:creator>
				<category><![CDATA[Academic]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[Matlab]]></category>
		<category><![CDATA[Vision]]></category>
		<category><![CDATA[active contours]]></category>
		<category><![CDATA[computer vision]]></category>
		<category><![CDATA[level sets]]></category>
		<category><![CDATA[paper]]></category>
		<category><![CDATA[segmentation]]></category>

		<guid isPermaLink="false">http://www.shawnlankton.com/?p=390</guid>
		<description><![CDATA[Active contour methods for image segmentation allow a contour to deform iteratively to partition an image into regions. Active contours are often implemented with level sets. The primary drawback, however, is that they are slow to compute.  This post presents a technical report describing, in detail, the sparse field method (SFM) proposed by Ross [...]]]></description>
			<content:encoded><![CDATA[<p>Active contour methods for image segmentation allow a contour to deform iteratively to partition an image into regions. Active contours are often implemented with level sets. The primary drawback, however, is that they are slow to compute.  This post presents a technical report describing, in detail, the sparse field method (SFM) proposed by Ross Whitaker [<a href="http://scholar.google.com/scholar?cluster=13533791363488741942&#038;hl=en&#038;as_sdt=20000000000">pdf</a>], which allows one to implement level set active contours very efficiently.  The algorithm is described in detail, specific notes are given about implementation, and source code is provided.</p>
<h2>Fast Level Sets Demo</h2>
<p>The links below point to the technical report and a demo written in C++/MEX that can be run directly in MATLAB.  The demo implements the Chan-Vese segmentation energy, but many energies can be minimized using the provided framework.</p>
<blockquote><p>
<a href="http://www.shawnlankton.com/wp-content/uploads/files/sfm_techreport.pdf">Sparse Field Method &#8211; Technical Report [pdf]</a><br />
<a href="http://www.shawnlankton.com/wp-content/uploads/files/sfm_chanvese_demo.zip">Sparse Field Method &#8211; Matlab Demo [zip]</a>
</p></blockquote>
<p>To run the MATLAB demo, simply unzip the file and run:<br />
<code>>>sfm_chanvese_demo</code><br />
at the command line.  On the first run, this will compile the MEX code on your machine and then run the demo.  If the MEX compile fails, please check your MEX setup.  The demo is for a 2D image, but the codes work for 3D images as well.</p>
<p>My hope is that other researchers wishing to quickly implement Whitaker&#8217;s method can use this information to easily understand the intricacies of the algorithm which, in my opinion, were not presented clearly in Whitaker&#8217;s original paper.  Personally, these codes have SUBSTANTIALLY sped up my segmentations, and are allowing me to make much faster progress towards completing my PhD!</p>
<p>Thanks to Ernst Schwartz and Andy for helping to find small bugs in the codes and documentation. (they&#8217;re fixed now!)</p>
<p>For more information regarding active contour, segmentation, and computer vision, check here: <a href="http://www.shawnlankton.com/category/vision/">Computer Vision Posts</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.shawnlankton.com/2009/04/sfm-and-active-contours/feed/</wfw:commentRss>
		<slash:comments>54</slash:comments>
		</item>
		<item>
		<title>Median Filter and Morphological Dilation in Python</title>
		<link>http://www.shawnlankton.com/2008/05/median-filter-and-morphological-dilation-in-python/</link>
		<comments>http://www.shawnlankton.com/2008/05/median-filter-and-morphological-dilation-in-python/#comments</comments>
		<pubDate>Thu, 01 May 2008 14:48:16 +0000</pubDate>
		<dc:creator>Shawn Lankton</dc:creator>
				<category><![CDATA[Matlab]]></category>
		<category><![CDATA[Vision]]></category>
		<category><![CDATA[image processing]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.shawnlankton.com/2008/05/median-filter-and-morphological-dilation-in-python/</guid>
		<description><![CDATA[Python is a very nice programming language.  Fast. Simple. Free.  I recently spent some time learning it for a class on computer vision.  I was using the PIL and numpy packages to make Python feel more like my old friend Matlab.  
The two functions that I couldn&#8217;t find, and missed the [...]]]></description>
			<content:encoded><![CDATA[<p>Python is a very nice programming language.  Fast. Simple. Free.  I recently spent some time learning it for a class on computer vision.  I was using the PIL and numpy packages to make Python feel more like my old friend Matlab.  </p>
<p>The two functions that I couldn&#8217;t find, and missed the most (especially when writing hack-y code for class projects) were median filtering and morphological dilation.  So, in hopes of sparing other the pain of writing them&#8230; here they are!  The function medfilt_dilate.py has both functions.</p>
<blockquote><p>
<a href="http://www.shawnlankton.com/wp-content/uploads/files/medfilt_dilate.py">medfilt_dilate.py</a>
</p></blockquote>
<p>The medfilt() function uses the PIL filtering code.  The dilate() function was written from scratch with NumPy.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shawnlankton.com/2008/05/median-filter-and-morphological-dilation-in-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fast 3D Stereo Vision</title>
		<link>http://www.shawnlankton.com/2008/04/stereo-vision-update-with-new-code/</link>
		<comments>http://www.shawnlankton.com/2008/04/stereo-vision-update-with-new-code/#comments</comments>
		<pubDate>Mon, 14 Apr 2008 22:00:11 +0000</pubDate>
		<dc:creator>Shawn Lankton</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[Matlab]]></category>
		<category><![CDATA[Vision]]></category>
		<category><![CDATA[3D]]></category>
		<category><![CDATA[computer vision]]></category>
		<category><![CDATA[stereo vision]]></category>

		<guid isPermaLink="false">http://www.shawnlankton.com/2008/04/stereo-vision-update-with-new-code/</guid>
		<description><![CDATA[Recently, I started looking at faster ways to perform dense stereo matching for some work with 3D video.  After some experimentation, I found out that by using a selective mode filter paired with naive correspondence matching, I was able to get satisfactory results very quickly.  Check out the slide show below for some [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I started looking at faster ways to perform dense stereo matching for some work with 3D video.  After some experimentation, I found out that by using a <a href="http://www.shawnlankton.com/2008/04/selective-mode-filter-in-matlab/">selective mode filter</a> paired with naive correspondence matching, I was able to get satisfactory results very quickly.  Check out the slide show below for some results!</p>
<p><center><br />
<embed type="application/x-shockwave-flash" src="http://picasaweb.google.com/s/c/bin/slideshow.swf" width="400" height="267" flashvars="host=picasaweb.google.com&#038;RGB=0x000000&#038;feed=http%3A%2F%2Fpicasaweb.google.com%2Fdata%2Ffeed%2Fapi%2Fuser%2Fshawn.lankton%2Falbumid%2F5188210756103456801%3Fkind%3Dphoto%26alt%3Drss%26authkey%3DxyPWYtqp8GM" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed><br />
[red indicates close, blue indicates far away]<br />
</center></p>
<p>&nbsp;</p>
<p>Here is a download-able Matlab demo, which should work on any pre-aligned stereo image pairs:</p>
<blockquote><p>
<a href="http://www.shawnlankton.com/wp-content/uploads/files/stereo_modefilt.zip">stereo_modefilt.zip</a>
</p></blockquote>
<p>The entire code is written in Matlab/C++/MEX.  The stereo matching is all in Matlab, and the selective mode filter is coded in C++ and callable from Matlab (meaning it must be compiled before it can run).  Currently, the correspondence is the major bottleneck, so anyone who can improve this, please let me know!  Enjoy.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shawnlankton.com/2008/04/stereo-vision-update-with-new-code/feed/</wfw:commentRss>
		<slash:comments>35</slash:comments>
		</item>
		<item>
		<title>Selective Mode filter in Matlab</title>
		<link>http://www.shawnlankton.com/2008/04/selective-mode-filter-in-matlab/</link>
		<comments>http://www.shawnlankton.com/2008/04/selective-mode-filter-in-matlab/#comments</comments>
		<pubDate>Sat, 12 Apr 2008 04:15:01 +0000</pubDate>
		<dc:creator>Shawn Lankton</dc:creator>
				<category><![CDATA[Matlab]]></category>
		<category><![CDATA[Vision]]></category>
		<category><![CDATA[image processing]]></category>

		<guid isPermaLink="false">http://www.shawnlankton.com/2008/04/selective-mode-filter-in-matlab/</guid>
		<description><![CDATA[The median filter is a well-known image processing filter.  It provides a very nice way to smooth an image while preserving edges.  The median filter replaces each pixel in the image with the median value of its neighboring pixels.  A similar non-linear filter with slightly different properties is the mode filter which [...]]]></description>
			<content:encoded><![CDATA[<p>The median filter is a well-known image processing filter.  It provides a very nice way to smooth an image while preserving edges.  The median filter replaces each pixel in the image with the <em>median</em> value of its neighboring pixels.  A similar non-linear filter with slightly different properties is the mode filter which replaces each pixel with the <em>mode</em> of its neighboring pixels.  I additionally make a slight modification so that &#8220;bad&#8221; pixels are ignored entirely in the computation of the mode.</p>
<p>This idea arose when I was trying to de-noise some images as well as do some in-painting of &#8220;bad&#8221; pixels (that have no value).  Consider the image below. The darkest-blue areas are bad pixels.  We have no information for those pixels.  The other pixels are colored to show how far that pixel is from the camera.  (<a href="http://www.shawnlankton.com/2007/12/3d-vision-with-stereo-disparity/">see the post on Stereo Vision</a>)  However, some of the good pixels still have the wrong value.  These are the noise pixels.</p>
<p><center><br />
<a href='http://www.shawnlankton.com/wp-content/uploads/2008/04/p_orig.png' title='original data'><img src='http://www.shawnlankton.com/wp-content/uploads/2008/04/p_orig.thumbnail.png' alt='original data' /></a><br />
[Initial Image]<br />
</center></p>
<p>In the rest this post I talk about how we use a selective mode filter to convert the above image into the one below.  (There&#8217;s also download-able Matlab/C++ code)</p>
<p><center><br />
<a href='http://www.shawnlankton.com/wp-content/uploads/2008/04/selective_modefilt1.png' title='selective modefilt'><img src='http://www.shawnlankton.com/wp-content/uploads/2008/04/selective_modefilt1.thumbnail.png' alt='selective modefilt' /></a><br />
[Final Result of Selective Mode Filter]<br />
</center></p>
<p><span id="more-323"></span></p>
<h2>First Try</h2>
<p>First, we compare a standard median filter along-side a standard mode filter. We see that the two results are very similar.  I deem the mode filter to be slightly better, but this is pretty subjective.  As you can see, both methods do a good job of eliminating noise although neither filter removes the bad pixels.</p>
<p><center><br />
<a href='http://www.shawnlankton.com/wp-content/uploads/2008/04/medfilt.png' title='median filter'><img src='http://www.shawnlankton.com/wp-content/uploads/2008/04/medfilt.thumbnail.png' alt='median filter' height=120 width=160/></a>&nbsp;&nbsp;<a href='http://www.shawnlankton.com/wp-content/uploads/2008/04/modefilt.png' title='mode filter'><img src='http://www.shawnlankton.com/wp-content/uploads/2008/04/modefilt.thumbnail.png' alt='mode filter' height=120 width=160/></a><br />
[Standard Median Filter, Standard Mode Filter]<br />
</center></p>
<h2>Being Selective</h2>
<p>The solution is to ignore bad pixels so that they can not contribute to the computation of the median or mode (depending on which filter you use).  In my example, bad pixels are value &#8216;0&#8242; while all the other pixels are positive and non-zero.  Therefore, I re-wrote the mode filter to ignore pixels under a threshold when computing the local mode around each pixel.  The result is very nice.  Some of the fine-scale features are lost, but overall, the number of noise pixels <em>and</em> bad pixels are greatly reduced.</p>
<h2>Conclusion &#038; Download</h2>
<p>This is a simple idea, but it had very nice results.  A good next step would be to create a selective median filter.  I implemented this in mex so the operation is very quick.  In the demo below I include binaries for most platforms, but if doesn&#8217;t work on your computer you may need to re-compile.</p>
<blockquote><p>
<a href="http://www.shawnlankton.com/wp-content/uploads/files/modefilt.zip">modefilt.zip</a>
</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.shawnlankton.com/2008/04/selective-mode-filter-in-matlab/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Active Contour Matlab Code Demo</title>
		<link>http://www.shawnlankton.com/2008/04/active-contour-matlab-code-demo/</link>
		<comments>http://www.shawnlankton.com/2008/04/active-contour-matlab-code-demo/#comments</comments>
		<pubDate>Mon, 07 Apr 2008 22:00:38 +0000</pubDate>
		<dc:creator>Shawn Lankton</dc:creator>
				<category><![CDATA[Academic]]></category>
		<category><![CDATA[Matlab]]></category>
		<category><![CDATA[Vision]]></category>
		<category><![CDATA[active contours]]></category>
		<category><![CDATA[computer vision]]></category>
		<category><![CDATA[paper]]></category>
		<category><![CDATA[segmentation]]></category>

		<guid isPermaLink="false">http://www.shawnlankton.com/2008/04/active-contour-matlab-code-demo/</guid>
		<description><![CDATA[
UPDATE: My new post: Sparse Field Active Contours implements quicker, more accurate active contours.

Today, I added demo code for the Hybrid Segmentation project. This segmentation algorithm (in the publications section) can be used to find the boundary of objects in images.  This approach uses localized statistics and sometimes gets better results than classic methods. [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>
<strong>UPDATE:</strong> <br/>My new post: <a href="http://www.shawnlankton.com/2009/04/sfm-and-active-contours/">Sparse Field Active Contours</a><br/> implements quicker, more accurate active contours.
</p></blockquote>
<p>Today, I added demo code for the <a href="http://www.shawnlankton.com/2007/02/cool-hybrid-segmentation/">Hybrid Segmentation</a> project. This segmentation algorithm (in the <a href="http://www.shawnlankton.com/publications/">publications section</a>) can be used to find the boundary of objects in images.  This approach uses localized statistics and sometimes gets better results than classic methods.  For an example, see the video below:  The contour begins as a rectangle, but deforms over time so that it finally forms the outline of the monkey.</p>
<p><center><object width="252" height="200"><param name="movie" value="http://www.youtube.com/v/9Kx4Wsylryw&#038;rel=0&#038;color1=0x234900&#038;color2=0x4e9e00&#038;hl=en"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/9Kx4Wsylryw&#038;rel=0&#038;color1=0x234900&#038;color2=0x4e9e00&#038;hl=en" type="application/x-shockwave-flash" wmode="transparent" width="252" height="200"></embed></object></center></p>
<p>This can be used to segment many different classes of image.  To try it out, download the demo below and run &gt;&gt;localized_seg_demo</p>
<blockquote><p><a href="http://www.shawnlankton.com/wp-content/uploads/files/hybridseg/localized_seg.zip">localized_seg.zip</a></p></blockquote>
<p><center><object width="252" height="200"><param name="movie" value="http://www.youtube.com/v/H3P5N7ZfvEo&#038;rel=0&#038;color1=0x234900&#038;color2=0x4e9e00&#038;hl=en"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/H3P5N7ZfvEo&#038;rel=0&#038;color1=0x234900&#038;color2=0x4e9e00&#038;hl=en" type="application/x-shockwave-flash" wmode="transparent" width="252" height="200"></embed></object></center></p>
<p>This code is based on a standard level set segmentation; it just optimizes a different energy.  I&#8217;ve also made a demo which implements the well-known Chan-Vese segmentation algorithm.  This technique is similar to the one above, but it looks at global statistics.  This makes it more robust to initialization, but it also means that more constraints are placed on the image.  Download it and see what you think!  Again, unzip the file and run &gt;&gt;region_seg_demo</p>
<blockquote><p><a href="http://www.shawnlankton.com/wp-content/uploads/files/sfm_chanvese_demo.zip">sfm_chanvese_demo.zip</a> (New! <a href="http://www.shawnlankton.com/2009/04/sfm-and-active-contours/">Described Here</a>)<br/><br />
<del datetime="2009-07-02T18:20:17+00:00"><a href="http://www.shawnlankton.com/wp-content/uploads/files/regionbased_seg.zip">regionbased_seg.zip</a></del> (old and slow)</p></blockquote>
<p>For another Matlab implementation of Active Contours check out: <a href="http://www.jgmalcolm.com">James Malcolm&#8217;s Webpage</a>.  He has some codes for very fast approximate implementations as well as a full numerical implementation.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shawnlankton.com/2008/04/active-contour-matlab-code-demo/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>A Short MEX Tutorial and Demo</title>
		<link>http://www.shawnlankton.com/2008/03/getting-started-with-mex-a-short-tutorial/</link>
		<comments>http://www.shawnlankton.com/2008/03/getting-started-with-mex-a-short-tutorial/#comments</comments>
		<pubDate>Sat, 08 Mar 2008 21:21:40 +0000</pubDate>
		<dc:creator>Shawn Lankton</dc:creator>
				<category><![CDATA[Academic]]></category>
		<category><![CDATA[Matlab]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.shawnlankton.com/2008/03/getting-started-with-mex-a-short-tutorial/</guid>
		<description><![CDATA[Matlab is a great programming language/environment because of its ease of use, great visualization, and rapid prototyping abilities.  Raw speed is not one of its strong suits.  MEX (Matlab Executables) are the answer.  These functions allow you to program in C or C++ (ultra fast languages), but be able to call and [...]]]></description>
			<content:encoded><![CDATA[<p>Matlab is a great programming language/environment because of its ease of use, great visualization, and rapid prototyping abilities.  Raw speed is not one of its strong suits.  MEX (Matlab Executables) are the answer.  These functions allow you to program in C or C++ (ultra fast languages), but be able to call and use them from Matlab programs.  This post is a short intro to mex files which should get you up and running.</p>
<h2>What This Post Teaches</h2>
<p>In this post, I show how to create a mex file, how to set up inputs and outputs, how to get access to Matlab objects, and how to manipulate them. I also give a skeleton mex program that might be helpful.  There is a lot more to learn, and I&#8217;d refer you to the <a href="http://www.google.com/url?sa=t&#038;ct=res&#038;cd=1&#038;url=http%3A%2F%2Fwww.mathworks.com%2Faccess%2Fhelpdesk%2Fhelp%2Fpdf_doc%2Fmatlab%2Fapiref.pdf&#038;ei=UEjQR4zPBpuezQTyje3lBA&#038;usg=AFQjCNHOvWlfBAEOFozXXGWCMxaZgO36gQ&#038;sig2=3dPkZwiCYs7v8rSNzVnhDA">mex manual</a> regardless.</p>
<p><span id="more-312"></span></p>
<h2>What You Should Already Know</h2>
<p>I&#8217;m going to assume you already know how to program in C, and already know how to program in Matlab.  This post does not teach either language, but will show you how to use them together to get the best of both worlds.</p>
<h2>Hello world!</h2>
<p>Lets get started.  First, type:<br />
<code>&gt;&gt;mex -setup</code><br />
Then go through the menus to select a compiler that you have installed on your system.  I use gcc on linux, visual studios on windows, and mexopts on mac. Now, create a file called, &#8220;helloworld.cpp&#8221; and we&#8217;ll start coding.  In this first example, pay attention to the signature of the mexFunction. This signature is always the same.</p>
<p><code><br />
#include &lt;math.h&gt;<br />
#include &lt;matrix.h&gt;<br />
#include &lt;mex.h&gt;</p>
<p>void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])<br />
{<br />
&nbsp;&nbsp;mexPrintf("Hello World!\n");<br />
}<br />
</code></p>
<p>Save the file, and in the Matlab prompt type,<br />
<code>&gt;&gt;mex helloworld.cpp</code><br />
If all goes well, you should get the following when you run helloworld:<br />
<code>&gt;&gt;helloworld<br />
&gt;&gt;Hello World!</code></p>
<h2>Setting Up Inputs and Outputs</h2>
<p>Now that we have a working program, the next step is to get inputs and outputs going so we can do something useful.  The mxArray pointers plhs and prhs represent a <strong>p</strong>ointer to the <strong>l</strong>eft <strong>h</strong>and <strong>s</strong>side and a <strong>p</strong>ointer to the <strong>r</strong>ight <strong>h</strong>and <strong>s</strong>side respectively.  The left hand side are the outputs and the right hand side are the inputs.</p>
<p>put something like this in the top of mexFunction:</p>
<p><code><br />
&nbsp;&nbsp;//declare variables<br />
&nbsp;&nbsp;mxArray *a_in_m, *b_in_m, *c_out_m, *d_out_m;<br />
&nbsp;&nbsp;const mwSize *dims;<br />
&nbsp;&nbsp;double *a, *b, *c, *d;<br />
&nbsp;&nbsp;int dimx, dimy, numdims;<br />
&nbsp;&nbsp;<br />
&nbsp;&nbsp;//associate inputs<br />
&nbsp;&nbsp;a_in_m = mxDuplicateArray(prhs[0]);<br />
&nbsp;&nbsp;b_in_m = mxDuplicateArray(prhs[1]);<br />
&nbsp;&nbsp;<br />
&nbsp;&nbsp;//figure out dimensions<br />
&nbsp;&nbsp;dims = mxGetDimensions(prhs[0]);<br />
&nbsp;&nbsp;numdims = mxGetNumberOfDimensions(prhs[0]);<br />
&nbsp;&nbsp;dimy = (int)dims[0]; dimx = (int)dims[1];<br />
&nbsp;&nbsp;<br />
&nbsp;&nbsp;//associate outputs<br />
&nbsp;&nbsp;c_out_m = plhs[0] = mxCreateDoubleMatrix(dimy,dimx,mxREAL);<br />
&nbsp;&nbsp;d_out_m = plhs[1] = mxCreateDoubleMatrix(dimy,dimx,mxREAL);<br />
</code></p>
<h2>Accessing and Manipulating Matlab Objects</h2>
<p>To access the variables, you need to associate a pointer to the data in the mxArray.  Once you do this, accessing the variables is very simple.</p>
<p><code><br />
&nbsp;&nbsp;a = mxGetPr(a_in_m);<br />
&nbsp;&nbsp;b = mxGetPr(b_in_m);<br />
&nbsp;&nbsp;c = mxGetPr(c_out_m);<br />
&nbsp;&nbsp;d = mxGetPr(d_out_m);<br />
</code></p>
<p>Now it is possible to access the arrays with standard C or C++ [] notation. There are three important things to remember though:</p>
<ul>
<li>You use 0-based indexing as always in C</li>
<li>You still use column-first indexing like in Matlab, though</li>
<li>To access the arrays, you use linear indexing (you can&#8217;t use [x][y], you have to use [y+x*dimy]</li>
</ul>
<p>With those three things in mind, go crazy.  You can use standard C libraries (as long as you include them).  You can use for loops as much as your heart desires, and your code will be much, much faster than its Matlab equivalent.</p>
<h2>Downloads</h2>
<p>Download this starter file (demo.cpp).  This has all of the features discussed in this post, and should be a good primer for any mex function.  Give it a try.  Tell me what you think, and let me know if any improvements are needed.</p>
<blockquote><p>
<a href="http://www.shawnlankton.com/wp-content/uploads/files/mex_tutorial/demo.cpp">demo.cpp</a></li>
</blockquote>
<p>To run the demo, download the .cpp file, then at the prompt run:</p>
<p><code>&nbsp;&nbsp;&gt;&gt;mex demo.cpp<br />
&nbsp;&nbsp;&gt;&gt;a = round(rand(2)*10)<br />
&nbsp;&nbsp;&gt;&gt;b = round(rand(2)*10)<br />
&nbsp;&nbsp;&gt;&gt;[c,d] = demo(a,b)</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.shawnlankton.com/2008/03/getting-started-with-mex-a-short-tutorial/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>GrowCut Segmentation In Matlab</title>
		<link>http://www.shawnlankton.com/2008/03/growcut-segmentation-in-matlab/</link>
		<comments>http://www.shawnlankton.com/2008/03/growcut-segmentation-in-matlab/#comments</comments>
		<pubDate>Thu, 06 Mar 2008 15:01:57 +0000</pubDate>
		<dc:creator>Shawn Lankton</dc:creator>
				<category><![CDATA[Matlab]]></category>
		<category><![CDATA[Vision]]></category>
		<category><![CDATA[computer vision]]></category>
		<category><![CDATA[segmentation]]></category>

		<guid isPermaLink="false">http://www.shawnlankton.com/2008/03/growcut-segmentation-in-matlab/</guid>
		<description><![CDATA[I came across a cute segmentation idea called &#8220;Grow Cut&#8221; [pdf]. This paper by Vladimir Vezhnevets and Vadim Konouchine presents a very simple idea that has very nice results.  I always feel that the simplest ideas are the best!  Below I give a brief description of the algorithm and link to the Matlab/C/mex [...]]]></description>
			<content:encoded><![CDATA[<p>I came across a cute segmentation idea called &#8220;Grow Cut&#8221; [<a href="http://graphics.cs.msu.su/en/publications/text/gc2005vk.pdf">pdf</a>]. This paper by Vladimir Vezhnevets and Vadim Konouchine presents a very simple idea that has very nice results.  I always feel that the simplest ideas are the best!  Below I give a brief description of the algorithm and link to the Matlab/C/mex code.</p>
<h2>GrowCut Region Growing Algorithm</h2>
<p>This algorithm is presented as an alternative to <a href="http://en.wikipedia.org/wiki/Graph_cuts_in_computer_vision">graph-cuts</a>.  The operation is very simple, and can be thought of with a biological metaphor: Imagine each image pixel is a &#8220;cell&#8221; of a certain type.  These cells can be foreground, background, undefined, or others.  As the algorithm proceeds, these cells compete to dominate the image domain.  The ability of the cells to spread is related to the image pixel intensity.</p>
<p>The authors give some pseudocode that very concisely describes the algorithm.</p>
<p><code><br />
//for every cell p<br />
for all p in image<br />
&nbsp;&nbsp;//copy previous state<br />
&nbsp;&nbsp;labels_new = labels;<br />
&nbsp;&nbsp;strength_new = strength;<br />
&nbsp;&nbsp;// all neighbors q of p attack<br />
&nbsp;&nbsp;for all q neighbors<br />
&nbsp;&nbsp;&nbsp;&nbsp;if(attack_force*strength(q)>strength_new(p))<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;labels_new(p) = labels(q)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strength(p) = strength_new(q)<br />
&nbsp;&nbsp;&nbsp;&nbsp;end if<br />
&nbsp;&nbsp;end for<br />
end for<br />
</code></p>
<h2>Segmentation Results</h2>
<p>Once implemented, this is a nice way to get segmentations.  It is quite fast, and the initialization is very intuitive.  Consider this picture of a lotus flower:</p>
<p><center><a href='http://www.shawnlankton.com/wp-content/uploads/2008/03/image.png' title='growcut image'><img src='http://www.shawnlankton.com/wp-content/uploads/2008/03/image.thumbnail.png' alt='growcut image' /></a></center></p>
<p>I made an initialization by clicking 20 points in the flower and 30 points outside.  I then made a &#8220;label map&#8221; where unlabeled pixels are 0 (gray), foreground pixels are 1 (white) and background pixels are -1 (black).</p>
<p><center><a href='http://www.shawnlankton.com/wp-content/uploads/2008/03/seeds.png' title='growcut seeds'><img src='http://www.shawnlankton.com/wp-content/uploads/2008/03/seeds.thumbnail.png' alt='growcut seeds' /></a></center></p>
<p>Based on this simple initialization, we obtain a very decent segmentation:</p>
<p><center><a href='http://www.shawnlankton.com/wp-content/uploads/2008/03/output.png' title='growcut output'><img src='http://www.shawnlankton.com/wp-content/uploads/2008/03/output.thumbnail.png' alt='growcut output' /></a></center></p>
<p>As you can see, it isn&#8217;t perfect, but it is quite good.  Its possible to interactively refine the seed points to improve the segmentation, but I didn&#8217;t do that here.</p>
<h2>Matlab Code Downloads</h2>
<p>I implemented this code in Matlab (using mex files due to the extensive use of for loops).  You can download this below with compiled binaries for mac, linux, and windows.  Unzip the file and run >>growcut_test for a demo.</p>
<p><strong>UPDATE:</strong> I&#8217;ve fixed some bugs thanks to reader, Lin.  The code works much better now!</p>
<blockquote><p>
<a href="http://www.shawnlankton.com/wp-content/uploads/files/growcut/growcut.zip">Source &#038; Compiled Binaries (96k) [zip]</a><br />
<a href="http://graphics.cs.msu.su/en/publications/text/gc2005vk.pdf">&#8220;GrowCut&#8221; Paper [pdf]</a>
</p></blockquote>
<p>Please let me know if you find this useful, and if you make improvements!   Also, check out these related segmentation posts: </p>
<h2>Related Segmentation Posts</h2>
<ul>
<li><a href="http://www.shawnlankton.com/2007/11/mean-shift-segmentation-in-matlab/">Mean-Shift Image Segmentation</a></li>
<li><a href="http://www.shawnlankton.com/2008/04/active-contour-matlab-code-demo/">Localized Active Contours</a></li>
<li><a href="http://www.shawnlankton.com/2009/04/sfm-and-active-contours/">Fast Level Sets for Segmentation</a></li>
]]></content:encoded>
			<wfw:commentRss>http://www.shawnlankton.com/2008/03/growcut-segmentation-in-matlab/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Quick and simple derivatives in Matlab</title>
		<link>http://www.shawnlankton.com/2008/02/quick-and-simple-derivatives-in-matlab/</link>
		<comments>http://www.shawnlankton.com/2008/02/quick-and-simple-derivatives-in-matlab/#comments</comments>
		<pubDate>Fri, 08 Feb 2008 06:10:30 +0000</pubDate>
		<dc:creator>malcolm</dc:creator>
				<category><![CDATA[Matlab]]></category>

		<guid isPermaLink="false">http://www.shawnlankton.com/2008/02/quick-and-simple-derivatives-in-matlab/</guid>
		<description><![CDATA[Ever try to compute the directional derivatives on a matrix?  Google turn up menacing formulas for Taylor expansions, grid spacing, and boundary conditions?
A quick and simple way of computing derivatives is to perform arithmetic on shifted versions of the matrix, and vectorized indexing help Matlab speed things up.  For example, use the following [...]]]></description>
			<content:encoded><![CDATA[<p>Ever try to compute the directional derivatives on a matrix?  Google turn up menacing formulas for Taylor expansions, grid spacing, and boundary conditions?</p>
<p>A quick and simple way of computing derivatives is to perform arithmetic on shifted versions of the matrix, and vectorized indexing help Matlab speed things up.  For example, use the following for the (central) x-derivative:</p>
<p><code>dx = (M(:,[2:end end]) - M(:,[1 1:end-1]))/2</code></p>
<p>You can even define inline functions to perform the shift and derivative operations.  Below are definitions for the shift operations and first and second order derivatives.</p>
<p><code>% shift operations<br />
shiftD = @(M) M([1 1:end-1],:);<br />
shiftL = @(M) M(:,[2:end end]);<br />
shiftR = @(M) M(:,[1 1:end-1]);<br />
shiftU = @(M) M([2:end end],:);</code></p>
<p><code>% derivatives<br />
Dx  = @(M) (shiftL(M) - shiftR(M))/2;<br />
Dy  = @(M) (shiftU(M) - shiftD(M))/2;<br />
Dxx = @(M) (shiftL(M) - 2*M + shiftR(M));<br />
Dyy = @(M) (shiftU(M) - 2*M + shiftD(M));<br />
Dxy = @(M) (shiftU(M) - shiftD(M) + shiftL(M) - shiftR(M))/4;</code></p>
<p>And use them like this:<br />
<code>>> Ax = Dx(A)<br />
Ax =<br />
   -7.0000   -6.5000    5.5000    5.0000<br />
    3.0000    2.5000   -1.5000   -1.0000<br />
   -1.0000   -1.5000    2.5000    3.0000<br />
    5.0000    5.5000   -6.5000   -7.0000</code></p>
<p><code>>> Ayy = Dyy(A)<br />
Ayy =<br />
   -11     9     7    -5<br />
    15   -13   -11     9<br />
    -9    11    13   -15<br />
     5    -7    -9    11<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.shawnlankton.com/2008/02/quick-and-simple-derivatives-in-matlab/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>3D Vision with Stereo Disparity</title>
		<link>http://www.shawnlankton.com/2007/12/3d-vision-with-stereo-disparity/</link>
		<comments>http://www.shawnlankton.com/2007/12/3d-vision-with-stereo-disparity/#comments</comments>
		<pubDate>Wed, 19 Dec 2007 22:00:16 +0000</pubDate>
		<dc:creator>Shawn Lankton</dc:creator>
				<category><![CDATA[Matlab]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Vision]]></category>
		<category><![CDATA[3D]]></category>
		<category><![CDATA[computer vision]]></category>
		<category><![CDATA[stereo vision]]></category>

		<guid isPermaLink="false">http://www.shawnlankton.com/2007/12/3d-vision-with-stereo-disparity/</guid>
		<description><![CDATA[2D is nice, but these days I&#8217;m getting interested in doing computer vision in 3D.  One way to get 3D data is to use two cameras and determine distance by looking at the differences in the two pictures (just like eyes!).  In this project I show some initial results and codes for computing [...]]]></description>
			<content:encoded><![CDATA[<p><a href='http://www.shawnlankton.com/2007/12/3d-vision-with-stereo-disparity/' title='Stereo Thumbnail'><img src='http://www.shawnlankton.com/wp-content/uploads/2007/12/stereo_thumb.png' alt='Stereo Thumbnail' align='right'/></a>2D is nice, but these days I&#8217;m getting interested in doing computer vision in 3D.  One way to get 3D data is to use two cameras and determine distance by looking at the differences in the two pictures (just like eyes!).  In this project I show some initial results and codes for computing disparity from stereo images. <span id="more-281"></span></p>
<h2>Introduction</h2>
<blockquote><p><strong>UPDATE:</strong> <a href="http://www.shawnlankton.com/2008/04/stereo-vision-update-with-new-code/">Check this recent post</a> for a newer, faster version of this code.  The new version no longer relies on mean-shift.
</p></blockquote>
<p>People can see depth because they look at the same scene at two slightly different angles (one from each eye).  Our brains then figure out how close things are by determining how far apart they are in the two images from our eyes.  The idea here is to do the same thing with a computer.  Check this for some information on the <a href="http://homepages.inf.ed.ac.uk/rbf/CVonline/LOCAL_COPIES/OWENS/LECT11/node4.html">geometry and mathematics of stereo vision</a>.  First, here are the images I&#8217;ll use to show results.</p>
<p><center><a href='http://www.shawnlankton.com/wp-content/uploads/2007/12/stacked.png' title='Stacked Tsu Images'><img src='http://www.shawnlankton.com/wp-content/uploads/2007/12/stacked.thumbnail.png' alt='Stacked Tsu Images' /></a></center></p>
<p>These two images are slightly different.  The top one is from the left and the bottom is from the right.  It&#8217;s a bit hard to see the disparity like this, so here are the same two images placed &#8220;on top&#8221; of one another.</p>
<p><center><a href='http://www.shawnlankton.com/wp-content/uploads/2007/12/together.png' title='Tsu images smashed together'><img src='http://www.shawnlankton.com/wp-content/uploads/2007/12/together.thumbnail.png' alt='Tsu images smashed together' /></a></center></p>
<p>You can see that the close-up objects like the lamp are very misaligned in the two images, while the farther-away things like the poster and the camera are lined up better.  The greater the misalignment, the closer the object.</p>
<p>This pair of images is one of many standard stereo pairs that can be found at the <a href="http://vision.middlebury.edu/stereo/">Middlebury stereo vision site</a>.  These guys keep a compendium of standard datasets as well as a scoreboard of who&#8217;s algorithms work the best.  The algorithm I talk about here is a knock-off of the one that was on top in December 2007: &#8220;<a href="http://www.google.com/url?sa=t&#038;ct=res&#038;cd=1&#038;url=http%3A%2F%2Fwww.vrvis.at%2Fpublications%2Fpdfs%2FVRVis_2006_05_22_16_20_00.pdf&#038;ei=JIlqR66AIqPGywSXytFC&#038;usg=AFQjCNHmGjB8ilAn_FjEkR3yM6HMeWsqag&#038;sig2=JgNCKIjiwxjGXKWSPsd5MQ">Segment-Based Stereo Matching Using Belief Propogation and a Self-Adapting Dissimilarity Measure[PDF]</a>&#8221; by Klaus, Sormann, and Karner.  (Mind that the algorithm here is *inspired* by the algorithm of Klaus <em>et al</em>.  Theirs is much more complete)</p>
<h2>Getting Pixel Disparity</h2>
<p>The first step here is to get an estimate of the disparity at each pixel in the image.  A reference image is chosen (in this case, the right image), and the other image slides across it.  As the two images &#8217;slide&#8217; over one another we subtract their intensity values.  Additionally, we subtract gradient information from the images (spatial derivatives).  Combining these gives better accuracy, especially on surfaces with texture.  In the video below, we can see a visualization of this process.  You&#8217;ll notice how far-away objects go dark (meaning they line up in the two images) at different times than close-up objects.  We record the offset when the difference is the smallest as well as the value of the difference.</p>
<p><center><object width="400" height="316"><param name="movie" value="http://www.youtube.com/v/LnzFcw0mXvo&#038;rel=0&#038;color1=0x234900&#038;color2=0x4e9e00&#038;border=0"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/LnzFcw0mXvo&#038;rel=0&#038;color1=0x234900&#038;color2=0x4e9e00&#038;border=0" type="application/x-shockwave-flash" wmode="transparent" width="400" height="316"></embed></object></center></p>
<p>We perform this slide-and-subtract operation from right-to-left (R-L) and left-to-right (L-R).  Then we try to eliminate bad pixels in two ways.  First, we use the disparity from the R-L pass or the L-R pass depending on which has the lowest matching difference.  Next, we mark as bad all points where the R-L disparity is significantly different from the L-R disparity.  Finally, we are left with a pixel disparity map.</p>
<p><center><a href='http://www.shawnlankton.com/wp-content/uploads/2007/12/original_disparity.png' title='Pixel Disparity'><img src='http://www.shawnlankton.com/wp-content/uploads/2007/12/original_disparity.thumbnail.png' alt='Pixel Disparity' /></a></center></p>
<p>In this image, red-er colors indicate closer pixels, and blue-er colors represent pixels that are farther away.</p>
<h2>Filtering the Pixel Disparity</h2>
<p>In the next step, we combine image information with the pixel disparities to get a cleaner disparity map.  First, we segment the reference image using a technique called &#8220;<a href="http://www.shawnlankton.com/2007/11/mean-shift-segmentation-in-matlab/">Mean Shift Segmentation</a>.&#8221;  This is a clustering algorithm that &#8220;over-segments&#8221; the image.  The result is a very &#8216;blocky&#8217; version of the original image.</p>
<p>Then, for each segment, we look at the associated pixel disparities.  In my simple implementation, we assign each segment to have the median disparity of all the pixels within that segment.  This gives the final result:</p>
<p><center><a href='http://www.shawnlankton.com/wp-content/uploads/2007/12/filtered_disparity.png' title='Filtered Disparity'><img src='http://www.shawnlankton.com/wp-content/uploads/2007/12/filtered_disparity.thumbnail.png' alt='Filtered Disparity' /></a></center></p>
<p>Here again, the red colors are close objects, and blue objects are far away.</p>
<h2>Matlab Code</h2>
<p>I spent some time getting these simple ideas into working form.  I&#8217;ve posted the codes and images I used as well as a demo script.  To see how the code works, simply un-archive everything and run <strong>demo</strong> from a Matlab prompt. Enjoy, and let me know how these work for you.</p>
<blockquote><p>
<a href="http://www.shawnlankton.com/wp-content/uploads/files/stereo_modefilt.zip">stereo_modefilt.zip</a> (New! <a href="http://www.shawnlankton.com/2008/04/stereo-vision-update-with-new-code/">Described here.</a>)<br/><br />
<del datetime="2009-07-02T17:55:52+00:00"><a href="http://www.shawnlankton.com/wp-content/uploads/2007/12/lankton_stereo.tar.gz">lankton_stereo.tar.gz</a></del> (Old)
</p></blockquote>
<p>Check out the newest results:</p>
<p><center><br />
<embed type="application/x-shockwave-flash" src="http://picasaweb.google.com/s/c/bin/slideshow.swf" width="400" height="267" flashvars="host=picasaweb.google.com&#038;RGB=0x000000&#038;feed=http%3A%2F%2Fpicasaweb.google.com%2Fdata%2Ffeed%2Fapi%2Fuser%2Fshawn.lankton%2Falbumid%2F5188210756103456801%3Fkind%3Dphoto%26alt%3Drss%26authkey%3DxyPWYtqp8GM" pluginspage="http://www.macromedia.com/go/getflashplayer"><br/>[red indicates close, blue indicates far away]</embed></center></p>
<h2>Conclusion</h2>
<p>This stereo algorithm is just a tool to be used on other projects.  For instance, by computing the stereo disparity of a stereoscopic video it is possible to improve tracking results by using the 3D information.  Also, segmentations can be made more accurate if 3D information is known.</p>
<p>I wanted to put this up to introduce people to stereo vision (as this was my introductory project).  Hopefully, the words and the codes above will save you some time getting up to speed.  I would ask that if you find this helpful and make improvements that you let me know!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shawnlankton.com/2007/12/3d-vision-with-stereo-disparity/feed/</wfw:commentRss>
		<slash:comments>34</slash:comments>
		</item>
		<item>
		<title>Mean Shift Segmentation in Matlab</title>
		<link>http://www.shawnlankton.com/2007/11/mean-shift-segmentation-in-matlab/</link>
		<comments>http://www.shawnlankton.com/2007/11/mean-shift-segmentation-in-matlab/#comments</comments>
		<pubDate>Fri, 16 Nov 2007 21:43:00 +0000</pubDate>
		<dc:creator>Shawn Lankton</dc:creator>
				<category><![CDATA[Matlab]]></category>
		<category><![CDATA[Vision]]></category>
		<category><![CDATA[image processing]]></category>
		<category><![CDATA[segmentation]]></category>

		<guid isPermaLink="false">http://www.shawnlankton.com/2007/11/mean-shift-segmentation-in-matlab/</guid>
		<description><![CDATA[Background
Recently I have decided to explore tracking from 3D point clouds extracted from stereo vision cameras.  Step 1: Extract 3D point cloud from stereo vision cameras.  So right now I&#8217;m implementing Segment-Based Stereo Matching Using Belief Propogation and Self-Adapting Dissimilarity Measure&#8221; by Klaus, Sormann, and Karner. This paper is defined by  the [...]]]></description>
			<content:encoded><![CDATA[<h2>Background</h2>
<p>Recently I have decided to explore tracking from 3D point clouds extracted from stereo vision cameras.  Step 1: Extract 3D point cloud from stereo vision cameras.  So right now I&#8217;m implementing <a href="http://www.vrvis.at/2d3d/technology/stereomatching/stereomatching.html">Segment-Based Stereo Matching Using Belief Propogation and Self-Adapting Dissimilarity Measure&#8221;</a> by Klaus, Sormann, and Karner. This paper is defined by <a href="http://vision.middlebury.edu/stereo/"> the source on stereo vision</a> to be the best one around.  This paper has two parts.  Part 1: Segment the image. Part 2: Compute disparity (and depth) from the segments.  Well, today I finished Part 1.</p>
<p><center><a href='http://www.shawnlankton.com/wp-content/uploads/2007/11/stereocameras.jpeg' title='Stereo Cameras'><img src='http://www.shawnlankton.com/wp-content/uploads/2007/11/stereocameras.thumbnail.jpeg' alt='Stereo Cameras' /></a></center></p>
<h2>First Try</h2>
<p>The authors refer to a mean-shift segmentation algorithm presented in <a href="http://www.google.com/url?sa=t&#038;ct=res&#038;cd=1&#038;url=http%3A%2F%2Fcourses.csail.mit.edu%2F6.869%2Fhandouts%2FPAMIMeanshift.pdf&#038;ei=zAc-R7qBIoL8gQSxp_XUAw&#038;usg=AFQjCNEwMLyGtgiJJPJhetSKiJ_DTA4rAw&#038;sig2=PEnW5eXVRTR4KtXqN9ymoQ">Mean Shift: A Robust Approach Toward Feature Space Analysis&#8221; [pdf]</a> by Comaniciu and Meer to do the image segmentation.  This paper (unlike some of my own previous work) leans towards <em>over</em>segmentation of an image.  Meaning that you prefer to get lots of little bits rather than the &#8220;right object&#8221; after the algorithm has run.</p>
<p>Well, after looking over the paper and getting a grasp for the mathematics, I took a crack at implementing it.  Easily done&#8230; HOWEVER, my first attempt, written in Matlab, was <em>painfully</em> slow.  (For a simple image it took 6 hours to run!)  So, I got on the internet and came up with a better solution!</p>
<h2>The Solution</h2>
<p>Some <a href="http://www.caip.rutgers.edu/riul/">great guys at Rutgers University</a> implemented this paper in C++ and made the code available to the public under the name <a href="http://www.caip.rutgers.edu/riul/research/code/EDISON/index.html">EDISON</a>. (there&#8217;s also a nice GUI that goes along with this if you want to just play to see if these codes will work for you).  Okay, so I had C++ codes that worked well (only 2 sec to do an image rather than 6 hours).  The next step was to bring the code into Matlab.</p>
<p><center><a href='http://www.shawnlankton.com/wp-content/uploads/2007/11/ms_results.jpg' title='Mean Shift Segmentation Results'><img src='http://www.shawnlankton.com/wp-content/uploads/2007/11/ms_results.thumbnail.jpg' alt='Mean Shift Segmentation Results' /></a><br />
These were the type of results I was trying for</center></p>
<p>I cracked my knuckles and got ready to write a MEX wrapper for this EDISON code.  Then I said to myself, &#8220;Self, maybe you should check the &#8216;net first.&#8221; Turns out I had a good point.  I found <a href="http://www.wisdom.weizmann.ac.il/~bagon/matlab.html">the website of Shai Bagon</a>.  Mr. Bagon had already made the MEX wrapper!  Awesome.</p>
<p>I downloaded the codes and put them together.  Mr. Bagon&#8217;s stuff worked right out of the box, although it would have saved me about an hour if I would have had this information (<a href="http://www.shawnlankton.com/wp-content/uploads/2007/11/readme.txt">alternative readme.txt for Matlab Interface for EDISON</a>).  I also wrote my own wrapper-wrapper so that I could process grayscale images, and do simpler calls to accomplish what I wanted.  If you&#8217;d like the code, download my wrapper-wrapper here (<a href="http://www.shawnlankton.com/wp-content/uploads/2007/11/msseg.m">msseg.m</a>).</p>
<h2>Results</h2>
<p>Here is a sample of the output of this algorithm.  The first image is a regular photo of some posed objects.  The second image is the segmented version.  Notice how the regions of the image are much, much more constant.  This image has been broken into &#8220;tiles&#8221; of constant color.<br />
<center><a href='http://www.shawnlankton.com/wp-content/uploads/2007/11/i1.png' title='Left Image'><img src='http://www.shawnlankton.com/wp-content/uploads/2007/11/i1.thumbnail.png' alt='Left Image' /></a><br />
The original image (part of a standard pair of test images).<br />
<a href='http://www.shawnlankton.com/wp-content/uploads/2007/11/s1.png' title='segmented image'><img src='http://www.shawnlankton.com/wp-content/uploads/2007/11/s1.thumbnail.png' alt='segmented image' /></a><br />
The segmented image (ready to be processed in step 2)</center></p>
<h2>Conclusion</h2>
<p>Don&#8217;t re-invent the wheel.  Taking a first crack at the implementation was good, and it helped me understand the algorithm.  However, there was no need for me to spend a week tweaking it to be super-fast or two days getting the Matlab interface working.  These things had already been done!  It feels nice to knock out a task that you thought was going to take a week in a few hours : ) Stay tuned for the stereo part of this paper coming soon.  Then maybe people will be writing about <em>my</em> page!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shawnlankton.com/2007/11/mean-shift-segmentation-in-matlab/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
	</channel>
</rss>
