<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns="http://www.w3.org/TR/REC-html40">

<head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 6.0">
<meta name="ProgId" content="FrontPage.Editor.Document">

<title>OpenCV exercise1</title>
<style>
<!--
table.MsoTableGrid
	{border:1.0pt solid windowtext;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:10.0pt;
	font-family:"Times New Roman"}
h3
	{margin-top:6.0pt;
	margin-right:0cm;
	margin-bottom:3.0pt;
	margin-left:36.0pt;
	text-indent:-36.0pt;
	line-height:12.0pt;
	page-break-after:avoid;
	font-size:10.0pt;
	font-family:Arial;
	font-weight:normal;
	font-style:italic;
	}
-->
</style>
</head>

<body>

<pre>              <b><font size="6" color="#FF0000"> <span lang="en-ca">The Learning of OpenCV</span></font></b></pre>
<pre>					
</pre>
<pre>
</pre>
<pre><font color="#FF0000" size="4"><b>Here contains just <span lang="en-ca">the exercises in book &quot;Learning OpenCV&quot; and of course I believe there is better solution for them.</span></b></font></pre>
<pre><span lang="en-ca">problems: </span></pre>
<pre><span lang="en-ca">1. If you set cvSetImageCOI, don't forget to set it back with parameter &quot;0&quot;, otherwise you may end up with single channel.</span></pre>
<pre><span lang="en-ca">2. If you get your IplImage from &quot;cvCapture&quot;, then do NOT release it as it is not created by you!</span></pre>
<pre><span lang="en-ca">3. There is some memory allocation in &quot;cvSubImageHeader, and do NOT forget to release them. </span></pre>
<pre><span lang="en-ca">4. I tried to erase my drawing lines by &quot;xor&quot; it again, but it seems not working by using &quot;cvGetRow&quot;. Maybe it is because I mess up with &quot;cvSetImageROI without reset it. So, I just use cvCopy to </span></pre>
<pre><span lang="en-ca">overwrite it.</span></pre>
<pre><span lang="en-ca">5. When you draw your rectangle of some width of line, be careful that the rectangle is bigger than that width of line.</span></pre>
<pre><span lang="en-ca">6. The color sequence in bytes order is &quot;BGR&quot;. </span></pre>
<pre><font color="#FF0000"><span lang="en-ca"><a name="chapter4-1">The following are exercises in chapter 4.</a></span></font></pre>
<pre><span lang="en-ca">/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////</span></pre>
<pre>#include &lt;cv.h&gt;
#include &lt;highgui.h&gt;
#include &lt;stdio.h&gt;
#include &lt;math.h&gt;
#include &lt;string.h&gt;
#include &lt;windows.h&gt;
#include &lt;tchar.h&gt;
#include &lt;time.h&gt;

#pragma comment(lib, &quot;cvd.lib&quot;)
#pragma comment(lib, &quot;cxcored.lib&quot;)
#pragma comment(lib, &quot;highguid.lib&quot;)

CvFont myfont = cvFont(1, 2);
char* szCombineName = &quot;combine image&quot;;

bool bMouseButtonDown = false;
int x = 0, y = 0;


void doDrawing(IplImage* pImage)
{
	char buffer[256];
	int winX = 0, winY = 0;
	if (bMouseButtonDown)
	{
		winX = x;
		winY = y;
		sprintf(buffer, &quot;mouse click at [%d,%d]&quot;, x, y);
		cvPutText(pImage, buffer, cvPoint(winX, winY), &amp;myfont, cvScalar(255,0,0));
	}
}

void myMouseCallback(int event, int myx, int myy, int flag, void* pUser)
{
	switch (event)
	{
	case CV_EVENT_LBUTTONDOWN:	
		bMouseButtonDown = true;
		x = myx;
		y = myy;
		break;
	case CV_EVENT_LBUTTONUP:
		bMouseButtonDown = false;
		break;
	}
}

void exercise1()
{
	char* szVideoName = &quot;e:\\mytest.avi&quot;;
	char* szWindowName = &quot;my video&quot;;
	char* szGrayName = &quot;gray image&quot;;
	char* szCannyName = &quot;canny image&quot;;
	CvCapture* pCapture = NULL;
	IplImage* pImage = NULL;
	IplImage* pGrayImg = NULL;
	IplImage* pCannyImg = NULL;
	IplImage* pCombineImg = NULL;
	IplImage* pSub1= NULL;
	IplImage* pSub2= NULL;
	IplImage* pSub3= NULL;

	int nWidth = 0, nHeight = 0;
	
	cvNamedWindow(szWindowName);
	cvNamedWindow(szGrayName);
	cvNamedWindow(szCannyName);
	cvNamedWindow(szCombineName);
	

	pCapture = cvCreateFileCapture(szVideoName);
	if (pCapture)
	{
		nWidth = (int) cvGetCaptureProperty(pCapture, CV_CAP_PROP_FRAME_WIDTH);
		nHeight = (int) cvGetCaptureProperty(pCapture, CV_CAP_PROP_FRAME_HEIGHT);
		pGrayImg = cvCreateImage(cvSize(nWidth, nHeight), 8, 1);
		pCannyImg = cvCreateImage(cvSize(nWidth, nHeight), 8, 1);
		pCombineImg = cvCreateImage(cvSize(nWidth, 3*nHeight), 8, 1);

		pSub1 = cvCreateImageHeader(cvSize(nWidth, nHeight), 8, 1);
		pSub2 = cvCreateImageHeader(cvSize(nWidth, nHeight), 8, 1);
		pSub3 = cvCreateImageHeader(cvSize(nWidth, nHeight), 8, 1);
		
		pSub1-&gt;origin = pCombineImg-&gt;origin;
		pSub2-&gt;origin = pCombineImg-&gt;origin;
		pSub3-&gt;origin = pCombineImg-&gt;origin;

		pSub1-&gt;widthStep = pCombineImg-&gt;widthStep;
		pSub2-&gt;widthStep = pCombineImg-&gt;widthStep;
		pSub3-&gt;widthStep = pCombineImg-&gt;widthStep;

		pSub1-&gt;imageData = pCombineImg-&gt;imageData;

		pSub2-&gt;imageData = pCombineImg-&gt;imageData + nHeight*pCombineImg-&gt;widthStep;

		pSub3-&gt;imageData = pCombineImg-&gt;imageData + nHeight*2*pCombineImg-&gt;widthStep;

		cvSetMouseCallback(szCombineName, myMouseCallback, pCombineImg);
		//cvResizeWindow(szCombineName, pCombineImg-&gt;width, pCombineImg-&gt;height);
		do
		{
			if (cvWaitKey(15) == 27)
			{
				break;
			}
			
			pImage = cvQueryFrame(pCapture);
			if (pImage)
			{				
				cvConvertImage(pImage, pGrayImg, 0);
				cvCanny(pGrayImg, pCannyImg, 1, 2, 5);

				//cvShowImage(szWindowName, pImage);
				//cvShowImage(szGrayName, pGrayImg);
				//cvShowImage(szCannyName, pCannyImg);

				cvSetImageCOI(pImage, 1);
				cvCopy(pImage, pSub1, NULL);
				cvCopy(pGrayImg, pSub2, NULL);
				cvCopy(pCannyImg, pSub3, NULL);

				cvPutText(pSub1, szWindowName, cvPoint(nWidth/2, nHeight/2), &amp;myfont, cvScalar(255,0,0));

				cvPutText(pSub2, szGrayName, cvPoint(nWidth/2, nHeight/2), &amp;myfont, cvScalar(255,0,0));

				cvPutText(pSub3, szCannyName, cvPoint(nWidth/2, nHeight/2), &amp;myfont, cvScalar(255,0,0));

				doDrawing(pCombineImg);

				cvShowImage(szCombineName, pCombineImg);
				cvSetImageCOI(pImage, 0);

			}
		}
		while (true);
		// do NOT release pImage because it is only retrieved from capture
		//cvReleaseImage(&amp;pImage);
		cvReleaseImageHeader(&amp;pSub1);
		cvReleaseImageHeader(&amp;pSub2);
		cvReleaseImageHeader(&amp;pSub3);
		cvReleaseImage(&amp;pGrayImg);
		cvReleaseImage(&amp;pCannyImg);
		cvReleaseImage(&amp;pCombineImg);

		cvReleaseCapture(&amp;pCapture);
	}
	cvDestroyAllWindows();
}

void myMouseOnClick(int event, int x, int y, int flag, void* pUser)
{
	char buffer[256];
	CvFont myFont = cvFont(2, 2);
	IplImage* pImage = (IplImage*)pUser;
	char* ptr = NULL;
	unsigned char r=0, g=0, b=0;

	switch (event)
	{
	case CV_EVENT_LBUTTONDOWN:
		ptr = pImage-&gt;imageData + y*pImage-&gt;widthStep + x * pImage-&gt;depth* pImage-&gt;nChannels/8;
		r = ptr[0];
		g = ptr[1];
		b = ptr[2];
		sprintf(buffer, &quot;rgb [%d,%d,%d]&quot;, r,g,b);
		cvPutText(pImage, buffer, cvPoint(x, y), &amp;myFont, cvScalar(255,0,0));
		break;
	}

}


void exercise2()
{
	IplImage* pImage = NULL; //, *pCloneImage = NULL;
	pImage = cvLoadImage(&quot;mytest.jpg&quot;);
	//pCloneImage = cvCloneImage(pImage);
	cvNamedWindow(&quot;mytest&quot;);
	cvSetMouseCallback(&quot;mytest&quot;, myMouseOnClick, pImage);
	do
	{
		cvShowImage(&quot;mytest&quot;, pImage);
		if (cvWaitKey(1000)==27)
		{
			break;
		}
	}
	while (true);

	cvReleaseImage(&amp;pImage);
	cvDestroyWindow(&quot;mytest&quot;);
}


#define HistogramWidth   48
#define HistogramHeight  800
IplImage* pImage = NULL, *pCloneImage = NULL, *pHistImage= NULL;
CvRect rect = cvRect(0,0,0,0);
bool bStart = false;


