OpenCV Tutorial 5 - Chapter 6

 Author: Noah Kuntz (2009)
Contact: nk752@drexel.edu

Keywords: OpenCV, computer vision, image processing, edge detection, canny, transforms, affine

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: Edge Detection(边缘检测)


Canny Edge Detection(Canny边缘检测)


This chapter presents the use of several image transforms(本章介绍了几种图像转换). The first transform I will cover is Canny Edge Detection(第一个转变,我将会说Canny边缘检测). The book describes Sobel and Laplace operators for gradient detection, for more technical details about their function please see the text(书中介绍了Sobel和Laplace梯度检测,有关的功能和更多的技术细节,请参阅文本). The Canny Edge Detection algorithm takes the derivative of an image to find the gradients, then determines the direction of these gradients (vertical, horizontal, diagonal up, diagonal down)(精明的边缘检测算法以图像的衍生工具找到梯度,则决定了这些梯度(垂直,水平,对角线,对角线向下)的方向). Then if the amplitude of a given gradient is high enough (the high threshold), the algorithm will trace along that gradient in its direction until the amplitude falls below the low threshold, or the gradient changes direction sharply(然后,如果给定的梯度幅值足够高(高阈值),该算法将沿其梯度方向跟踪,直到幅度低于门槛低,或方向梯度变化急剧。). The algorithm will also suppress local non-maximums around the edges(该算法也将抑制周围非最大值的边缘地方).cvCanny implements this algorithm(cvCanny实现了该算法). The inputs are the two images to be used, the thresholds, and the aperature size of the convolution kernal(其输入是两个图像,使用,阈值,以及大小籽粒卷积aperature). For this example I created two sliders that adjust the thresholds, try moving the sliders and see the effect of the thresholds on the resulting edges(对于这个例子,我创建了两个滑块调整阈值,尝试移动滑块,看到由此产生的边缘效应的阈值). This example also makes use ofcvCopyMakeBorder to pad the borders of the image for convolution(这个例子也利用cvCopyMakeBorder用与垫卷积图像的边界). This is not required but can improve results(这不是必须的,但可以改善结果). Here is the code(以下是代码):


 

int high_switch_value = 0;

int highInt = 0;

int low_switch_value = 0;

int lowInt = 0;

 

void switch_callback_h( int position ){

        highInt = position;

}

void switch_callback_l( int position ){

        lowInt = position;

}

 

int _tmain(int argc, _TCHAR* argv[])

{

        const char* name = "Edge Detection Window";

 

        // Kernel size

        int N = 7;

 

        // Set up images

        IplImage* img = cvLoadImage( "MGC.jpg", 0 );

        IplImage* img_b = cvCreateImage( cvSize(img->width+N-1,img->height+N-1), img->depth, img->nChannels );

        IplImage* out = cvCreateImage( cvGetSize(img_b), IPL_DEPTH_8U, img_b->nChannels );

 

        // Add convolution boarders

        CvPoint offset = cvPoint((N-1)/2,(N-1)/2);

        cvCopyMakeBorder(img, img_b, offset, IPL_BORDER_REPLICATE, cvScalarAll(0));

 

        // Make window

        cvNamedWindow( name, 1 );

       

        // Edge Detection Variables

        int aperature_size = N;

        double lowThresh = 20;

        double highThresh = 40;

 

        // Create trackbars

        cvCreateTrackbar( "High", name, &high_switch_value, 4, switch_callback_h );

        cvCreateTrackbar( "Low", name, &low_switch_value, 4, switch_callback_l );

 

        while( 1 ) {

               switch( highInt ){

                       case 0:

                               highThresh = 200;

                               break;

                       case 1:

                               highThresh = 400;

                               break;

                       case 2:

                               highThresh = 600;

                               break;

                       case 3:

                               highThresh = 800;

                               break;

                       case 4:

                               highThresh = 1000;

                               break;

               }

               switch( lowInt ){

                       case 0:

                               lowThresh = 0;

                               break;

                       case 1:

                               lowThresh = 100;

                               break;

                       case 2:

                               lowThresh = 200;

                               break;

                       case 3:

                               lowThresh = 400;

                               break;

                       case 4:

                               lowThresh = 600;

                               break;

               }

 

               // Edge Detection

               cvCanny( img_b, out, lowThresh*N*N, highThresh*N*N, aperature_size );        

               cvShowImage(name, out);

              

               if( cvWaitKey( 15 ) == 27 )

                       break;

        }

 

        // Release

        cvReleaseImage( &img );

        cvReleaseImage( &img_b );

        cvReleaseImage( &out );

        cvDestroyWindow( name );

 

        return 0;

}

 


