<?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>Damiles &#187; cvThreshold</title>
	<atom:link href="http://blog.damiles.com/tag/cvthreshold/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.damiles.com</link>
	<description>Blender, OpenCV, OpenCV, Tutorials and more...</description>
	<lastBuildDate>Mon, 11 Feb 2013 13:36:42 +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>OpenCV More with cameras.</title>
		<link>http://blog.damiles.com/2008/11/opencv-more-with-cameras/</link>
		<comments>http://blog.damiles.com/2008/11/opencv-more-with-cameras/#comments</comments>
		<pubDate>Mon, 03 Nov 2008 20:41:21 +0000</pubDate>
		<dc:creator>damiles</dc:creator>
				<category><![CDATA[OpenCV]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[cvAbsDiff]]></category>
		<category><![CDATA[cvCloneImage]]></category>
		<category><![CDATA[cvCreateImage]]></category>
		<category><![CDATA[cvCreateTrackbar]]></category>
		<category><![CDATA[cvCvtColor]]></category>
		<category><![CDATA[cvThreshold]]></category>

		<guid isPermaLink="false">http://blog.damiles.com/?p=67</guid>
		<description><![CDATA[In last tutorial we learn how to get frames of our camera, now we go to learn some basic OpenCV functions to work with our captured frames or video. There is nothing special, but it&#8217;s the first steps to introduce to work with OpenCV Imagine we want create a basic OpenCV program that get a [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><a href="http://blog.damiles.com/wp-content/uploads/2008/11/cam_diff_thres.jpg"><img class="aligncenter size-full wp-image-68" title="cam_diff_thres" src="http://blog.damiles.com/wp-content/uploads/2008/11/cam_diff_thres.jpg" alt="" width="347" height="166" /></a></p>
<p>In last tutorial we learn how to get frames of our camera, now we go to learn some basic OpenCV functions to work with our captured frames or video. There is nothing special, but it&#8217;s the first steps to introduce to work with OpenCV</p>
<p><span id="more-67"></span>Imagine we want create a basic OpenCV program that get a video o camera capture and process the images to get a binary image difference between actual frame and last frame.</p>
<p>Then we have a actual image, lastImage, diffImage where we store the difference images, and the bitImage result that are IplImage instance.</p>
<blockquote><p>IplImage* image;<br />
IplImage* lastImage;<br />
IplImage* diffImage;<br />
IplImage* bitImage;</p></blockquote>
<p>Whe need create these variables and initialize with cvCreateImage, this function create and allocate in memory the structure and headers of our image, this function need as attribute image size, depth and number of channels. We get frame as reference to initialize the variables.</p>
<blockquote><p>image=cvCreateImage(cvSize(frame-&gt;width,frame-&gt;height),frame-&gt;depth,1);</p></blockquote>
<p>The frame whe get sure is a RGB image, but we go to working with gray level images the we need conver captured frame to Gray Level. The cvCvtColor conver color spaces of image to another and is defined:</p>
<blockquote><p>void cvCvtColor( const CvArr* src, CvArr* dst, int code );</p></blockquote>
<dl>
<dt><strong>src</strong> is the source 8-bit (8u), 16-bit (16u) or single-precision floating-point (32f) image. <strong>dst</strong> is the destination image of the same data type as the source one. The number of channels may be different. And <strong>code</strong> is color conversion operation that can be specified using CV_&lt;src_color_space&gt;2&lt;dst_color_space&gt; constants</dt>
</dl>
<blockquote><p>cvCvtColor(frame, image, CV_BGR2GRAY);</p></blockquote>
<p>We can get a new copy in memory of image withcvCloneImage</p>
<blockquote><p>lastImage=cvCloneImage(image);</p></blockquote>
<p>To get the image differences we use cvAbsDiff function, to get absolute diference between images:</p>
<blockquote><p>cvAbsDiff(image,lastImage,diffImage);</p></blockquote>
<p>The function definition</p>
<blockquote><p>void cvAbsDiff( const CvArr* src1, const CvArr* src2, CvArr* dst );</p></blockquote>
<dl>
<dt>Where src1 is the first source array. <strong>src2</strong> is the second source array, and <strong>dst</strong> is the destination array.</dt>
</dl>
<p>Then to finish we only need get a threshold of image, and we do it with cvThreshold function</p>
<blockquote><p>cvThreshold(diffImage,bitImage,tr,255,CV_THRESH_BINARY);</p></blockquote>
<p>And this function have this definition:</p>
<blockquote><p>double cvThreshold( const CvArr* src, CvArr* dst, double threshold,<br />
double max_value, int threshold_type );</p></blockquote>
<dl>
<dt>Where <strong>src</strong> is source array (single-channel, 8-bit of 32-bit floating point), <strong>dst</strong> is destination array; must be either the same type as <code>src</code> or 8-bit, <strong>threshold</strong> is the threshold value, <strong>max_value</strong> is the maximum value to use with <code>CV_THRESH_BINARY</code> and <code>CV_THRESH_BINARY_INV</code> thresholding types, and <strong>threshold_type</strong> is thresholding type</dt>
</dl>
<p>This is the main loop program:</p>
<blockquote><p>for(;;){<br />
IplImage* frame = 0;<br />
int c;</p>
<p>frame = cvQueryFrame( capture );<br />
if( !frame )<br />
break;<br />
//If is the first frame<br />
if(!image){<br />
//Create image header same as frame but with 1 channel to gray<br />
image=cvCreateImage(cvSize(frame-&gt;width,frame-&gt;height),frame-&gt;depth,1);<br />
bitImage=cvCreateImage(cvSize(frame-&gt;width,frame-&gt;height),frame-&gt;depth,1);<br />
}<br />
//Convert frame to gray and store in image<br />
cvCvtColor(frame, image,CV_BGR2GRAY);</p>
<p>//If is the first frame<br />
if(!lastImage){</p>
<p>//If no lastImage clone actual image;<br />
lastImage=cvCloneImage(image);<br />
}<br />
if(!diffImage){<br />
//Create image header same as frame but with 1 channel to gray<br />
diffImage=cvCreateImage(cvSize(frame-&gt;width,frame-&gt;height),frame-&gt;depth,1);<br />
}</p>
<p>cvShowImage( &#8220;CamSub&#8221;, frame );<br />
//Differences with actual and last image<br />
cvAbsDiff(image,lastImage,diffImage);<br />
//threshold image<br />
cvThreshold(diffImage,bitImage,tr,255,CV_THRESH_BINARY);<br />
//Change datas;<br />
lastImage=cvCloneImage(image);</p>
<p>cvShowImage(&#8220;CamSub 1&#8243;,bitImage);</p>
<p>c = cvWaitKey(10);<br />
if( (char) c == 27 )<br />
break;<br />
}</p></blockquote>
<p><a title="Demo Source Camera II" href="http://www.artresnet.com/david/resource.jsp?f=7" target="_blank">Demo Source</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.damiles.com/2008/11/opencv-more-with-cameras/feed/</wfw:commentRss>
		<slash:comments>81</slash:comments>
		</item>
	</channel>
</rss>