#if 0
void eraseFrame()
{
	CvMat vecSrc, vecDst;
	cvSetImageROI(pImage, rect);
	cvSetImageROI(pCloneImage, rect);

	cvGetRow(pImage, &amp;vecSrc, 0);
	cvGetRow(pCloneImage, &amp;vecDst, 0);

	cvCopy(&amp;vecSrc, &amp;vecDst);

	cvGetRow(pImage, &amp;vecSrc, rect.height-1);
	cvGetRow(pCloneImage, &amp;vecDst, rect.height-1);

	cvCopy(&amp;vecSrc, &amp;vecDst);

	cvGetCol(pImage, &amp;vecSrc, 0);
	cvGetCol(pCloneImage, &amp;vecDst, 0);

	cvCopy(&amp;vecSrc, &amp;vecDst);

	cvGetCol(pImage, &amp;vecSrc, rect.width-1);
	cvGetCol(pCloneImage, &amp;vecDst, rect.width-1);

	cvCopy(&amp;vecSrc, &amp;vecDst);

	cvResetImageROI(pImage);
	cvResetImageROI(pCloneImage);
}
#else
void eraseFrame()
{
	cvSetImageROI(pImage, rect);
	cvSetImageROI(pCloneImage, rect);
	
	cvCopy(pCloneImage, pImage);

	cvResetImageROI(pImage);
	cvResetImageROI(pCloneImage);
}
#endif

#define LINE_WIDTH 1
void drawFrame()
{
	cvRectangle(pImage, cvPoint(rect.x, rect.y), cvPoint(rect.x+rect.width-1, rect.y+rect.height-1), cvScalarAll(255), LINE_WIDTH);	
	//cvLine(pImage, cvPoint(rect.x, rect.y), cvPoint(rect.x+rect.width, rect.y+rect.height), cvScalarAll(255), LINE_WIDTH);
}

void drawHistogram()
{
	char* ptr = NULL, *pRow = NULL, *pCol = NULL;
	char buffer[32];
	int hist[3][8]= {0};
	int index = 0;
	int nPixWidth = 0;
	int r, c, i, nOffset = 0;
	int x = 0 , y = 0;
	CvFont myFont = cvFont(1,1);
	int nMax = 0, nScale=1;
	nPixWidth = pCloneImage-&gt;nChannels*pCloneImage-&gt;depth/8;
	ptr = pCloneImage-&gt;imageData + rect.y*pCloneImage-&gt;widthStep + rect.x*nPixWidth;
	
	pRow = ptr;
	for (r = 0; r &lt; rect.height; r ++)
	{
		pCol = pRow;
		for (c = 0; c &lt; rect.width; c ++)
		{
			for (i =0; i &lt; 3; i ++)
			{
				index = pCol[i] / 32;
				hist[i][index] ++;
				if (hist[i][index] &gt; nMax)
				{
					nMax = hist[i][index];
				}
			}
			pCol += nPixWidth;
		}
		pRow += pCloneImage-&gt;widthStep;
	}
	cvSet(pHistImage, cvScalarAll(255));
	y = HistogramHeight;
	nScale = HistogramHeight / HistogramHeight;
	if (nScale == 0)
	{
		nScale = 1;
	}
	for (c = 0;  c &lt; 8;  c ++)
	{
		
		for (i = 0;  i &lt; 3; i ++)
		{
			unsigned char color[3]={0};
			x = nOffset;
			color[i] = 255;			

			if (hist[i][c] / nScale != 0)
			{			
				cvSetImageROI(pHistImage, cvRect(x+i*HistogramWidth, y - hist[i][c]/nScale, HistogramWidth, hist[i][c]/nScale));
				cvSet(pHistImage, cvScalar(color[0], color[1], color[2]));
				cvResetImageROI(pHistImage);
			}
			sprintf(buffer, &quot;%d&quot;, hist[i][c]);
			cvPutText(pHistImage, buffer, cvPoint(x+i*HistogramWidth, y-100), &amp;myFont, cvScalarAll(0));
		}
		nOffset += HistogramWidth*3;
	}
	cvShowImage(&quot;histogram&quot;, pHistImage);

}

void myMouseCallback3(int event, int x, int y, int flag, void* pUser)
{
	int w = 0, h = 0;
	switch (event)
	{
	case CV_EVENT_MOUSEMOVE:
		if (!bStart)
		{
			break;
		}

		w = x - rect.x;
		h = y - rect.y;
		if (w &gt; LINE_WIDTH &amp;&amp; h &gt; LINE_WIDTH)
		{
			if (w != rect.width || h != rect.height)
			{
				if (rect.width &gt; 0 &amp;&amp; rect.height &gt; 0)
				{
					
					eraseFrame();
					/////////////////////////////////			
				}	
			
				rect.width = w;
				rect.height = h;
				
				//cvSetImageROI(pImage, rect);
				//cvSetImageROI(pCloneImage, rect);

				//cvSet(pImage, cvScalarAll(255));
				drawFrame();

				//cvResetImageROI(pImage);
				//cvResetImageROI(pCloneImage);
			}
		}
		break;
	case CV_EVENT_LBUTTONDOWN:
		if (rect.width &gt; 0 &amp;&amp; rect.height &gt; 0)
		{
			cvSetImageROI(pImage, rect);
			cvSetImageROI(pCloneImage, rect);

			cvCopy(pCloneImage, pImage);
			cvResetImageROI(pImage);
			cvResetImageROI(pCloneImage);
			/////////////////////////////////			
		}	
		rect.x = x;
		rect.y = y;
		rect.width = rect.height = 0;
		bStart = true;
		break;
	case CV_EVENT_LBUTTONUP:
	
		if (rect.width &gt; 0 &amp;&amp; rect.height &gt; 0)
		{
			cvSetImageROI(pImage, rect);
			cvSet(pImage, cvScalarAll(255));
			cvResetImageROI(pImage);

			drawHistogram();
			
			/////////////////////////////////			
		}			
		bStart = false;
		break;
	}
}

void exercise3()
{
	pImage = cvLoadImage(&quot;mytest.jpg&quot;);
	pCloneImage = cvCloneImage(pImage);
	cvNamedWindow(&quot;histogram&quot;);
	cvNamedWindow(&quot;mytest&quot;);
	cvSetMouseCallback(&quot;mytest&quot;, myMouseCallback3, NULL);
	pHistImage = cvCreateImage(cvSize(8 * HistogramWidth * 3, HistogramHeight+200), 8, 3);
	do
	{
		cvShowImage(&quot;mytest&quot;, pImage);
		if (cvWaitKey(100)==27)
		{
			break;
		}
	}
	while (true);

	cvReleaseImage(&amp;pImage);
	cvReleaseImage(&amp;pCloneImage);
	cvDestroyWindow(&quot;mytest&quot;);
}

int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
	exercise3();
	//myTest();
	return 0;
}</pre>
<pre><span lang="en-ca">/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////</span></pre>
<pre><span lang="en-ca"><font size="4">There is a problem that I don't know how to control the program flow. For example, I setup two cvWaitKey because I place one in my main program and another </font></span></pre>
<pre><span lang="en-ca"><font size="4">one in the while loop of &quot;playing video&quot;. You see, if you don't place a &quot;cvWaitKey&quot; inside the video playing loop, then there is simply no redrawing of </font></span></pre>
<pre><span lang="en-ca"><font size="4">window. Just image there is no overlapping threads to either decode video or draw window.</font></span></pre>
<pre><b><font color="#FF0000" size="4"><span lang="en-ca">The following is a simple program acting as an video player(exercise4)</span></font></b></pre>
<pre>　</pre>
<pre>#include &lt;cv.h&gt;
#include &lt;highgui.h&gt;
#include &lt;stdio.h&gt;
#include &lt;math.h&gt;
#include &lt;string.h&gt;
#include &lt;windows.h&gt;
#include &lt;tchar.h&gt;
#include &lt;time.h&gt;

#pragma comment(lib, &quot;cvd.lib&quot;)
#pragma comment(lib, &quot;cxcored.lib&quot;)
#pragma comment(lib, &quot;highguid.lib&quot;)

int g_nProgress = 0;
int g_nPause = 0;
int g_nWait = 0;
CvCapture* pCapture = NULL;
int nValue = -1;
char* szWindowName = &quot;myplayer&quot;;

void switchCallback4(int nPos)
{
	double fPos = 0.0;
	fPos = (double)nPos / (double)10;
	if (pCapture)
	{
		cvSetCaptureProperty(pCapture, CV_CAP_PROP_POS_AVI_RATIO, fPos);
	}
}

void myPlayer()
{
	IplImage* pImage = NULL;

	do
	{
		if (g_nPause == 0)
		{
			break;
		}
		pImage = cvQueryFrame(pCapture);
		if (pImage)
		{
			cvShowImage(szWindowName, pImage);
		}
		nValue = cvWaitKey(50);
	}
	while (nValue == -1);
}

void switchCallback5(int nPos)
{
	if (nPos == 1)
	{
		myPlayer();
	}
}

void exercise4()
{

	pCapture = cvCreateFileCapture(&quot;mytest.avi&quot;);
	if (pCapture)
	{
		cvNamedWindow(szWindowName);
		cvCreateTrackbar(&quot;progress&quot;, szWindowName, &amp;g_nProgress, 10, switchCallback4);
		cvCreateTrackbar(&quot;pause&quot;, szWindowName, &amp;g_nPause, 1, switchCallback5);		
	
		do
		{
			if (cvWaitKey(0) == 27)
			{
				break;
			}
		}
		while(true);

		cvReleaseCapture(&amp;pCapture);
		cvDestroyWindow(szWindowName);
	}	
}

int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
	exercise4();
	return 0;
}</pre>
<pre><b><font color="#FF0000" size="6"><a name="chapter5-1"></a>chapter 5 exercises:</font></b></pre>
<pre>#include &lt;cv.h&gt;
#include &lt;highgui.h&gt;
#include &lt;stdio.h&gt;
#include &lt;math.h&gt;
#include &lt;string.h&gt;
#include &lt;windows.h&gt;
#include &lt;tchar.h&gt;
#include &lt;time.h&gt;

#pragma comment(lib, &quot;cvd.lib&quot;)
#pragma comment(lib, &quot;cxcored.lib&quot;)
#pragma comment(lib, &quot;highguid.lib&quot;)