Step 2: Affine Transforms(仿射变换)


Affine Transform Example(仿射变换的范例)


Another basic transform is an affine transform(另一种基本变换是仿射变换). And affine transform allows the user to warp, stretch, rotate and resize an image(仿射变换允许用户变形,拉伸,旋转和调整大小图像). Essentially the image is multiplied by 2x3 matrix to perform the transformation(本质上,图像是乘以2x3矩阵执行转换). An affine transform produces parallelograms (which includes standard rectangles)(仿射变换产生平行四边形(包括标准矩形)). A more complex transform is an perspective transformation, or "Homography," which uses a 3x3 matrix and turns the image into a trapezoid (which means all affine transforms are also possible)(一个更复杂的变换是透视变换,或“单应”,它将使用到一个3x3矩阵把图像变成一个梯形(这意味着所有的仿射变换也是可能的)). Here I will give an example of an affine transform, read the chapter for more mathematical details and information on how to perform a more advanced perspective transform(在这里,我举一个例子的仿射变换,请阅读更多的数学细节和信息关于如何执行更先进的透视变换). The cvWarpAffine function is used to perform the transformation, this takes a similiar form to the filter functions, but the more important input here is the third input variable, the map matrix(该cvWarpAffine函数用于执行转换,这需要一种类似过滤器功能的形式,但这里更重要的输入是第三个输入变量,地图矩阵). This is what defines the transformation. For this example we use cvGetAffineTransform to build the parallelograming warp matrix andcv2DRotationMatrix to build the matrix for performing a rotation and scaling on the image(这是定义的转变。对于这个例子,我们使用cvGetAffineTransform来建立平行四边形变形经矩阵和cv2DRotationMatrix用于对图像的旋转和缩放建立矩阵). For the first, three points in the original image and corresponding points in the destination image are used to define the warp(首先,原始图像中的三个点和目标图像中对应点的点被用来定义经). For the second, the center, scale, and angle are used to define the rotation(其次,中心,尺度和角度被用来定义旋转). This example uses sliders to allow you to change the rotation and scale(这个例子使用滑杆让你来改变旋转和尺度). Here is the code(以下是代码):


 

int angle_switch_value = 0;

int angleInt = 0;

int scale_switch_value = 0;

int scaleInt = 0;

 

void switch_callback_a( int position ){

        angleInt = position;

}

void switch_callback_s( int position ){

        scaleInt = position;

}

 

int _tmain(int argc, _TCHAR* argv[])

