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

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

重点是这个函数:

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;
}


  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值