void exercise1()
{
	char* szWindowName[] = {&quot;loadImage&quot;, &quot;simpleImage&quot;, &quot;simpleNoScale&quot;, &quot;NoMedian&quot;, &quot;Guassian&quot;, &quot;bilateral&quot;};
	IplImage* pImage = NULL, *pSimpleImg = NULL, *pNoScaleImg = NULL, *pMedianImg = NULL, *pGaussianImg = NULL, *pBilateralImg = NULL;
	int i = 0;
	
	for (i = 0; i &lt; sizeof(szWindowName)/sizeof(char*); i ++)
	{
		cvNamedWindow(szWindowName[i]);
	}

	pImage = cvLoadImage(&quot;mytest.jpg&quot;);
	if (pImage)
	{
		pSimpleImg = cvCreateImage(cvSize(pImage-&gt;width, pImage-&gt;height), pImage-&gt;depth, pImage-&gt;nChannels);
		pNoScaleImg = cvCreateImage(cvSize(pImage-&gt;width, pImage-&gt;height), IPL_DEPTH_16S, pImage-&gt;nChannels);
		pMedianImg = cvCreateImage(cvSize(pImage-&gt;width, pImage-&gt;height), pImage-&gt;depth, pImage-&gt;nChannels);
		pGaussianImg = cvCreateImage(cvSize(pImage-&gt;width, pImage-&gt;height), pImage-&gt;depth, pImage-&gt;nChannels);
		pBilateralImg = cvCreateImage(cvSize(pImage-&gt;width, pImage-&gt;height), pImage-&gt;depth, pImage-&gt;nChannels);


		cvSmooth(pImage, pSimpleImg, CV_BLUR, 5,5, 5,5);
		cvSmooth(pImage, pNoScaleImg, CV_BLUR_NO_SCALE,11,11, 32,0.5);
		cvSmooth(pImage, pMedianImg, CV_MEDIAN,5,5, 5,5);
		cvSmooth(pImage, pGaussianImg, CV_GAUSSIAN,5,5, 5,5);
		cvSmooth(pImage, pBilateralImg, CV_BILATERAL, 3,3,0.01, 0.003);


		cvShowImage(szWindowName[0], pImage);
		cvShowImage(szWindowName[1], pSimpleImg);
		cvShowImage(szWindowName[2], pNoScaleImg);
		cvShowImage(szWindowName[3], pMedianImg);
		cvShowImage(szWindowName[4], pGaussianImg);
		cvShowImage(szWindowName[5], pBilateralImg);
		cvWaitKey(0);

		cvReleaseImage(&amp;pImage);
		cvReleaseImage(&amp;pSimpleImg);
		cvReleaseImage(&amp;pNoScaleImg);
		cvReleaseImage(&amp;pMedianImg);
		cvReleaseImage(&amp;pGaussianImg);
		cvReleaseImage(&amp;pBilateralImg);
	}
	cvDestroyAllWindows();
}


void exercise2()
{
	char* szWindowName[] = {&quot;original&quot;, &quot;3x3&quot;, &quot;5x5&quot;, &quot;5x5 second&quot;, &quot;9x9&quot;, &quot;11x11&quot;};
	IplImage* pImage = NULL, *pImage3 = NULL, *pImage5 = NULL, *pImage5_2 = NULL, *pImage7 = NULL, *pImage9 = NULL, *pImage11 = NULL;
	int i = 0;
	
	for (i = 0; i &lt; sizeof(szWindowName)/sizeof(char*); i ++)
	{
		cvNamedWindow(szWindowName[i]);
	}

	pImage = cvLoadImage(&quot;mytest.jpg&quot;);
	if (pImage)
	{
		pImage3 = cvCreateImage(cvSize(pImage-&gt;width, pImage-&gt;height), pImage-&gt;depth, pImage-&gt;nChannels);
		pImage5 = cvCreateImage(cvSize(pImage-&gt;width, pImage-&gt;height), pImage-&gt;depth, pImage-&gt;nChannels);
		pImage5_2 = cvCreateImage(cvSize(pImage-&gt;width, pImage-&gt;height), pImage-&gt;depth, pImage-&gt;nChannels);
		pImage7 = cvCreateImage(cvSize(pImage-&gt;width, pImage-&gt;height), pImage-&gt;depth, pImage-&gt;nChannels);
		pImage9 = cvCreateImage(cvSize(pImage-&gt;width, pImage-&gt;height), pImage-&gt;depth, pImage-&gt;nChannels);
		pImage11 = cvCreateImage(cvSize(pImage-&gt;width, pImage-&gt;height), pImage-&gt;depth, pImage-&gt;nChannels);

		cvSmooth(pImage, pImage3, CV_GAUSSIAN, 3,3);
		cvSmooth(pImage, pImage5, CV_GAUSSIAN, 5,5);
		cvSmooth(pImage5, pImage5_2, CV_GAUSSIAN, 5,5);
		cvSmooth(pImage, pImage7, CV_GAUSSIAN,7,7);
		cvSmooth(pImage, pImage9, CV_GAUSSIAN,9,9);
		cvSmooth(pImage, pImage11, CV_GAUSSIAN,11,11);


		cvShowImage(szWindowName[0], pImage);
		cvShowImage(szWindowName[1], pImage3);
		cvShowImage(szWindowName[2], pImage5);
		cvShowImage(szWindowName[3], pImage5_2);
		cvShowImage(szWindowName[4], pImage7);
		cvShowImage(szWindowName[5], pImage9);
		cvShowImage(szWindowName[6], pImage11);
		cvWaitKey(0);

		cvReleaseImage(&amp;pImage);
		cvReleaseImage(&amp;pImage3);
		cvReleaseImage(&amp;pImage5);
		cvReleaseImage(&amp;pImage5_2);
		cvReleaseImage(&amp;pImage7);
		cvReleaseImage(&amp;pImage9);
		cvReleaseImage(&amp;pImage11);
	}
	cvDestroyAllWindows();
}


void exercise3()
{
	IplImage* pImage = NULL, *pImage5 = NULL, *pImage9 = NULL, *pImage5_second = NULL;
	char* ptr = NULL;
	int i = 0;
	char* szWindowName[] = {&quot;original&quot;, &quot;5x5&quot;, &quot;9x9&quot;, &quot;5x5 second&quot;};

	pImage = cvCreateImage(cvSize(100, 100), 8, 1);
	pImage5 = cvCreateImage(cvSize(100, 100), 8, 1);
	pImage5_second = cvCreateImage(cvSize(100, 100), 8, 1);
	pImage9 = cvCreateImage(cvSize(100, 100), 8, 1);

	cvSetZero(pImage);
	cvSetZero(pImage5);
	cvSetZero(pImage9);

	ptr = pImage-&gt;imageData + pImage-&gt;widthStep * 49 + 50 * pImage-&gt;depth * pImage-&gt;nChannels/8;
	*ptr = 255;

	for (i = 0; i &lt; sizeof(szWindowName)/sizeof(char*); i ++)
	{
		cvNamedWindow(szWindowName[i]);
	}

	cvSmooth(pImage, pImage5, CV_GAUSSIAN, 5,5);
	cvSmooth(pImage5, pImage5_second, CV_GAUSSIAN, 5,5);
	cvSmooth(pImage, pImage9, CV_GAUSSIAN, 9,9);

	//cvSmooth(pImage, pImage5);
	//cvSmooth(pImage, pImage9);
	//cvSmooth(pImage5, pImage5_second);

	cvMoveWindow(szWindowName[1], 100, 200);

	cvShowImage(szWindowName[0], pImage);
	cvShowImage(szWindowName[1], pImage5);
	cvShowImage(szWindowName[2], pImage9);
	cvShowImage(szWindowName[3], pImage5_second);

	cvWaitKey(0);
	cvReleaseImage(&amp;pImage);
	cvReleaseImage(&amp;pImage5);
	cvReleaseImage(&amp;pImage9);
	cvReleaseImage(&amp;pImage5_second);

	cvDestroyAllWindows();
}


void exercise4()
{
	IplImage* pImage[8]= {NULL};
	int i = 0;
	double fSize[3] = {1,4,6};

	char* szWindowName[] = {&quot;original&quot;, &quot;9-9-x&quot;, &quot;0-0-x&quot;, &quot;0-0-1-9&quot;, &quot;0-0-9-1&quot;, &quot;0-0-==&gt;1-9==&gt;9-1&quot;, &quot;0-0-9-9&quot;,&quot;9-9-0-0&quot;};

	
	for (i = 0; i &lt; sizeof(szWindowName)/sizeof(char*); i ++)
	{
		if (i == 0)
		{
			pImage[0] = cvLoadImage(&quot;mytest.jpg&quot;);
		}
		else
		{
			pImage[i] = cvCreateImage(cvSize(pImage[0]-&gt;width, pImage[0]-&gt;height), pImage[0]-&gt;depth, pImage[0]-&gt;nChannels);
		}
		cvNamedWindow(szWindowName[i]);
	}


	for (i = 0; i &lt; 3; i ++)
	{
		cvSmooth(pImage[0], pImage[1], CV_GAUSSIAN, 9,9,fSize[i]);
		cvSmooth(pImage[0], pImage[2], CV_GAUSSIAN, 0,0,fSize[i]);
		cvShowImage(szWindowName[0], pImage[0]);
		cvShowImage(szWindowName[1], pImage[1]);
		cvShowImage(szWindowName[2], pImage[2]);
		cvWaitKey(0);
	}

	// c.
	cvSmooth(pImage[0], pImage[3], CV_GAUSSIAN, 0,0,1,9);
	cvShowImage(szWindowName[3], pImage[3]);

	// d.
	cvSmooth(pImage[0], pImage[4], CV_GAUSSIAN, 0,0,9,1);
	cvShowImage(szWindowName[4], pImage[4]);

	// e
	cvSmooth(pImage[3], pImage[5], CV_GAUSSIAN, 0,0,9,1);
	cvShowImage(szWindowName[5], pImage[5]);

	// f.

	cvSmooth(pImage[3], pImage[6], CV_GAUSSIAN, 0,0,9,9);
	cvShowImage(szWindowName[6], pImage[6]);


	cvSmooth(pImage[3], pImage[7], CV_GAUSSIAN, 9,9,0,0);
	cvShowImage(szWindowName[7], pImage[7]);

	cvShowImage(szWindowName[0], pImage[0]);
	//////////////////////////////////////////////////

	cvWaitKey(0);

	for (i = 0; i &lt; sizeof(szWindowName)/sizeof(char*); i ++)
	{
		cvReleaseImage(&amp;pImage[i]);
	}

	cvDestroyAllWindows();
}