{

        // Set up variables

        CvPoint2D32f srcTri[3], dstTri[3];

        CvMat* rot_mat = cvCreateMat(2,3,CV_32FC1);

        CvMat* warp_mat = cvCreateMat(2,3,CV_32FC1);

        IplImage *src, *dst;

        const char* name = "Affine_Transform";

 

        // Load image

        src=cvLoadImage("MGC.jpg");

        dst = cvCloneImage( src );

        dst->origin = src->origin;

        cvZero( dst );

        cvNamedWindow( name, 1 );

 

        // Create angle and scale

        double angle = 0.0;

        double scale = 1.0;

 

        // Create trackbars

        cvCreateTrackbar( "Angle", name, &angle_switch_value, 4, switch_callback_a );

        cvCreateTrackbar( "Scale", name, &scale_switch_value, 4, switch_callback_s );

 

        // Compute warp matrix

        srcTri[0].x = 0;

        srcTri[0].y = 0;

        srcTri[1].x = src->width - 1;

        srcTri[1].y = 0;

        srcTri[2].x = 0;

        srcTri[2].y = src->height - 1;

 

        dstTri[0].x = src->width*0.0;

        dstTri[0].y = src->height*0.25;

        dstTri[1].x = src->width*0.90;

        dstTri[1].y = src->height*0.15;

        dstTri[2].x = src->width*0.10;

        dstTri[2].y = src->height*0.75;

 

        cvGetAffineTransform( srcTri, dstTri, warp_mat );

        cvWarpAffine( src, dst, warp_mat );

        cvCopy ( dst, src );

 

        while( 1 ) {

               switch( angleInt ){

                       case 0:

                               angle = 0.0;

                               break;

                       case 1:

                               angle = 20.0;

                               break;

                       case 2:

                               angle = 40.0;

                               break;

                       case 3:

                               angle = 60.0;

                               break;

                       case 4:

                               angle = 90.0;

                               break;

               }

               switch( scaleInt ){

                       case 0:

                               scale = 1.0;

                               break;

                       case 1:

                               scale = 0.8;

                               break;

                       case 2:

                               scale = 0.6;

                               break;

                       case 3:

                               scale = 0.4;

                               break;

                       case 4:

                               scale = 0.2;

                               break;

               }

 

               // Compute rotation matrix

               CvPoint2D32f center = cvPoint2D32f( src->width/2, src->height/2 );

               cv2DRotationMatrix( center, angle, scale, rot_mat );

 

               // Do the transformation

               cvWarpAffine( src, dst, rot_mat );

 

               cvShowImage( name, dst );

 

               if( cvWaitKey( 15 ) == 27 )

                       break;

        }

 

        cvReleaseImage( &dst );

        cvReleaseMat( &rot_mat );

        cvReleaseMat( &warp_mat );

 

        return 0;

}

 


Step 3: Histogram Equalization(直方图均衡化)


Original and Equalized Image


This example is very simple code, but can be a very useful function(这个例子是非常简单的代码,但可以是一个非常有用的功能). Images are captured with a limited dyanmic range due to the limitations of camera sensors(由于摄像传感器的局限性,图像在有限的动态范围内被拍摄). But by using histogram equalization, the brightness distribution of an image can be equalized and thereby increase the contrast of the image and in a sense increase the dynamic range(但通过使用直方图均衡化,图像的亮度分布均衡,从而可以在一定意义上提高了图像对比度,提高了动态范围). There are no extra variables for the function,cvEqualizeHist takes in just the source and destination image (which must be grayscale, or each color processed seperately)(这里没有额外的函数变量,cvEqualizeHist快速地获得原始图像和目的图像(它们必须被灰度化或单独处理每一种颜色)).                              


 

 

 

int _tmain(int argc, _TCHAR* argv[])

{

        // Set up images

        const char* name = "Histogram Equalization";

        IplImage *img = cvLoadImage("MGC.jpg", 0);

        IplImage* out = cvCreateImage( cvGetSize(img), IPL_DEPTH_8U, 1 );

 

        // Show original

        cvNamedWindow( "Original", 1) ;

        cvShowImage( "Original", img );

 

        // Perform histogram equalization

        cvEqualizeHist( img, out );

 

        // Show histogram equalized

        cvNamedWindow( name, 1) ;

        cvShowImage( name, out );

 

        cvWaitKey();

 

        cvReleaseImage( &img );

        cvReleaseImage( &out );

       

 

        return 0;

}

 


 

Final Words(结束语)

This tutorial's objective was to show how to use some image transform functions(本教程的目标是展示如何使用一些图像变换功能). You should be able to extend the use of these functions, and now learn about similiar more advanced function(你应该能够扩展这些功能的使用,而现在学习类似的更先进的功能). Please read the chapter for details on functions such as the perspective transform, discrete fourier transform, and others(请仔细阅读,如透视变换,离散傅里叶变换的详细功能章,以及其他).

Click here to email me.
Click here to return to my Tutorials page.

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值