您的位置:首页 > 运维架构

利用opencv函数计算图像的梯度幅度和梯度方向

2016-10-19 16:38 423 查看
没有难点,就是为了方便使用记录,自己实现的话比较麻烦,直接使用内置函数计算比较省心。

重点是这个函数:

C++: void gpu::cartToPolar(const
GpuMat& x, const GpuMat& y, GpuMat& magnitude, GpuMat& angle, bool angleInDegrees=false, Stream&stream=Stream::Null())
Parameters:x – Source matrix containing real components ( CV_32FC1 ).
y – Source matrix containing imaginary components ( CV_32FC1 ).
magnitude – Destination matrix of float magnitudes ( CV_32FC1 ).
angle – Destionation matrix of angles ( CV_32FC1 ).
angleInDegress – Flag for angles that must be evaluated in degress.
stream – Stream for the asynchronous version.
#include<opencv2/opencv.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
int main()
{

//*****注意:数据类型非常非常重要!!数据类型不一致,程序不报错,但是计算结果严重错误
//如果是float类型就全是float,double类型就全是double

float data[3][3]={2,4,3,
7,5,6,
4,-8,9};
Mat mat=Mat(3,3,CV_32FC1,data);
cout<<mat;
cout<<endl<<"卷积运算"<<endl;
Mat outmat1,outmat2;
float k1[]={-1,0,1};  //水平方向的核
float k2[3][1]={-1,0,1};  //垂直的核
Mat Kore=Mat(1,3,CV_32FC1,k1);
Mat Kore2=Mat(3,1,CV_32FC1,k2);
filter2D(mat,outmat1,-1,Kore);    //水平卷积运算
filter2D(mat,outmat2,-1,Kore2);  //垂直卷积运算

cout<<outmat1<<endl;
cout<<outmat2;
Mat tidu;
Mat jiaodu;
cartToPolar(outmat1,outmat2,tidu,jiaodu,true);  //角度的结果在0-360之间
cout<<"-----------------"<<endl;
cout<<endl<<tidu;  //梯度幅度值图
cout<<endl<<jiaodu;   //角度图

waitKey(0);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