int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
	exercise4();
	return 0;
}</pre>
<pre>　</pre>
<pre>　</pre>
<pre><b><font size="3" color="#FF0000">// these are chapter 5 exercises:</font></b></pre>
<pre>#include &lt;cv.h&gt;
#include &lt;highgui.h&gt;
#include &lt;stdio.h&gt;
#include &lt;math.h&gt;
#include &lt;string.h&gt;
#include &lt;windows.h&gt;
#include &lt;tchar.h&gt;
#include &lt;time.h&gt;

#pragma comment(lib, &quot;cvd.lib&quot;)
#pragma comment(lib, &quot;cxcored.lib&quot;)
#pragma comment(lib, &quot;highguid.lib&quot;)

void exercise5()
{
	CvCapture* pCapture = NULL;
	char* szWinName = &quot;image-diff&quot;;
	IplImage* pSrc = NULL, * pImage1 = NULL, * pImage2 = NULL, *pDst = NULL;


	cvNamedWindow(szWinName);
	pCapture = cvCreateCameraCapture(0);
	if (pCapture)
	{
		pSrc = cvQueryFrame(pCapture);
		pImage1 = cvCreateImage(cvSize(pSrc-&gt;width, pSrc-&gt;height), pSrc-&gt;depth, pSrc-&gt;nChannels);		

		cvCopy(pSrc, pImage1);

		cvShowImage(szWinName, pImage1);
		cvWaitKey(0);
		pSrc = cvQueryFrame(pCapture);
		pImage2 = cvCreateImage(cvSize(pSrc-&gt;width, pSrc-&gt;height), pSrc-&gt;depth, pSrc-&gt;nChannels);
		cvCopy(pSrc, pImage2);

		cvShowImage(szWinName, pImage2);

		cvWaitKey(0);
		pDst = cvCreateImage(cvSize(pImage1-&gt;width, pImage1-&gt;height), pImage1-&gt;depth, pImage1-&gt;nChannels);
		////////////////////////////////////////////////////////////////////////////////////////////////


		cvSub(pImage1, pImage2, pDst);

		cvShowImage(szWinName, pDst);

		cvWaitKey(0);

		cvAnd(pImage1, pImage2, pDst);

		cvShowImage(szWinName, pDst);

		cvWaitKey(0);

		cvReleaseImage(&amp;pDst);
		cvReleaseImage(&amp;pImage1);
		cvReleaseImage(&amp;pImage2);
		cvReleaseCapture(&amp;pCapture);
	}
	cvDestroyWindow(szWinName);
}

void exercise6()
{
	CvCapture* pCapture = NULL;
	char* szWinName = &quot;image-diff&quot;;
	IplImage* pImage = NULL, * pImage1 = NULL, * pImage2 = NULL, *pSrc = NULL, *pErodeImage = NULL, *pDilateImage = NULL;


	cvNamedWindow(szWinName);
	pCapture = cvCreateCameraCapture(0);
	if (pCapture)
	{
		pSrc = cvQueryFrame(pCapture);
		pImage1 = cvCreateImage(cvSize(pSrc-&gt;width, pSrc-&gt;height), pSrc-&gt;depth, pSrc-&gt;nChannels);		

		cvCopy(pSrc, pImage1);

		cvShowImage(szWinName, pImage1);
		cvWaitKey(0);
		pSrc = cvQueryFrame(pCapture);
		pImage2 = cvCreateImage(cvSize(pSrc-&gt;width, pSrc-&gt;height), pSrc-&gt;depth, pSrc-&gt;nChannels);
		cvCopy(pSrc, pImage2);

		cvShowImage(szWinName, pImage2);

		cvWaitKey(0);
		pImage = cvCreateImage(cvSize(pImage1-&gt;width, pImage1-&gt;height), pImage1-&gt;depth, pImage1-&gt;nChannels);
		////////////////////////////////////////////////////////////////////////////////////////////////


		cvSub(pImage1, pImage2, pImage);

		cvShowImage(szWinName, pImage);

		cvWaitKey(0);

		pErodeImage = cvCreateImage(cvSize(pImage1-&gt;width, pImage1-&gt;height), pImage1-&gt;depth, pImage1-&gt;nChannels);
		////////////////////////////////////////////////////////////////////////////////////////////////
		pDilateImage = cvCreateImage(cvSize(pImage1-&gt;width, pImage1-&gt;height), pImage1-&gt;depth, pImage1-&gt;nChannels);
		////////////////////////////////////////////////////////////////////////////////////////////////

		cvErode(pImage, pErodeImage);

		cvShowImage(szWinName, pErodeImage);

		cvWaitKey(0);

		cvDilate(pImage, pDilateImage);

		cvShowImage(szWinName, pDilateImage);

		cvWaitKey(0);


		cvReleaseImage(&amp;pErodeImage);
		cvReleaseImage(&amp;pDilateImage);
		cvReleaseImage(&amp;pImage);
		cvReleaseImage(&amp;pImage1);
		cvReleaseImage(&amp;pImage2);
		cvReleaseCapture(&amp;pCapture);
	}
	cvDestroyWindow(szWinName);
}

void exercise7()
{
	CvCapture* pCapture = NULL;
	char* szWinName = &quot;image-diff&quot;;
	IplImage* pThreshold = NULL, * pImage1 = NULL, * pImage2 = NULL, *pSrc = NULL, *pDiffImage = NULL, *pMorphImg = NULL;

	cvNamedWindow(szWinName);
	pCapture = cvCreateCameraCapture(0);
	if (pCapture)
	{
		pSrc = cvQueryFrame(pCapture);
		pImage1 = cvCreateImage(cvSize(pSrc-&gt;width, pSrc-&gt;height), pSrc-&gt;depth, 1);		

		cvConvertImage(pSrc, pImage1);

		cvShowImage(szWinName, pImage1);
		cvWaitKey(0);
		pSrc = cvQueryFrame(pCapture);
		pImage2 = cvCreateImage(cvSize(pSrc-&gt;width, pSrc-&gt;height), pSrc-&gt;depth, 1);
		cvConvertImage(pSrc, pImage2);

		cvShowImage(szWinName, pImage2);

		cvWaitKey(0);

		pDiffImage = cvCreateImage(cvSize(pSrc-&gt;width, pSrc-&gt;height), pSrc-&gt;depth, 1);
		pThreshold = cvCreateImage(cvSize(pSrc-&gt;width, pSrc-&gt;height), pSrc-&gt;depth, 1);
		////////////////////////////////////////////////////////////////////////////////////////////////
		pMorphImg = cvCreateImage(cvSize(pSrc-&gt;width, pSrc-&gt;height), pSrc-&gt;depth, 1);

		cvAbsDiff(pImage1, pImage2, pDiffImage);

		cvShowImage(szWinName, pDiffImage);

		cvWaitKey(0);

		cvThreshold(pDiffImage, pThreshold, 70, 255, CV_THRESH_BINARY);

		cvShowImage(szWinName, pThreshold);

		cvWaitKey(0);

		cvMorphologyEx(pDiffImage, pMorphImg, NULL, NULL, CV_MOP_CLOSE, 1);
		cvShowImage(szWinName, pMorphImg);
		cvWaitKey(0);

		cvReleaseImage(&amp;pDiffImage);
		cvReleaseImage(&amp;pThreshold);
		cvReleaseImage(&amp;pMorphImg);
		cvReleaseImage(&amp;pImage1);
		cvReleaseImage(&amp;pImage2);
		cvReleaseCapture(&amp;pCapture);
	}
	cvDestroyWindow(szWinName);
}

void exercise8()
{
	IplImage* pImage[9]= {NULL};
	int i = 0;
	char* pDst, * pSrc = NULL;
	int r;

	IplImage* pImg = NULL;
	char* szWin = &quot;original &quot;&quot;erode &quot;&quot;dilate &quot; &quot;opening &quot;&quot;closing &quot; &quot;gradient &quot;&quot;tophat &quot;&quot;blackhat &quot; &quot;dummy &quot;;

	char* szWindowName[] = {&quot;original&quot;, &quot;erode&quot;, &quot;dilate&quot;, &quot;opening&quot;, &quot;closing&quot;, &quot;gradient&quot;, &quot;tophat&quot;,&quot;blackhat&quot;, &quot;dummy&quot;};
	int nNumber = sizeof(szWindowName)/sizeof(char*);

	
	for (i = 0; i &lt; nNumber; i ++)
	{
		if (i == 0)
		{
			pImage[0] = cvLoadImage(&quot;mytest.jpg&quot;, CV_LOAD_IMAGE_GRAYSCALE);
		}
		else
		{
			pImage[i] = cvCreateImage(cvSize(pImage[0]-&gt;width, pImage[0]-&gt;height), pImage[0]-&gt;depth, 1);
		}
		cvNamedWindow(szWindowName[i]);
	}
	cvNamedWindow(szWin);
	pImg = cvCreateImage(cvSize(pImage[0]-&gt;width*nNumber, pImage[0]-&gt;height), pImage[0]-&gt;depth, 1);

	//pImage[nNumber] = cvCreateImage(cvSize(pImage[0]-&gt;width, pImage[0]-&gt;height), pImage[0]-&gt;depth, 1);

	cvShowImage(szWindowName[0], pImage[0]);

	cvErode(pImage[0], pImage[1]);
	cvShowImage(szWindowName[1], pImage[1]);

	cvDilate(pImage[0], pImage[2]);
	cvShowImage(szWindowName[2], pImage[2]);

	cvMorphologyEx(pImage[0], pImage[3], NULL, NULL, CV_MOP_OPEN);
	cvShowImage(szWindowName[3], pImage[3]);

	cvMorphologyEx(pImage[0], pImage[4], NULL, NULL,CV_MOP_CLOSE);
	cvShowImage(szWindowName[4], pImage[4]);

	cvMorphologyEx(pImage[0], pImage[5], pImage[8], NULL, CV_MOP_GRADIENT);
	cvShowImage(szWindowName[5], pImage[5]);

	cvMorphologyEx(pImage[0], pImage[6], NULL, NULL, CV_MOP_TOPHAT);
	cvShowImage(szWindowName[6], pImage[6]);

	cvMorphologyEx(pImage[0], pImage[7], NULL, NULL, CV_MOP_BLACKHAT);
	cvShowImage(szWindowName[7], pImage[7]);

	cvShowImage(szWindowName[8], pImage[8]);

	cvWaitKey(0);


	for (i = 0; i &lt; nNumber; i ++)
	{
		pDst = pImg-&gt;imageData + i*pImage[i]-&gt;width;
		pSrc = pImage[i]-&gt;imageData;
		for (r = 0; r &lt; pImage[i]-&gt;height; r ++)
		{
			memcpy(pDst, pSrc, pImage[i]-&gt;width);
			pDst += pImg-&gt;widthStep;
			pSrc += pImage[i]-&gt;widthStep;
		}
		cvReleaseImage(&amp;pImage[i]);
	}

	cvShowImage(szWin, pImg);

	cvWaitKey(0);

	cvReleaseImage(&amp;pImg);
	cvDestroyAllWindows();
}


