Gabor

Gabor滤波器改

float W_line = 0.56;/使用自己写的Gabor滤波器时,脊线宽度所占整个周期(脊线加谷线)的比例

float PI = 3.141592653589793;

double modPI(double x)将角度x转成一个周期内的表达形式
{
	if (x >= 0 && x < (2*PI))
		return x;
	int t = (int)(x /(2* PI));
	return (x - t*2*PI);
}

double myGaborCos(double x)
{
	double res;
	if (x < 0)
		x = -x;
	if (x - 6.3153 < 0.001 && x>6.3)
		x = x;
	double y = modPI(x);///y在0-2PI之间
	
	if (y >= 0 && y < PI*W_line)
	{
		res = cos(x / (2 * W_line));
	}
	else if (y >= PI*W_line && y < (2 * PI - PI*W_line))
	{
		res = -cos((x - PI) / (2 - 2 * W_line));
	}
	else if (y >= (2 * PI - PI*W_line) && y < 2 * PI)
	{
		res = cos((x - 2 * PI) / (2 * W_line));
	}

	return res;
	
	
}

//拷贝OpenCV源码的并且稍微改了下
Mat MygetGaborKernel(Size ksize, double sigma, double theta,
	double lambd, double gamma, double psi, int ktype = CV_64F)
{
	double sigma_x = sigma;
	double sigma_y = sigma / gamma;
	int nstds = 3;
	int xmin, xmax, ymin, ymax;
	double c = cos(theta), s = sin(theta);

	// 定卷积核的大小
	if (ksize.width > 0)
		xmax = ksize.width / 2;
	else
		xmax = cvRound(std::max(fabs(nstds*sigma_x*c), fabs(nstds*sigma_y*s)));

	if (ksize.height > 0)
		ymax = ksize.height / 2;
	else
		ymax = cvRound(std::max(fabs(nstds*sigma_x*s), fabs(nstds*sigma_y*c)));

	xmin = -xmax;
	ymin = -ymax;

	CV_Assert(ktype == CV_32F || ktype == CV_64F);

	Mat kernel(ymax - ymin + 1, xmax - xmin + 1, ktype);
	double scale = 1;
	double ex = -0.5 / (sigma_x*sigma_x);
	double ey = -0.5 / (sigma_y*sigma_y);
	double cscale = CV_PI * 2 / lambd;

	for (int y = ymin; y <= ymax; y++)
		for (int x = xmin; x <= xmax; x++)
		{
			double xr = x*c + y*s;
			double yr = -x*s + y*c;

			// Gabor滤波器的公式,将余弦值的计算换成实际需要的
			double v = scale*std::exp(ex*xr*xr + ey*yr*yr)*myGaborCos(cscale*xr + psi);
			if (ktype == CV_32F)
				kernel.at<float>(ymax - y, xmax - x) = (float)v;
			else
				kernel.at<double>(ymax - y, xmax - x) = v;
		}

	return kernel;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值