OpenCV_图像梯度化

OpenCV_图像梯度化

#include "stdafx.h"

#include "cv.h"
#include "cxcore.h"
#include "highgui.h"

using namespace std;
using namespace cv;
int main(int argc, char **argv)
{
	CvPoint center;//定义一个二维坐标的点
	double scale = -3;
	int i,j;
	Mat image = imread("pic.jpg");//打开一张图片
	if(!image.data)
		return -1;
	center=Point2f(image.cols/2,image.rows/2);//构造这个二维坐标的点

	Mat ptr[3];
	Mat gradImg;
	split(image,ptr);
	for(int i=0;i<image.rows;i++)
	{
		for(int j=0;j<image.cols;j++)
		{
			double dx=(double)(j-center.x)/center.x;
			double dy=(double)(i-center.y)/center.y;
			double weight =exp((dx*dx+dy*dy)*scale);

			ptr[0].at<uchar>(i,j)=(int)ptr[0].at<uchar>(i,j)*weight;// 将浮点数转化为整数。
			ptr[1].at<uchar>(i,j)=(int)ptr[1].at<uchar>(i,j)*weight;
			ptr[2].at<uchar>(i,j)=(int)ptr[2].at<uchar>(i,j)*weight;
		}
	}
		merge(ptr,3,gradImg);
	
		imshow("Window",gradImg);
		cvWaitKey(-1);
		return 0;
}
效果:

OpenCV_图像梯度化_第1张图片

你可能感兴趣的:(opencv)