int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
	exercise8();
	return 0;
}</pre>
<pre><b><font color="#FF0000" size="4">// this is a <a name="chapter5-3"></a>good one which can help you to analysis the image.</font></b></pre>
<pre>#include &lt;cv.h&gt;
#include &lt;highgui.h&gt;
#include &lt;stdio.h&gt;
#include &lt;math.h&gt;
#include &lt;string.h&gt;
#include &lt;windows.h&gt;
#include &lt;tchar.h&gt;
#include &lt;time.h&gt;

#pragma comment(lib, &quot;cvd.lib&quot;)
#pragma comment(lib, &quot;cxcored.lib&quot;)
#pragma comment(lib, &quot;highguid.lib&quot;)


void doExercise9(double fLow, double fHigh)
{
	IplImage* pImage[6]= {NULL};
	int i = 0;
	char* pDst, * pSrc = NULL;
	int r;

	IplImage* pImg = NULL;
	char* szWin = &quot;original &quot; &quot;binary &quot; &quot;binary-inv &quot; &quot;trunc &quot; &quot;tozero-inv &quot; &quot;tozero&quot;;

	char* szWindowName[] = {&quot;original&quot;, &quot;binary&quot;, &quot;binary-inv&quot;, &quot;trunc&quot;, &quot;tozero-inv&quot;, &quot;tozero&quot;};
	int nNumber = sizeof(szWindowName)/sizeof(char*);

	
	for (i = 0; i &lt; nNumber; i ++)
	{
		if (i == 0)
		{
			pImage[0] = cvLoadImage(&quot;mytest.jpg&quot;, CV_LOAD_IMAGE_GRAYSCALE);
		}
		else
		{
			pImage[i] = cvCreateImage(cvSize(pImage[0]-&gt;width, pImage[0]-&gt;height), pImage[0]-&gt;depth, 1);
		}
		cvNamedWindow(szWindowName[i]);
	}
	cvNamedWindow(szWin);

	pImg = cvCreateImage(cvSize(pImage[0]-&gt;width*nNumber, pImage[0]-&gt;height), pImage[0]-&gt;depth, 1);

	//pImage[nNumber] = cvCreateImage(cvSize(pImage[0]-&gt;width, pImage[0]-&gt;height), pImage[0]-&gt;depth, 1);

	cvShowImage(szWindowName[0], pImage[0]);

	cvThreshold(pImage[0], pImage[1], fLow, fHigh, CV_THRESH_BINARY);
	cvShowImage(szWindowName[1], pImage[1]);

	cvThreshold(pImage[0], pImage[2], fLow, fHigh, CV_THRESH_BINARY_INV);
	cvShowImage(szWindowName[2], pImage[2]);

	cvThreshold(pImage[0], pImage[3], fLow, fHigh, CV_THRESH_TRUNC);
	cvShowImage(szWindowName[3], pImage[3]);

	cvThreshold(pImage[0], pImage[4], fLow, fHigh, CV_THRESH_TOZERO_INV);
	cvShowImage(szWindowName[4], pImage[4]);

	cvThreshold(pImage[0], pImage[5], fLow, fHigh, CV_THRESH_TOZERO);
	cvShowImage(szWindowName[5], pImage[5]);

	//cvWaitKey(0);


	for (i = 0; i &lt; nNumber; i ++)
	{
		pDst = pImg-&gt;imageData + i*pImage[i]-&gt;width;
		pSrc = pImage[i]-&gt;imageData;
		for (r = 0; r &lt; pImage[i]-&gt;height; r ++)
		{
			memcpy(pDst, pSrc, pImage[i]-&gt;width);
			pDst += pImg-&gt;widthStep;
			pSrc += pImage[i]-&gt;widthStep;
		}
		cvReleaseImage(&amp;pImage[i]);
	}

	cvShowImage(szWin, pImg);

	cvWaitKey(0);

	cvReleaseImage(&amp;pImg);
	cvDestroyAllWindows();
}

void exercise9()
{
	for (int i = 0; i &lt; 10; i ++)
	{
		doExercise9(i*15, i*20);
	}
}




void getImage(IplImage** ppImage)
{
	CvCapture* pCapture = NULL;
	char* szWinName = &quot;image-diff&quot;;
	IplImage*pImage1 = NULL, *pImage2 = NULL, *pImage3 = NULL, *pSrc = NULL;
	int nChannel = 1;
	cvNamedWindow(szWinName);

	pCapture = cvCreateCameraCapture(0);
	if (pCapture)
	{
		pSrc = cvQueryFrame(pCapture);
		nChannel = 1;
		pImage1 = cvCreateImage(cvSize(pSrc-&gt;width, pSrc-&gt;height), pSrc-&gt;depth, nChannel);		

		cvConvertImage(pSrc, pImage1);

		cvShowImage(szWinName, pImage1);
		cvWaitKey(0);
		pSrc = cvQueryFrame(pCapture);
		pImage2 = cvCreateImage(cvSize(pSrc-&gt;width, pSrc-&gt;height), pSrc-&gt;depth, nChannel);
		cvConvertImage(pSrc, pImage2);

		cvShowImage(szWinName, pImage2);

		cvWaitKey(0);

		pImage3 = cvCreateImage(cvSize(pSrc-&gt;width, pSrc-&gt;height), pSrc-&gt;depth, nChannel);

		*ppImage = cvCreateImage(cvSize(pSrc-&gt;width, pSrc-&gt;height), pSrc-&gt;depth, nChannel);
	
		cvAbsDiff(pImage1, pImage2, pImage3);		
	
		cvThreshold(pImage3, pImage1, 20, 255, CV_THRESH_BINARY);

		cvMorphologyEx(pImage1, pImage3, NULL, NULL, CV_MOP_OPEN);

		//cvShowImage(szWinName, pImage3);

		//cvWaitKey(0);

		cvCopy(pImage3, *ppImage);

		cvShowImage(szWinName, *ppImage);

		cvWaitKey(0);

		cvReleaseImage(&amp;pImage1);
		cvReleaseImage(&amp;pImage2);
		cvReleaseImage(&amp;pImage3);
		cvReleaseCapture(&amp;pCapture);
	}
	cvDestroyWindow(szWinName);
}

bool findImage(IplImage*pImage, char nVal, CvPoint* pPos)
{
	char* ptr = NULL;
	if (pImage-&gt;nChannels == 1)
	{
		ptr = pImage-&gt;imageData;
		if (ptr != NULL)
		{
			for (int r = 0; r &lt; pImage-&gt;height; r ++)
			{
				for (int c = 0;  c &lt; pImage-&gt;width; c ++)
				{
					if (ptr[c] == nVal)
					{
						pPos-&gt;x = c;
						pPos-&gt;y = r;
						return true;
					}
					if (ptr[c]&gt;150)
					{
						int x = 0;
					}
				}
				ptr += pImage-&gt;widthStep;
			}
		}
	}
	return false;
}


void exercise10()
{
	IplImage* pImg = NULL;
	//IplImage* pMaskImg = NULL;
	CvPoint oldPoint = {0, 0}, curPoint = {0, 0};
	int nArea = 0, newArea = 0;
	CvConnectedComp comp;
	char* szMyWin = &quot;my win&quot;;

	getImage(&amp;pImg);
	if (pImg != NULL)
	{
		//pMaskImg = cvCreateImage(cvSize(pImg-&gt;width + 2, pImg-&gt;height + 2), pImg-&gt;depth, 1);
		//cvSetZero(pMaskImg);
		do
		{
			if (findImage(pImg, 255, &amp;curPoint))
			{				
				cvFloodFill(pImg, curPoint, cvScalarAll(100), cvScalarAll(0), cvScalarAll(0), &amp;comp, 8|CV_FLOODFILL_FIXED_RANGE);
				if (comp.area &lt; nArea)
				{
					cvFloodFill(pImg, curPoint, cvScalarAll(0), cvScalarAll(0), cvScalarAll(0), &amp;comp, 8|CV_FLOODFILL_FIXED_RANGE);
				}
				else
				{
					newArea = comp.area;
					if (nArea &gt; 0)
					{
						cvFloodFill(pImg, oldPoint, cvScalarAll(0), cvScalarAll(0), cvScalarAll(0), &amp;comp, 8|CV_FLOODFILL_FIXED_RANGE);
					}
					memcpy(&amp;oldPoint, &amp;curPoint, sizeof(CvPoint));
					nArea = newArea;					
				}
			}
			else
			{
				if (nArea &gt; 0)
				{
					cvFloodFill(pImg, oldPoint, cvScalarAll(255), cvScalarAll(0), cvScalarAll(0), &amp;comp, 8|CV_FLOODFILL_FIXED_RANGE);
				}
				break;
			}

		}
		while (true);
		cvNamedWindow(szMyWin);
		cvShowImage(szMyWin, pImg);
		cvWaitKey(0);

		cvReleaseImage(&amp;pImg);
		cvDestroyWindow(szMyWin);
	}

}


int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
	exercise10();
	return 0;
}</pre>
<pre>　</pre>
<pre><b><font color="#FF0000" size="4"><a name="chapter5-4">// so, this some exercises about cvPyrSegmentation/cvfloodfill etc.</a></font></b></pre>
<pre>#include &lt;cv.h&gt;
#include &lt;highgui.h&gt;
#include &lt;stdio.h&gt;
#include &lt;math.h&gt;
#include &lt;string.h&gt;
#include &lt;windows.h&gt;
#include &lt;tchar.h&gt;
#include &lt;time.h&gt;

