//gaborFilterHelper 根据opencv-gabor-filter-master 修改
#include "stdafx.h"
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <math.h>
//定义初始系数
//创建gabor核
cv::Mat mkKernel( int ks, double sig, double th, double lm, double ps)
{
int hks = (ks-1)/2;
double theta = th*CV_PI/180;
double psi = ps*CV_PI/180;
double del = 2.0/(ks-1);
double lmbd = lm;
double sigma = sig/ks;
double x_theta;
double y_theta;
cv::Mat kernel(ks,ks, CV_32F);
for (int y=-hks; y<=hks; y++)
{
for (int x=-hks; x<=hks; x++)
{
x_theta = x*del*cos(theta)+y*del*sin(theta);
y_theta = -x*del*sin(theta)+y*del*cos(theta);
kernel.at< float >(hks+y,hks+x) = (float )exp(-0.5*(pow(x_theta,2)+pow(y_theta,2))/pow(sigma,2))* cos(2*CV_PI*x_theta/lmbd + psi);
}
}
return kernel;
}
//得到gabor图像,在我看来gabor算是一种纹理强化,类似于canny
Mat GetGabor(Mat src, double sig,double lm, double th, double ps,int kernel_size)
{
Mat dest;
Mat src_f;
if (!kernel_size%2)
{
kernel_size+=1;
}
src.convertTo(src_f, CV_32F, 1.0/255, 0);
//创建卷积核
cv::Mat kernel = mkKernel(kernel_size, sig, th, lm, ps);
//卷积
cv::filter2D(src_f, dest, CV_32F, kernel);
cv::Mat Lkernel(kernel_size*20, kernel_size*20, CV_32F);
cv::resize(Lkernel, Lkernel, Lkernel.size());
Lkernel /= 2.;
Lkernel += 0.5;
cv::Mat mag;
cv::pow(dest, 2.0, mag);
return mag;
}
//int main(int argc, char** argv)
//{
// cv::Mat image = cv::imread("数据集/训练图片/1.jpg",1);
// cv::imshow("Src", image);
// cv::Mat src;
// cv::cvtColor(image, src, CV_BGR2GRAY);
// src.convertTo(src_f, CV_32F, 1.0/255, 0);
// if (!kernel_size%2)
// {
// kernel_size+=1;
// }
// cv::namedWindow("Process window", 1);
// cv::createTrackbar("Sigma", "Process window", &pos_sigma, kernel_size, Process);
// cv::createTrackbar("Lambda", "Process window", &pos_lm, 100, Process);
// cv::createTrackbar("Theta", "Process window", &pos_th, 180, Process);
// cv::createTrackbar("Psi", "Process window", &pos_psi, 360, Process);
// Process(0,0);
// cv::waitKey(0);
// return 0;
//}