
<?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>The Visioneers &#187; emgu</title>
	<atom:link href="http://www.oliveragustin.com/category/emgu/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.oliveragustin.com</link>
	<description>Tech, IT, Security, Research, Trends and  Tips</description>
	<lastBuildDate>Thu, 26 Jan 2012 10:45:23 +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>Background Subtraction in C#</title>
		<link>http://www.oliveragustin.com/background-subtraction-in-c/</link>
		<comments>http://www.oliveragustin.com/background-subtraction-in-c/#comments</comments>
		<pubDate>Fri, 14 Nov 2008 17:16:27 +0000</pubDate>
		<dc:creator>whaldsz</dc:creator>
				<category><![CDATA[emgu]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[projects]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[background subtraction]]></category>
		<category><![CDATA[emgu.cv]]></category>
		<category><![CDATA[opencv]]></category>

		<guid isPermaLink="false">http://www.oliveragustin.com/background-subtraction-in-c</guid>
		<description><![CDATA[I have been trying to learn OpenCV for a while now and I came across Emgu.CV, a C# wrapper.  Although the Emgu.CV is still under development, it has a many features that will make your work a lot easier if you&#8217;re doing a project in computer vision and image processing areas.  Some of its capabilities [...]]]></description>
			<content:encoded><![CDATA[<p>I have been trying to learn <a href="http://sourceforge.net/projects/opencvlibrary/" rel="nofollow">OpenCV</a> for a while now and I came across <a href="http://www.emgu.com" rel="nofollow">Emgu.CV</a>, a C# wrapper.  Although the Emgu.CV is still under development, it has a many features that will make your work a lot easier if you&#8217;re doing a project in computer vision and image processing areas.  Some of its capabilities enable you to develop  web applications that can do processing of video and images via TCP/IP.</p>
<p>My goal in this article is to demonstrate simple background segmentation techniques using Emgu.CV.  Background segmentation is important if you are trying to extract features from an image and you want to remove or filter out unwanted objects.  You may also find this technique important when the recovery of color information from foreground objects is important.</p>
<p>Our objective is to remove the background from the rice image below. We want to get the color information from the rice kernels and be able to extract features for for each kernel, maybe for shape analysis or classification purposes.</p>
<p>&nbsp;</p>
<p><a href="http://www.oliveragustin.com/wp-content/uploads/2008/11/image.png"><img style="border-width: 0px;" src="http://www.oliveragustin.com/wp-content/uploads/2008/11/image-thumb.png" alt="Original image" width="244" height="204" border="0" /></a></p>
<p>I am sure there are other ways to perform background subtraction but this article will cover three methods that .</p>
<ol>
<li>Color filtering in Cielab color space</li>
<li>Image masking (as i call it)</li>
<li>Cielab + image masking</li>
</ol>
<p>The three methods will be explained shortly and the link to the source code is available at the visioneer forum.</p>
<p><strong>Color Filtering in Cielab Color Space</strong></p>
<p>In this method, a copy of RGB image in Cielab space is obtained, and then process this image (in Cielab space) pixel by pixel.  Each pixel is checked if its color value is within specified range, otherwise, the pixel in the corresponding RGB image is set to black color.  An implementation is visible below:</p>
<div style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px;">
<div style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 1:</span> <span style="color: #008000;">// background subtraction in cielab color space</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 2:</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> CielabColorFilteringBGSubtraction(<span style="color: #0000ff;">string</span> filename, <span style="color: #0000ff;">bool</span> displayResult)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 3:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 4:</span>     <span style="color: #008000;">// create new image from file</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 5:</span>     Image&lt;Bgr, Byte&gt; rgbimage = <span style="color: #0000ff;">new</span> Image&lt;Bgr, <span style="color: #0000ff;">byte</span>&gt;(filename);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 6:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 7:</span>     <span style="color: #008000;">// make a copy of the image in lab color space</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 8:</span>     Image&lt;Lab, Byte&gt; labimage = rgbimage.Convert&lt;Lab, Byte&gt;();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 9:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 10:</span>     <span style="color: #008000;">// get the width and size</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 11:</span>     <span style="color: #0000ff;">int</span> width = labimage.Width;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 12:</span>     <span style="color: #0000ff;">int</span> height = labimage.Height;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 13:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 14:</span>     <span style="color: #008000;">// get the filter range for each channel in lab color space</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 15:</span>     IntRange l = <span style="color: #0000ff;">new</span> IntRange(Byte.Parse(min1TextBox.Text), Byte.Parse(max1TextBox.Text));</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 16:</span>     IntRange a = <span style="color: #0000ff;">new</span> IntRange(Byte.Parse(min2TextBox.Text), Byte.Parse(max2TextBox.Text));</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 17:</span>     IntRange b = <span style="color: #0000ff;">new</span> IntRange(Byte.Parse(min3TextBox.Text), Byte.Parse(max3TextBox.Text));</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 18:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 19:</span>     <span style="color: #008000;">// process each row in the image</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 20:</span>     <span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 0; i &lt; width; i++)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 21:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 22:</span>         <span style="color: #008000;">// process each pixel</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 23:</span>         <span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> j = 0; j &lt; height; j++)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 24:</span>         {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 25:</span>             <span style="color: #0000ff;">if</span> (</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 26:</span>                 (labimage[j, i].X &gt;=l.Max ) || (labimage[j, i].X &lt;= l.Min) ||</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 27:</span>                  (labimage[j, i].Y &gt;= a.Max) || (labimage[j, i].Y &lt;= a.Min) ||</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 28:</span>                  (labimage[j, i].Z &gt;= b.Max) || (labimage[j, i].Z &lt;= b.Min)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 29:</span>                 )</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 30:</span>             {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 31:</span>                 <span style="color: #008000;">// if outside the filter range, set the pixel to black color</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 32:</span>                 rgbimage[j, i] = <span style="color: #0000ff;">new</span> Bgr(0, 0, 0);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 33:</span>             }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 34:</span>         }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 35:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 36:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 37:</span>     <span style="color: #008000;">// display</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 38:</span>     <span style="color: #0000ff;">if</span> (displayResult) <span style="color: #0000ff;">this</span>.NewImage(<span style="color: #006080;">"Cielab-based background segmented image"</span>, rgbimage);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 39:</span> }</pre>
</div>
</div>
<p>The resulting image looks like this.  In my PC, it took about 3.20 sec to perform background segmentation.</p>
<p><a href="http://www.oliveragustin.com/wp-content/uploads/2008/11/output.png"><img style="border-width: 0px;" src="http://www.oliveragustin.com/wp-content/uploads/2008/11/output-thumb.png" alt="output" width="244" height="204" border="0" /></a></p>
<p><strong>Image Masking</strong></p>
<p>In this method, what we do is convert an RGB image into binary via binary thresholding.  (The choice of threshold value depends on the image, so you have to experiment on this.)  This binary image will serve as the mask image to copy pixels from the original image to the destination image, if the corresponding pixel in the mask image is nonzero. This is given by the following representation:</p>
<p>DestinatioImage(x,y) = SourceImage(x,y) if MaskImage(x,y) &lt;&gt; 0</p>
<p>where x and y is the pixel coordinates of the image. Source, destination and mask image have the same size.</p>
<p>Code implementation is as follows:</p>
<div style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px;">
<div style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 1:</span> <span style="color: #008000;">// background subtraction by converting the RGB image to binary to create the mask</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 2:</span> <span style="color: #008000;">// then use this mask to copy foreground objects in the image</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 3:</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> RGBImageMaskBGSubtraction(<span style="color: #0000ff;">string</span> filename, <span style="color: #0000ff;">bool</span> displayResult)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 4:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 5:</span>     <span style="color: #008000;">// load the threshold value for grayscale image</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 6:</span>     <span style="color: #0000ff;">double</span> threshold = <span style="color: #0000ff;">double</span>.Parse(max2TextBox.Text);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 7:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 8:</span>     <span style="color: #008000;">// create new image</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 9:</span>     Image&lt;Bgr, Byte&gt; img = <span style="color: #0000ff;">new</span> Image&lt;Bgr, <span style="color: #0000ff;">byte</span>&gt;(filename);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 10:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 11:</span>     <span style="color: #008000;">//convert to grayscale</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 12:</span>     Image&lt;Gray, Byte&gt; gray = img.Convert&lt;Gray, Byte&gt;();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 13:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 14:</span>     <span style="color: #008000;">//convert to binary image using the threshold</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 15:</span>     gray = gray.ThresholdBinary(<span style="color: #0000ff;">new</span> Gray(threshold), <span style="color: #0000ff;">new</span> Gray(255));</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 16:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 17:</span>     <span style="color: #008000;">// copy pixels from the original image where pixels in </span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 18:</span>     <span style="color: #008000;">// mask image is nonzero</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 19:</span>     Image&lt;Bgr, Byte&gt; newimg = img.Copy(gray);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 20:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 21:</span>     <span style="color: #008000;">// display result</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 22:</span>     <span style="color: #0000ff;">if</span> (displayResult) <span style="color: #0000ff;">this</span>.NewImage(<span style="color: #006080;">"Background segmented"</span>, newimg);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 23:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 24:</span> }</pre>
</div>
</div>
<p>One thing we should be aware of is that, all channels in RGB image is used to convert the image in grayscale image, then eventually binary image.  The output image is shown below:</p>
<p><a href="http://www.oliveragustin.com/wp-content/uploads/2008/11/output1.png"><img style="border-width: 0px;" src="http://www.oliveragustin.com/wp-content/uploads/2008/11/output-thumb1.png" alt="output" width="244" height="204" border="0" /></a></p>
<p>Obviously, this is far worst then the first method.  But the advantage of segmentation using this technique is the speed.  The image was segmented for only 0.25 sec with more than 12X improvement, but we have to live with the quality. According to my tests, the performance improvement increases as the image size increased.  If we are very much concerned with the pixels in foreground objects, then the Image Masking method will not satisfy our requirements.</p>
<p>However, there is another method to achieve the segmentation quality of cielab color filtering method, and the speed of the image masking method.</p>
<p><strong>Cielab + Image masking</strong></p>
<p>In this method, we combine the first two techniques to get the cream of both ice creams. <img src='http://www.oliveragustin.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />   What we do is to perform the processing as in image masking method, but instead of using 3 channels (in RGB format) to convert the image to grayscale, we convert the image into Cielab space, select the a*-channel of the image, and use this channel to derive the mask image.</p>
<p>Code implementation here:</p>
<div style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px;">
<div style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 1:</span> <span style="color: #008000;">// background subtraction by extracting one channel in cielab image to </span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 2:</span>  <span style="color: #008000;">// create binary mask, and use this mask to copy foreground objects in </span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 3:</span>  <span style="color: #008000;">// the original image</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 4:</span>  <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> CielabChannelMaskBGSubtraction(<span style="color: #0000ff;">string</span> filename, <span style="color: #0000ff;">bool</span> displayResult)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 5:</span>  {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 6:</span>      <span style="color: #0000ff;">double</span> threshold = <span style="color: #0000ff;">double</span>.Parse(max2TextBox.Text);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 7:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 8:</span>      Image&lt;Bgr, <span style="color: #0000ff;">byte</span>&gt; rgb = <span style="color: #0000ff;">new</span> Image&lt;Bgr, <span style="color: #0000ff;">byte</span>&gt;(filename);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 9:</span>      Image&lt;Lab, Byte&gt; img = rgb.Convert&lt;Lab, Byte&gt;();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 10:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 11:</span>      <span style="color: #008000;">//get the a* channel </span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 12:</span>      Image&lt;Gray, Byte&gt; gray = img[channel];</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 13:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 14:</span>      <span style="color: #008000;">//threshold and invert</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 15:</span>      gray = gray.ThresholdBinary(<span style="color: #0000ff;">new</span> Gray(threshold), <span style="color: #0000ff;">new</span> Gray(255)).Not();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 16:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 17:</span>     <span style="color: #008000;">// display the result</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px;"><span style="color: #606060;"> 18:</span>      <span style="color: #0000ff;">if</span> (displayResult) <span style="color: #0000ff;">this</span>.NewImage(<span style="color: #006080;">"Background segmented"</span>, image);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px;"><span style="color: #606060;"> 19:</span>  }</pre>
</div>
</div>
<p>Resulting output image here, note that it has almost the same quality as in the first method but with little performance penalty (processing takes around 0.03 sec longer than the second method).</p>
<p><a href="http://www.oliveragustin.com/wp-content/uploads/2008/11/output2.png"><img style="border-width: 0px;" src="http://www.oliveragustin.com/wp-content/uploads/2008/11/output-thumb2.png" alt="output" width="244" height="204" border="0" /></a></p>
<p>Please note that you need to tweak the filter range(threshold value) for this to work in your problem area. Also,</p>
<ol>
<li>In Method 1, all three channels are used as filter ranges.</li>
<li>Method 2, uses channel 1(maximum) for grayscale threshold value</li>
<li>In method 3, you can select among the radio buttons (on the right) which channels to choose as the source for the mask image.  Then modify the filter range for that selected channel mask. You can also modify the source code to select one or more channels if it gives better performance.</li>
<li>I have included two images located in the source code folder for testing purposes.</li>
</ol>
<p>Screenshot is given below:</p>
<p><a href="http://www.oliveragustin.com/wp-content/uploads/2008/11/image1.png"><img style="border: 0px;" src="http://www.oliveragustin.com/wp-content/uploads/2008/11/image-thumb1.png" alt="image" width="244" height="202" border="0" /></a></p>
<p>[attachments force_saveas="0" logged_users="0"]</p>
<p style="white-space:nowrap"><img style="border:0px" src="http://tarpipe.com/img/tarpipe.png" />&nbsp;<a target="_blank" href="http://tarpipe.com/share/?t=Background+Subtraction+in+C%23&u=http%3A%2F%2Fwww.oliveragustin.com%2Fbackground-subtraction-in-c%2F&b=Reading %22Background+Subtraction+in+C%23%22">Share now!</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.oliveragustin.com/background-subtraction-in-c/feed/</wfw:commentRss>
		<slash:comments>65</slash:comments>
		</item>
	</channel>
</rss>