#pragma comment(lib, &quot;cvd.lib&quot;)
#pragma comment(lib, &quot;cxcored.lib&quot;)
#pragma comment(lib, &quot;highguid.lib&quot;)


void exercise11()
{
	char* szWinName[] = {&quot;original&quot;, &quot;top-hat&quot;, &quot;target&quot;, &quot;result&quot;, &quot;mask&quot;};
	IplImage* pImage[5]={NULL};
	int i = 0;

	for (i = 0; i &lt; sizeof(szWinName)/sizeof(char*); i ++)
	{
		cvNamedWindow(szWinName[i]);
	}

	if ((pImage[0] = cvLoadImage(&quot;myMask.jpg&quot;, CV_LOAD_IMAGE_GRAYSCALE))!= NULL)
	{
		if ((pImage[1] = cvCreateImage(cvSize(pImage[0]-&gt;width, pImage[0]-&gt;height), pImage[0]-&gt;depth, 1))!= NULL)
		{
			cvMorphologyEx(pImage[0], pImage[1], NULL, NULL, CV_MOP_OPEN);
			cvThreshold(pImage[1], pImage[1], 70, 255, CV_THRESH_BINARY);
			if ((pImage[2] = cvLoadImage(&quot;mytarget.jpg&quot;))!= NULL)
			{
				//cvResize(pImage[1], pImage[2]);
				pImage[4] = cvCreateImage(cvSize(pImage[2]-&gt;width, pImage[2]-&gt;height), pImage[2]-&gt;depth, 1);
				cvResize(pImage[1], pImage[4]);
				//cvCopy(pImage[1], pImage[4]);

				//if (pImage[2]-&gt;width &gt;= pImage[3]-&gt;width &amp;&amp; pImage[2]-&gt;height &gt;= pImage[3]-&gt;height)
				{
					//cvSetImageROI(pImage[1], cvRect(0, 0, pImage[2]-&gt;width, pImage[2]-&gt;height));
					pImage[3] = cvCreateImage(cvSize(pImage[2]-&gt;width, pImage[2]-&gt;height), pImage[2]-&gt;depth, pImage[2]-&gt;nChannels);
					cvSetZero(pImage[3]);
					cvCopy(pImage[2], pImage[3], pImage[4]);
					for (i = 0; i &lt; sizeof(szWinName)/sizeof(char*); i ++)
					{
						cvShowImage(szWinName[i], pImage[i]);
					}
					cvWaitKey(0);
					for (i = 0; i &lt; sizeof(szWinName)/sizeof(char*); i ++)
					{
						cvReleaseImage(&amp;pImage[i]);
						cvDestroyWindow(szWinName[i]);
					}
				}

			}
		}
	}
}


void exercise12()
{
	char* szWinName[] = {&quot;original&quot;, &quot;nearest neighbour&quot;, &quot;bilinear&quot;, &quot;pixel area resampling&quot;, &quot;bicubic interpolation&quot;, &quot;nearest neighbour, bilinear, pixel area resampling,  bicubic interpolation&quot;};
	IplImage* pImage[6]={NULL};
	int newWidth, newHeight, i, nNumber = sizeof(szWinName)/sizeof(char*), nPixelSize = 0;

	char* pDst = NULL, *pSrc = NULL;
	srand(time(0));
	for ( i = 0; i &lt; nNumber; i ++)
	{
		if (i == 0)
		{
			if ((pImage[0] = cvLoadImage(&quot;mytest.jpg&quot;))!= NULL)
			{
				newWidth = rand()% 500 + 100;
				newHeight = rand()% 500 + 100;
			}
			else
			{
				return;
			}
		}
		else
		{
			if (i == nNumber - 1)
			{
				pImage[i] = cvCreateImage(cvSize(newWidth*(nNumber - 2), newHeight), pImage[0]-&gt;depth, pImage[0]-&gt;nChannels);
			}
			else
			{
				pImage[i] = cvCreateImage(cvSize(newWidth, newHeight), pImage[0]-&gt;depth, pImage[0]-&gt;nChannels);
				
			}
		}
		cvNamedWindow(szWinName[i]);
	}

	cvResize(pImage[0], pImage[1], CV_INTER_NN);
	cvResize(pImage[0], pImage[2], CV_INTER_LINEAR);
	cvResize(pImage[0], pImage[3], CV_INTER_AREA);
	cvResize(pImage[0], pImage[4], CV_INTER_CUBIC);

	
	nPixelSize = pImage[0]-&gt;depth*pImage[0]-&gt;nChannels * newWidth / 8;
	for (i = 0; i &lt; nNumber; i ++)
	{
		if (i != 0 &amp;&amp; i != nNumber - 1)
		{			
			pSrc = pImage[i]-&gt;imageData;
			pDst = pImage[nNumber - 1]-&gt;imageData + (i-1)*nPixelSize;
			for (int r = 0; r &lt; newHeight; r ++)
			{
				memcpy(pDst, pSrc, nPixelSize);
				pDst += pImage[nNumber-1]-&gt;widthStep;
				pSrc += pImage[i]-&gt;widthStep;
			}
		}
		cvShowImage(szWinName[i], pImage[i]);
	}

	cvWaitKey(0);

	for (i = 0; i &lt; nNumber; i ++)
	{
		cvReleaseImage(&amp;pImage[i]);
		cvDestroyWindow(szWinName[i]);
	}
}


void exercise13()
{
	char* szWinName[] = {&quot;low variance&quot;, &quot;bilateral filter&quot;};
	IplImage* pImage = NULL, *pFilterImg = NULL;
	int nWidth = 400, nHeight = 400;
	char* ptr = NULL;
	int nOffset = 40, i;
	CvPoint ptStart, ptEnd;
	pImage = cvCreateImage(cvSize(nWidth + 1, nHeight + 1), 8, 1);

	ptr = pImage-&gt;imageData; 
	for (int r = 0; r &lt; nHeight; r ++)
	{
		for (int c =0; c &lt; nWidth; c ++)
		{
			ptr[c] = rand()%4 + 100;
		}
		ptr += pImage-&gt;widthStep;
	}
	for (i = 0; i &lt;= 10; i ++)
	{
		ptStart = cvPoint(i*40, 0);
		ptEnd = cvPoint(nWidth - i*40, nHeight);
		cvLine(pImage, ptStart, ptEnd, cvScalarAll(255));

		ptStart = cvPoint(0, i*40);
		ptEnd = cvPoint(nWidth, nHeight - i*40);
		cvLine(pImage, ptStart, ptEnd, cvScalarAll(255));
	}
	cvNamedWindow(szWinName[0]);
	cvShowImage(szWinName[0], pImage);
	pFilterImg = cvCreateImage(cvSize(pImage-&gt;width, pImage-&gt;height), 8, 1);
	cvSmooth(pImage, pFilterImg, CV_BILATERAL, 5,5, 58, 20);
	cvNamedWindow(szWinName[1]);
	cvShowImage(szWinName[1], pFilterImg);

	cvWaitKey(0);

	cvReleaseImage(&amp;pImage);
	cvReleaseImage(&amp;pFilterImg);
	cvDestroyAllWindows();

}

struct MyStruct
{
	CvSeq* pSeq;
	IplImage* pImage;
	IplImage* pRectImage;
};


bool findImage(IplImage* pImage, CvRect rect, CvScalar color, CvPoint* pPoint)
{
	unsigned char* ptrRow = NULL, * ptr = NULL;
	int nPixelStep = 0;
	nPixelStep = pImage-&gt;depth * pImage-&gt;nChannels / 8;
	ptrRow = (unsigned char*)(pImage-&gt;imageData + rect.x * nPixelStep + rect.y * pImage-&gt;widthStep);
	for (int r = 0; r &lt; rect.height; r ++)
	{
		ptr = ptrRow;
		for (int c = 0; c &lt; rect.width; c ++)
		{
			bool bFind = true;
			for (int i = 0; i &lt; nPixelStep; i ++)
			{
				if (ptr[i] != color.val[i])
				{
					bFind = false;
					break;
				}
			}
			if (bFind)
			{
				pPoint-&gt;x = rect.x + c;
				pPoint-&gt;y = rect.y + r;
				return true;
			}
			ptr += nPixelStep;
		}
		ptrRow += pImage-&gt;widthStep;
	}

	return false;
}
char* szMaskWin = &quot;mask window&quot;;

void myMouseCallback(int event, int x, int y, int flags, void* pUser)
{
	MyStruct* pStruct = (MyStruct*)pUser;
	CvSeq* pSeq = NULL;
	IplImage* pImage = NULL;
	CvConnectedComp* pComp = NULL;
	CvRect* pRect = NULL;
	char buffer[128];
	CvFont myFont = cvFont(1,1);
	unsigned char* ptr = NULL;
	int nPixelSize,  i;
	CvScalar color = {0};
	CvPoint point;


	if (pStruct == NULL)
	{
		return;
	}
	switch (event)
	{
	case CV_EVENT_LBUTTONDOWN:
		pSeq = pStruct-&gt;pSeq;
		pImage = pStruct-&gt;pImage;
		nPixelSize = pImage-&gt;depth * pImage-&gt;nChannels / 8;
		ptr = (unsigned char*)(pImage-&gt;imageData + pImage-&gt;widthStep * y + pImage-&gt;depth * pImage-&gt;nChannels / 8 * x); 
		for (i = 0; i &lt; pImage-&gt;nChannels; i ++)
		{
			color.val[i] = ptr[i];
		}

		for (i = 0; i &lt; pSeq-&gt;total; i ++)
		{
			pComp = (CvConnectedComp*)cvGetSeqElem(pSeq, i);
			pRect = &amp;pComp-&gt;rect;

			//if (x &gt; pRect-&gt;x &amp;&amp; x &lt; pRect-&gt;x + pRect-&gt;width &amp;&amp; y &gt; pRect-&gt;y &amp;&amp; y &lt; pRect-&gt;y + pRect-&gt;height)
			if (memcmp(&amp;color, &amp;pComp-&gt;value, sizeof(CvScalar))==0)
			{
				/*
				cvRectangle(pImage, cvPoint(pRect-&gt;x, pRect-&gt;y), cvPoint(pRect-&gt;x+pRect-&gt;width-1, pRect-&gt;y+pRect-&gt;height-1), pComp-&gt;value);

				sprintf(buffer, &quot;[area=%.00f][x=%d,y=%d,w=%d,h=%d]&quot;, pComp-&gt;area, pRect-&gt;x, pRect-&gt;y, pRect-&gt;width, pRect-&gt;height);
				//cvPutText(pImage, buffer, cvPoint(pRect-&gt;x , pRect-&gt;y + pRect-&gt;height/2), &amp;myFont, cvScalarAll(0));
				cvPutText(pImage, buffer, cvPoint(pRect-&gt;x, pRect-&gt;y+20), &amp;myFont, cvScalarAll(0));
				
				if (pStruct-&gt;pRectImage)
				{
					cvReleaseImage(&amp;pStruct-&gt;pRectImage);
				}
				pStruct-&gt;pRectImage = cvCreateImage(cvSize(pRect-&gt;width + 1, pRect-&gt;height + 1), pImage-&gt;depth, 1);
				*/
				cvSetZero(pStruct-&gt;pRectImage);
				if (findImage(pImage, *pRect, (CvScalar)pComp-&gt;value, &amp;point))
				{
					//cvSetImageROI(pImage, *pRect);
					cvFloodFill(pImage, point, cvScalarAll(0), cvScalarAll(0), cvScalarAll(0), NULL, 8|CV_FLOODFILL_FIXED_RANGE|(100&lt;&lt;8), pStruct-&gt;pRectImage);
					//cvResetImageROI(pImage);
					cvShowImage(szMaskWin, pStruct-&gt;pRectImage);
				}
				return;
			}
		}
		// here is problem
		sprintf(buffer, &quot;no finding&quot;);
		cvPutText(pImage, buffer, cvPoint(10,10), &amp;myFont, cvScalarAll(0));
		break;
	}
	
}

