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

来源:互联网 发布:卖情侣装好的淘宝店 编辑:程序博客网 时间:2024/06/07 06:12

没有难点,就是为了方便使用记录,自己实现的话比较麻烦,直接使用内置函数计算比较省心。

重点是这个函数:

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类型就全是doublefloat 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;}


0 0