OpenCV Tutorial 5 - Chapter 6

本教程展示了如何使用OpenCV进行图像边缘检测、仿射变换和直方图均衡化等核心操作,提供了详细的代码示例和参数调整,旨在帮助读者理解和应用计算机视觉的基本原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 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.

 

基于数据挖掘的音乐推荐系统设计与实现 需要一个代码说明,不需要论文 采用python语言,django框架,mysql数据库开发 编程环境:pycharm,mysql8.0 系统分为前台+后台模式开发 网站前台: 用户注册, 登录 搜索音乐,音乐欣赏(可以在线进行播放) 用户登陆时选择相关感兴趣的音乐风格 音乐收藏 音乐推荐算法:(重点) 本课题需要大量用户行为(如播放记录、收藏列表)、音乐特征(如音频特征、歌曲元数据)等数据 (1)根据用户之间相似性或关联性,给一个用户推荐与其相似或有关联的其他用户所感兴趣的音乐; (2)根据音乐之间的相似性或关联性,给一个用户推荐与其感兴趣的音乐相似或有关联的其他音乐。 基于用户的推荐和基于物品的推荐 其中基于用户的推荐是基于用户的相似度找出相似相似用户,然后向目标用户推荐其相似用户喜欢的东西(和你类似的人也喜欢**东西); 而基于物品的推荐是基于物品的相似度找出相似的物品做推荐(喜欢该音乐的人还喜欢了**音乐); 管理员 管理员信息管理 注册用户管理,审核 音乐爬虫(爬虫方式爬取网站音乐数据) 音乐信息管理(上传歌曲MP3,以便前台播放) 音乐收藏管理 用户 用户资料修改 我的音乐收藏 完整前后端源码,部署后可正常运行! 环境说明 开发语言:python后端 python版本:3.7 数据库:mysql 5.7+ 数据库工具:Navicat11+ 开发软件:pycharm
MPU6050是一款广泛应用在无人机、机器人和运动设备中的六轴姿态传感器,它集成了三轴陀螺仪和三轴加速度计。这款传感器能够实时监测并提供设备的角速度和线性加速度数据,对于理解物体的动态运动状态至关重要。在Arduino平台上,通过特定的库文件可以方便地与MPU6050进行通信,获取并解析传感器数据。 `MPU6050.cpp`和`MPU6050.h`是Arduino库的关键组成部分。`MPU6050.h`是头文件,包含了定义传感器接口和函数声明。它定义了类`MPU6050`,该类包含了初始化传感器、读取数据等方法。例如,`begin()`函数用于设置传感器的工作模式和I2C地址,`getAcceleration()`和`getGyroscope()`则分别用于获取加速度和角速度数据。 在Arduino项目中,首先需要包含`MPU6050.h`头文件,然后创建`MPU6050`对象,并调用`begin()`函数初始化传感器。之后,可以通过循环调用`getAcceleration()`和`getGyroscope()`来不断更新传感器读数。为了处理这些原始数据,通常还需要进行校准和滤波,以消除噪声和漂移。 I2C通信协议是MPU6050与Arduino交互的基础,它是一种低引脚数的串行通信协议,允许多个设备共享一对数据线。Arduino板上的Wire库提供了I2C通信的底层支持,使得用户无需深入了解通信细节,就能方便地与MPU6050交互。 MPU6050传感器的数据包括加速度(X、Y、Z轴)和角速度(同样为X、Y、Z轴)。加速度数据可以用来计算物体的静态位置和动态运动,而角速度数据则能反映物体转动的速度。结合这两个数据,可以进一步计算出物体的姿态(如角度和角速度变化)。 在嵌入式开发领域,特别是使用STM32微控制器时,也可以找到类似的库来驱动MPU6050。STM32通常具有更强大的处理能力和更多的GPIO口,可以实现更复杂的控制算法。然而,基本的传感器操作流程和数据处理原理与Arduino平台相似。 在实际应用中,除了基本的传感器读取,还可能涉及到温度补偿、低功耗模式设置、DMP(数字运动处理器)功能的利用等高级特性。DMP可以帮助处理传感器数据,实现更高级的运动估计,减轻主控制器的计算负担。 MPU6050是一个强大的六轴传感器,广泛应用于各种需要实时运动追踪的项目中。通过 Arduino 或 STM32 的库文件,开发者可以轻松地与传感器交互,获取并处理数据,实现各种创新应用。博客和其他开源资源是学习和解决问题的重要途径,通过这些资源,开发者可以获得关于MPU6050的详细信息和实践指南
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值