void exercise14()
{
	MyStruct myStruct = {0};
	char* szWinName[] = {&quot;original&quot;, &quot;input&quot;, &quot;segment&quot;};
	CvMemStorage* pMem = NULL;
	CvSeq* pSeq = NULL;
	IplImage* pImage[sizeof(szWinName)/sizeof(char*)] = {NULL};
	int nSize = 512, i;
	pImage[0] = cvLoadImage(&quot;mytest.jpg&quot;);

	pImage[1] = cvCreateImage(cvSize(nSize, nSize), pImage[0]-&gt;depth, pImage[0]-&gt;nChannels);
	pImage[2] = cvCreateImage(cvSize(nSize, nSize), pImage[0]-&gt;depth, pImage[0]-&gt;nChannels);
	cvResize(pImage[0], pImage[1]);
	pMem = cvCreateMemStorage();

	cvPyrSegmentation(pImage[1], pImage[2], pMem, &amp;pSeq, 5, 100, 30);
	for (i = 0; i &lt; sizeof(szWinName)/sizeof(char*); i ++)
	{
		cvNamedWindow(szWinName[i]);
		cvShowImage(szWinName[i], pImage[i]);

	}
	

	cvWaitKey(0);
	myStruct.pImage = pImage[2];
	myStruct.pSeq = pSeq;
	cvSetMouseCallback(szWinName[2], myMouseCallback, &amp;myStruct);
	myStruct.pRectImage = cvCreateImage(cvSize(myStruct.pImage-&gt;width + 2, myStruct.pImage-&gt;height + 2), myStruct.pImage-&gt;depth, 1);
	cvNamedWindow(szMaskWin);

	do
	{
		if (cvWaitKey(500) == 27)
		{
			break;
		}
		cvShowImage(szWinName[2], pImage[2]);
	}
	while (true);



	for (i = 0; i &lt; sizeof(szWinName)/sizeof(char*); i ++)
	{
		cvDestroyWindow(szWinName[i]);
		cvReleaseImage(&amp;pImage[i]);
	}
	cvReleaseMemStorage(&amp;pMem);
}





int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	
	exercise14();
	return 0;
}</pre>
<pre>　</pre>
<pre><b><font color="#FF0000" size="4"><a name="laplace">// I don't understand why Laplace transform looks like this. Probably I made some mistake as I cannot even show the result directly.</a></font></b></pre>
<pre>#include &lt;cv.h&gt;
#include &lt;highgui.h&gt;
#include &lt;stdio.h&gt;
#include &lt;math.h&gt;
#include &lt;string.h&gt;
#include &lt;windows.h&gt;
#include &lt;tchar.h&gt;
#include &lt;time.h&gt;

#pragma comment(lib, &quot;cvd.lib&quot;)
#pragma comment(lib, &quot;cxcored.lib&quot;)
#pragma comment(lib, &quot;highguid.lib&quot;)

void exercise17()
{
	char data1[3][4] =
	{
		{1,-2,1, 0},
		{2,-4,2,0},
		{1,-2,1,0},
	};
	char data2[3][3] =
	{
		{1,-2,1},
		{2,-4,2},
		{1,-2,1},
	};
	CvMat* pMat = NULL;
	unsigned char* ptr = NULL;
	CvMat myMat;
	IplImage* pInput = NULL, * pOutput = NULL, *pImage = NULL;
	cvInitMatHeader(&amp;myMat, 3, 3, CV_8SC1, data2, 3);
	pMat = cvCreateMat(3, 3, CV_8SC1);
	
	ptr = pMat-&gt;data.ptr;

	for (int r = 0; r &lt; 3; r ++)
	{
		for (int c = 0; c &lt; 3; c ++)
		{
			ptr[c] = data2[r][c];
		}
		ptr += pMat-&gt;step;
	}

	pInput = cvLoadImage(&quot;mytest.jpg&quot;);
	pOutput = cvCreateImage(cvSize(pInput-&gt;width, pInput-&gt;height), pInput-&gt;depth, pInput-&gt;nChannels);
	pImage = cvCreateImage(cvSize(pInput-&gt;width, pInput-&gt;height), pInput-&gt;depth, pInput-&gt;nChannels);

	cvFilter2D(pInput, pOutput, &amp;myMat);
	cvFilter2D(pInput, pImage, pMat);

	cvNamedWindow(&quot;input&quot;);
	cvNamedWindow(&quot;output&quot;);
	cvNamedWindow(&quot;convolve&quot;);
	cvShowImage(&quot;input&quot;, pInput);
	cvShowImage(&quot;output&quot;, pOutput);
	cvShowImage(&quot;convolve&quot;, pImage);
	cvWaitKey(0);

	cvReleaseImage(&amp;pInput);
	cvReleaseImage(&amp;pOutput);
	cvReleaseImage(&amp;pImage);
	cvDestroyAllWindows();
}


void exercise19();

void exercise18()
{
	char* pSrc = NULL, *pDst = NULL;
	char* szWinName[] = {&quot;input&quot;,  &quot;output10&quot;,&quot;output20&quot;, &quot;output01&quot;,&quot;output11&quot;, &quot;output21&quot;, &quot;output02&quot;,&quot;output12&quot;, &quot;output22&quot;, &quot;paranoma&quot;};
	IplImage* pImage[sizeof(szWinName)/sizeof(char*)] = {NULL};
	int i = 0, x, y, index = 0, nNumber = sizeof(szWinName)/sizeof(char*);
	for (i = 0; i &lt; nNumber; i ++)
	{
		if (i ==0 )
		{
			pImage[i] = cvLoadImage(&quot;mytest.jpg&quot;, CV_LOAD_IMAGE_GRAYSCALE);			
		}
		else
		{
			if (i == nNumber-1)
			{
				pImage[i] = cvCreateImage(cvSize(pImage[0]-&gt;width*(nNumber-1), pImage[0]-&gt;height), pImage[0]-&gt;depth, pImage[0]-&gt;nChannels);
			}
			else
			{
				pImage[i] = cvCreateImage(cvSize(pImage[0]-&gt;width, pImage[0]-&gt;height), pImage[0]-&gt;depth, pImage[0]-&gt;nChannels);
			}
		}
		cvNamedWindow(szWinName[i]);
	}
	for (y = 0; y &lt; 3; y ++)
	{
		for (x = 0; x &lt; 3; x ++)
		{
			index = y*3+x;
			if (x!=0 || y != 0)
			{
				cvSobel(pImage[0], pImage[index], x, y);
			}
			cvShowImage(szWinName[index], pImage[index]);
		}
	}
	for (i = 0; i &lt; nNumber - 1; i ++)
	{
		pSrc = pImage[i]-&gt;imageData;
		pDst = pImage[nNumber - 1]-&gt;imageData + pImage[i]-&gt;width*i;
		for (y = 0; y &lt; pImage[i]-&gt;height; y ++)
		{
			memcpy(pDst, pSrc, pImage[i]-&gt;width);
			pSrc += pImage[i]-&gt;widthStep;
			pDst += pImage[nNumber - 1]-&gt;widthStep;
		}
	}
	cvShowImage(szWinName[nNumber - 1], pImage[nNumber - 1]);

	//exercise19();

	cvWaitKey(0);
	
	for (i = 0; i &lt; nNumber; i ++)
	{
		cvReleaseImage(&amp;pImage[i]);
		cvDestroyWindow(szWinName[i]);
	}
}

