My Vision Tutorials Index
This tutorial assumes the reader:
(1) Has a basic knowledge of Visual C++
(2) Has some familiarity with computer vision concepts
(3) Has read the previous tutorials in this series
The rest of the tutorial is presented as follows:
Important Note!
More information on the topics of these tutorials can be found in this book:Learning OpenCV: Computer Vision with the OpenCV Library
Step 1: Filter Examples(过滤器的例子)
Six different filters applied to an image(六种不同的过滤器在图像中的应用)
This chapter presents the use of several basic image processing filters(本章介绍了使用过滤器处理几个基本的图像). The best way to understand these filters in a general sense is to see them in action(最好的方法在一般意义上理解这些过滤器,是看他们的行动). For more technical details about their function please see the text(有关它们的功能更多的技术细节请参阅文本。). This example will demonstrate blur, gaussian, and median smoothing usingcvSmooth(这个例子将使用cvSmooth展示模糊,高斯,和中值平滑). Smoothing operations are often an important precursor to other processing(平滑操作往往是其他处理重要的先导). ThencvErode, cvDilate and cvFloodFill are demonstrated(然后演示了cvErode,cvDilate和cvFloodFill). All of these can be helpful when segmenting an image, amoung other things(除其他外,所有这些都可以有助于分割图像). Here is the code(以下是代码):
int g_switch_value = 0;
int filterInt = 0;
int lastfilterInt = -1;
void switch_callback( int position )
{
filterInt = position;
}
int _tmain(int argc, _TCHAR* argv[])
{
const char* name = "Filters Window";
IplImage* img = cvLoadImage( "MGC.jpg" );
IplImage* out = cvCreateImage( cvGetSize(img), IPL_DEPTH_8U, 3 );
cvNamedWindow( name, 1 );
cvShowImage(name, out);
// Other Variables
CvPoint seed_point = cvPoint(305,195);
CvScalar color = CV_RGB(250,0,0);
// Create trackbar
cvCreateTrackbar( "FILTER", name, &g_switch_value, 5, switch_callback );
while( 1 ) {
switch( filterInt ){
case 0:
cvSmooth( img, out, CV_BLUR, 7, 7 );
break;
case 1:
cvSmooth( img, out, CV_GAUSSIAN, 7, 7 );
break;
case 2:
cvSmooth( img, out, CV_MEDIAN, 7, 7 );
break;
case 3:
cvErode( img, out, NULL, 1);
break;
case 4:
cvDilate( img, out, NULL, 1);
break;
case 5:
cvFloodFill( out, seed_point, color, cvScalarAll(5.0), cvScalarAll(5.0), NULL, 4, NULL );
break;
}
if(filterInt != lastfilterInt){
cvShowImage(name, out);
lastfilterInt = filterInt;
}
if( cvWaitKey( 15 ) == 27 )
break;
}
cvReleaseImage( &img );
cvReleaseImage( &out );
cvDestroyWindow( name );
return 0;
}
Step 2: Thresholding(阈值)
Thresholded Image(阈值图像)
Another basic filter is thresholding(另一个基本的过滤器是阈值). This code shows how to do a truncation, where values over 100 are discarded(此代码演示了如何做一个截断,有超过100个值将被丢弃). This is of course not the same as a binarization thresholding operation, where everything that is above one value is white and everything below is black(这当然不是作为一个二值化阈值操作,其中一个高于一切价值的是白色,下面是黑色的).cvThreshold is able to take five possible values: CV_THRESH_BINARY, CV_THRESH_BINARY_INV, CV_THRESH_TRUNC, CV_THRESH_TOZERO, CV_THRESH_TOZERO_INV.(cvThreshold能够采取五种可能的值:CV_THRESH_BINARY,CV_THRESH_BINARY_INV,CV_THRESH_TRUNC,CV_THRESH_TOZERO,CV_THRESH_TOZERO_INV。) Binary and binary inverse should be self explanitory, and truncate is shown in this example(这个例子展示二进制和反二进制,explanitory,并截断). To zero is essentially the opposite of truncate (preserving everything below and discarding values above)(正好相反到零就是截断(保存,抛弃以上一切值)).
void sum_rgb( IplImage* src, IplImage* dst ){
// Allocate image planes
IplImage* r = cvCreateImage( cvGetSize(src), IPL_DEPTH_8U, 1 );
IplImage* g = cvCreateImage( cvGetSize(src), IPL_DEPTH_8U, 1 );
IplImage* b = cvCreateImage( cvGetSize(src), IPL_DEPTH_8U, 1 );
// Split image onto the color planes
cvSplit( src, r, g, b, NULL );
IplImage* s = cvCreateImage( cvGetSize(src), IPL_DEPTH_8U, 1 );
// Add equally weighted rgb values
cvAddWeighted( r, 1./3., g, 1./3., 0.0, s );
cvAddWeighted( s, 2./3., b, 1./3., 0.0, s );
// Truncate values over 100
cvThreshold( s, dst, 100, 100, CV_THRESH_TRUNC );
cvReleaseImage( &r );
cvReleaseImage( &g );
cvReleaseImage( &b );
cvReleaseImage( &s );
}
int _tmain(int argc, _TCHAR* argv[])
{
const char* name = "Thresholding";
cvNamedWindow( name, 1 );
IplImage* src = cvLoadImage("MGC.jpg");
IplImage* dst = cvCreateImage( cvGetSize(src), src->depth, 1 );
sum_rgb( src, dst);
cvShowImage( name, dst);
while( 1 ){
if( (cvWaitKey(10)&0x7f) == 27 )
break;
}
cvDestroyWindow( name );
cvReleaseImage( &src );
cvReleaseImage( &dst );
return 0;
}
Final Words(结束语)
This tutorial's objective was to show how to use some image filters functions(本教程的目标是展示如何使用一些图像过滤功能). You should be able to extend the use of these functions for more elaborate filtering operations(你应该能够扩展这些功能,使用更精细的过滤操作).
Click here to email me.
Click here to return to my Tutorials page.