void exercise19()
{
	char*szWinName[] = {&quot;original&quot;, &quot;laplace&quot;};
	IplImage* pImage[3] = { NULL };
	char* pDst = NULL, *pDstRow, *pSrcRow;
	char* pSrc = NULL;
	short* pSrcShort = NULL;
	int i;
	int nSrcPix, nDstPix;
	pImage[0] = cvLoadImage(&quot;mytest.jpg&quot;, CV_LOAD_IMAGE_GRAYSCALE);
	if (pImage[0]-&gt;depth == 8)
	{
		pImage[1] = cvCreateImage(cvSize(pImage[0]-&gt;width, pImage[0]-&gt;height), IPL_DEPTH_16S, pImage[0]-&gt;nChannels);
		pImage[2] = cvCreateImage(cvSize(pImage[0]-&gt;width, pImage[0]-&gt;height), pImage[0]-&gt;depth, pImage[0]-&gt;nChannels);
	
		cvLaplace(pImage[0], pImage[1]);
		//cvConvertImage(pImage[1], pImage[2]);
		
		pSrcRow = pImage[1]-&gt;imageData;
		pDstRow = pImage[2]-&gt;imageData;
		nSrcPix = (pImage[1]-&gt;depth^IPL_DEPTH_SIGN)*pImage[1]-&gt;nChannels/8;
		nDstPix = pImage[2]-&gt;depth*pImage[2]-&gt;nChannels/8;
		for (int r = 0; r &lt; pImage[1]-&gt;height; r ++)
		{
			pDst = pDstRow;
			pSrc = pSrcRow;
			for (int c = 0; c &lt; pImage[1]-&gt;width; c ++)
			{
				pSrcShort = (short*)pSrc;
				for (int n = 0; n &lt; pImage[0]-&gt;nChannels; n ++)
				{					
					pDst[n] = pSrcShort[n];
				}
				pDst += nDstPix;
				pSrc += nSrcPix;			
			}
			pDstRow += pImage[2]-&gt;widthStep;
			pSrcRow += pImage[1]-&gt;widthStep;
		}
		

		for (i = 0; i &lt; 2; i ++)
		{
			cvNamedWindow(szWinName[i]);
			
		}
		cvShowImage(szWinName[0], pImage[0]);
		cvShowImage(szWinName[1], pImage[2]);
	
		cvWaitKey(0);

		for (i = 0; i &lt; 2; i ++)
		{
			cvReleaseImage(&amp;pImage[i]);
			cvDestroyWindow(szWinName[i]);
		}
	
	}
}


int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{	
	exercise19();
	return 0;
}</pre>
<pre><b><font size="4" color="#FF0000"><a name="chapter6-1">//the exercise of chapter 6</a></font></b></pre>
<pre>#include &lt;cv.h&gt;
#include &lt;highgui.h&gt;
#include &lt;stdio.h&gt;
#include &lt;math.h&gt;
#include &lt;string.h&gt;
#include &lt;windows.h&gt;
#include &lt;tchar.h&gt;
#include &lt;time.h&gt;

#pragma comment(lib, &quot;cvd.lib&quot;)
#pragma comment(lib, &quot;cxcored.lib&quot;)
#pragma comment(lib, &quot;highguid.lib&quot;)


void exercise20(bool bFirst)
{
	char* szWinName[] = {&quot;original&quot;, &quot;3x3&quot;, &quot;5x5&quot;, &quot;7x7&quot;};//, &quot;9x9&quot;, &quot;11x11&quot;, &quot;13x13&quot;};
	IplImage* pImage[sizeof(szWinName)/sizeof(char*)] = {NULL};
	int nWidth = 600, nHeight = 600;
	int n = 0, i = 0,  nNumber = sizeof(szWinName)/sizeof(char*);
	for (n = 0; n &lt; nNumber; n ++)
	{
		pImage[n] = cvCreateImage(cvSize(nWidth, nHeight), 8, 3);
		if (n== 0)
		{			
			cvZero(pImage[n]);

			for (i = 0; i &lt;= 12; i ++)
			{
				cvCircle(pImage[n], cvPoint(nWidth/2, nHeight/2), i*25, cvScalar(i%3*i*50, (i+1)%3*i*50, (i+2)%3*50));
				cvLine(pImage[n], cvPoint(i*50, 0), cvPoint(nWidth-i*50, nHeight-1), cvScalarAll(255));
				cvLine(pImage[n], cvPoint(0, i*50), cvPoint(nWidth-1, nHeight-i*50), cvScalarAll(255));
			}
		}
		else
		{
			if (true)
			{
				//cvSobel(pImage[0], pImage[n], 1, 0, (n-1)*2+3);
				//cvLaplace(pImage[0], pImage[n], (n-1)*2 + 3);
			}
			else
			{
				//cvSobel(pImage[0], pImage[n], 0, 1, (n-1)*2+3);
			}

		}
		cvNamedWindow(szWinName[n]);
		cvShowImage(szWinName[n], pImage[n]);
	}
	cvWaitKey(0);

	for (n = 0; n &lt; nNumber; n ++)
	{
		cvReleaseImage(&amp;pImage[n]);
		cvDestroyWindow(szWinName[n]);
	}
}

void exercise21()
{
	char* szWinName[] = {&quot;original&quot;, &quot;circles&quot;};
	IplImage* pImage[sizeof(szWinName)/sizeof(char*)] = {NULL};
	CvMemStorage* pMem = NULL;
	int n = 0, i = 0,  nNumber = sizeof(szWinName)/sizeof(char*);
	CvSeq* pSeq = NULL;
	float* pNumber = NULL;

	pMem = cvCreateMemStorage();

	for (i = 0; i &lt; nNumber; i ++)
	{
		switch (i)
		{
		case 0:
			pImage[i] = cvLoadImage(&quot;mytest.jpg&quot;, CV_LOAD_IMAGE_GRAYSCALE);					
			break;
		case 1:	
			pImage[i] = cvCreateImage(cvGetSize(pImage[0]), pImage[0]-&gt;depth, pImage[0]-&gt;nChannels);
			cvZero(pImage[i]);
			cvSmooth(pImage[0], pImage[i], CV_GAUSSIAN, 5, 5);		
			
			
			pSeq = cvHoughCircles(pImage[i], pMem, CV_HOUGH_GRADIENT, 2, pImage[i]-&gt;width/10);
			for (n = 0; n &lt; pSeq-&gt;total; n ++)
			{
				pNumber = (float*)cvGetSeqElem(pSeq, n);
				CvPoint pt = cvPoint(cvRound(pNumber[0]), cvRound(pNumber[1]));
				cvCircle(pImage[i], pt, cvRound(pNumber[2]), CV_RGB(255, 255, 255));
			}
			break;
		}
		cvNamedWindow(szWinName[i]);	
		cvShowImage(szWinName[i], pImage[i]);	
			
	}

	cvWaitKey(0);

	for (i = 0; i &lt; nNumber; i ++)
	{
		cvReleaseImage(&amp;pImage[i]);
		cvDestroyWindow(szWinName[i]);
	}
	cvReleaseMemStorage(&amp;pMem);

}


void exercise22()
{
#define PI 3.1415926
	char* szWinName[] = {&quot;original&quot;, &quot;lines&quot;, &quot;canny&quot;};
	IplImage* pImage[sizeof(szWinName)/sizeof(char*)] = {NULL};
	CvMemStorage* pMem = NULL;
	int n = 0, i = 0,  nNumber = sizeof(szWinName)/sizeof(char*);
	CvSeq* pSeq = NULL;
	CvPoint* ptr = NULL;

	pMem = cvCreateMemStorage();

	for (i = 0; i &lt; nNumber; i ++)
	{
		switch (i)
		{
		case 0:
			pImage[i] = cvLoadImage(&quot;mytest.jpg&quot;, CV_LOAD_IMAGE_GRAYSCALE);					
			break;
		case 1:	
			pImage[i] = cvCreateImage(cvGetSize(pImage[0]), pImage[0]-&gt;depth, pImage[0]-&gt;nChannels);
			cvZero(pImage[i]);
			cvSmooth(pImage[0], pImage[i], CV_GAUSSIAN, 5, 5);				
						 //1, CV_PI/180, 50, 50, 10 );
			pSeq = cvHoughLines2(pImage[i], pMem, CV_HOUGH_PROBABILISTIC, 100, PI/18.0, 250, 350, 0);
			for (n = 0; n &lt; pSeq-&gt;total; n ++)
			{
				ptr = (CvPoint*)cvGetSeqElem(pSeq, n);
				
				cvLine(pImage[i], ptr[0], ptr[1], cvScalarAll(0));
			}
			break;
		case 2:
			pImage[i] = cvCreateImage(cvGetSize(pImage[0]), pImage[0]-&gt;depth, pImage[0]-&gt;nChannels);
			cvZero(pImage[i]);
			cvCanny(pImage[0], pImage[i], 10, 50, 5);

		}
		cvNamedWindow(szWinName[i]);	
		cvShowImage(szWinName[i], pImage[i]);	
			
	}
	

	cvWaitKey(0);

	for (i = 0; i &lt; nNumber; i ++)
	{
		cvReleaseImage(&amp;pImage[i]);
		cvDestroyWindow(szWinName[i]);
	}

}


void exercise23()
{
	float alpha = 0.866f, beta = 0.5f, x = 200.0f, y = 200.0f;
	float pData[6] = {0.866025f, 0.5, 0.0f, -0.5f, 0.866025f, 0.0f};
	char* szWinName[] = {&quot;original&quot;, &quot;warpAffine&quot;};
	IplImage* pImage[sizeof(szWinName)/sizeof(char*)] = {NULL};
	int n = 0, i = 0,  nNumber = sizeof(szWinName)/sizeof(char*);
	CvMat myMat = {0};
	CvMat* pMat = cvCreateMat(2, 3, CV_32FC1);

	//pData[2] = (1-alpha)*x - beta*y;
	//pData[5] = beta*x + (1-alpha)*y;

	cvInitMatHeader(&amp;myMat, 2, 3, CV_32FC1, pData, sizeof(float)*3);
	for (i = 0; i &lt; nNumber; i ++)
	{
		switch (i)
		{
		case 0:
			pImage[i] = cvLoadImage(&quot;mytest.jpg&quot;);					
			break;
		case 1:	
			pImage[i] = cvCreateImage(cvGetSize(pImage[0]), pImage[0]-&gt;depth, pImage[0]-&gt;nChannels);
			cv2DRotationMatrix(cvPoint2D32f(x, y), 60, 1.0, pMat);
			//cvWarpAffine(pImage[0], pImage[1], pMat);
			cvWarpAffine(pImage[0], pImage[1], &amp;myMat);
			break;
		}
		cvNamedWindow(szWinName[i]);	
		cvShowImage(szWinName[i], pImage[i]);	
			
	}
	

	cvWaitKey(0);

	for (i = 0; i &lt; nNumber; i ++)
	{
		cvReleaseImage(&amp;pImage[i]);
		cvDestroyWindow(szWinName[i]);
	}

}

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{	
	exercise23();
	return 0;
}</pre>
<pre>　</pre>
<pre>　</pre>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="logic.htm">      







                       <img src="picture/back.gif" style="border: medium none" alt="back.gif (341 bytes)" width="32" height="35"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   
<a href="index.htm">
<img src="picture/up.gif" style="border: medium none" alt="up.gif (335 bytes)" width="35" height="32"></a>       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<a href="Matrix.htm">
<img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35">            


</a>            


</p>

</body>

</html>