Gabor变换的opencv实现

2024-02-10 20:18
文章标签 实现 opencv 变换 gabor

本文主要是介绍Gabor变换的opencv实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一直在做人脸识别方向,想用一下Gabor滤波器做一下Gabor人脸。从网上看到 Mian Zhou. Thesis. Gabor-Boosting Face Recognition的Gabor代码,也是基于Opencv的但是,代码是基于C结构的,用起来感觉不方便,就花一天的时间,修改成了C++结构的。
/*
Copyright (C) 2014 by Fangqi Su
Reference: Mian Zhou. Thesis. Gabor-Boosting Face Recognition
*/
#ifndef GABORFLITER_H
#define GABORFLITER_H
#include<iostream>
#include<opencv2\opencv.hpp>
#include<highgui.h>
#define CV_GABOR_REAL 1
#define CV_GABOR_IMAG 2
#define CV_GABOR_MAG 3
#define CV_GABOR_PHASE 4
using namespace cv;
class Gabor
{public:Gabor();Gabor(int iMu,int iNu,double dSigma);Gabor(int iMu,int iNu,double dSigma,double dF);Gabor(double dPhi,int iNu);Gabor(double dPhi,int iNu,double dSigma);Gabor(double dPhi,int iNu,double dSigma,double dF);Gabor(int iMu,int iNu);bool IsInit();bool IsKernelCreate();long mask_width();void Init(int iMu,int iNu,double dSigma,double dF);void Init(double dPhi,int iNu,double dSigma,double dF);void output_file(const char *filename,int Type);Mat get_matrix(int Type);Mat get_image(int Type);void show(int Type);void conv_img(Mat &src,Mat&dst,int Type);long Gabor::get_mask_width();protected:int KernelSize;double Sigma;double F;double Kmax;double K;double Phi;bool bInitialised;bool bKernel;long Width;Mat Imag;Mat Real;private:void creat_kernel();};#endif
/*
Copyright (C) 2014 by Fangqi Su
Reference: Mian Zhou. Thesis. Gabor-Boosting Face Recognition
*/
#include"GaborFliter.h"Gabor::Gabor()
{}
/*
fn Gabor::Gabor(int iMu,int iNu,double dSigma);
Constuct a gabor
Parameters:
iMu		The orientation iMu*PI/8,
iNu 		The scale,
dSigma 		The sigma value of Gabor,Create a gabor with a orientation iMu*PI/8, a scale iNu, and a sigma value dSigma. The spatial frequence (F) is set to sqrt(2) defaultly. It calls Init() to generate parameters and kernels.
*/
Gabor::Gabor(int iMu,int iNu,double dSigma)
{F=sqrt(2.0);Init(iMu,iNu,dSigma,F);
}
/*!
\fn Gabor::Gabor(int iMu, int iNu, double dSigma, double dF)
Construct a gaborParameters:
iMu		    The orientation iMu*PI/8
iNu 		The scale
dSigma 		The sigma value of Gabor
dF		    The spatial frequency Returns:
NoneCreate a gabor with a orientation iMu*PI/8, a scale iNu, a sigma value dSigma, and a spatial frequence dF. It calls Init() to generate parameters and kernels.
*/
Gabor::Gabor(int iMu,int iNu,double dSigma,double dF)
{Init(iMu,iNu,dSigma,dF);
}
/*!
\fn Gabor::Gabor(double dPhi, int iNu)
Construct a gaborParameters:
dPhi		The orientation in arc
iNu 		The scaleReturns:
NoneCreate a gabor with a orientation dPhi, and with a scale iNu. The sigma (Sigma) and the spatial frequence (F) are set to 2*PI and sqrt(2) defaultly. It calls Init() to generate parameters and kernels.
*/
Gabor::Gabor(double dPhi,int iNu)
{Sigma=2*CV_PI;F=sqrt(2.0);Init(dPhi,iNu,Sigma,F);
}
/*!
\fn Gabor::Gabor(double dPhi, int iNu, double dSigma)
Construct a gaborParameters:
dPhi		The orientation in arc
iNu 		The scale
dSigma		The sigma value of GaborReturns:
NoneCreate a gabor with a orientation dPhi, a scale iNu, and a sigma value dSigma. The spatial frequence (F) is set to sqrt(2) defaultly. It calls Init() to generate parameters and kernels.
*/
Gabor::Gabor(double dPhi,int iNu,double dSigma)
{F=sqrt(2.0);Init(dPhi,iNu,dSigma,F);
}
/*!
\fn Gabor::Gabor(double dPhi, int iNu, double dSigma, double dF)
Construct a gaborParameters:
dPhi		The orientation in arc
iNu 		The scale
dSigma 		The sigma value of Gabor
dF		The spatial frequency Returns:
NoneCreate a gabor with a orientation dPhi, a scale iNu, a sigma value dSigma, and a spatial frequence dF. It calls Init() to generate parameters and kernels.
*/
Gabor::Gabor(double dPhi,int iNu,double dSigma,double dF)
{Init(dPhi,iNu,dSigma,dF);
}
/*!
\fn Gabor::IsInit()
Determine the gabor is initilised or notParameters:
NoneReturns:
a boolean value, TRUE is initilised or FALSE is non-initilised.Determine whether the gabor has been initlized - variables F, K, Kmax, Phi, Sigma are filled.
*/
bool Gabor::IsInit()
{return bInitialised;
}
/*!
\fn Gabor::mask_width()
Give out the width of the maskParameters:
NoneReturns:
The long type show the width.Return the width of mask (should be NxN) by the value of Sigma and iNu.
*/
long Gabor::mask_width()
{long lWidth;if(IsInit()==false){perror("Error:Error: The Object has not been initilised in mask_width()!\n");return 0;}else {double dModSigma=Sigma/K;double dWidth=cvRound(dModSigma*6+1);if(fmod(dWidth,2.0)==0.0)dWidth++;lWidth=(long)dWidth;return lWidth;}
}
/*!
\fn Gabor::creat_kernel()
Create gabor kernelParameters:
NoneReturns:
NoneCreate 2 gabor kernels - REAL and IMAG, with an orientation and a scale 
*/
void Gabor::creat_kernel()
{if (IsInit() == false) {perror("Error: The Object has not been initilised in creat_kernel()!\n");}else {Mat mReal(Width,Width,CV_32F),mImag(Width,Width,CV_32F);/**************************** Gabor Function ****************************/ int x,y;double dReal;double dImag;double dTemp1,dTemp2,dTemp3;for(int i=0;i<Width;i++){for(int j=0;j<Width;j++){x=i-(Width-1)/2;y=j-(Width-1)/2;dTemp1 = (pow(K,2)/pow(Sigma,2))*exp(-(pow((double)x,2)+pow((double)y,2))*pow(K,2)/(2*pow(Sigma,2)));dTemp2 = cos(K*cos(Phi)*x + K*sin(Phi)*y) - exp(-(pow(Sigma,2)/2));dTemp3 = sin(K*cos(Phi)*x + K*sin(Phi)*y);dReal = dTemp1*dTemp2;dImag = dTemp1*dTemp3; mReal.at<float>(i,j)=dReal;mImag.at<float>(i,j)=dImag;}}/**************************** Gabor Function ****************************/bKernel=true;mReal.copyTo(Real);mImag.copyTo(Imag);}
}
/*!
\fn Gabor::get_image(int Type)
Get the speific type of image of GaborParameters:
Type		The Type of gabor kernel, e.g. REAL, IMAG, MAG, PHASE   Returns:
Pointer to image structure, or NULL on failure	Return an Image (gandalf image class) with a specific Type   "REAL"	"IMAG" "MAG" "PHASE"  
*/
Mat Gabor::get_image(int Type)
{if(IsKernelCreate() == false){ perror("Error: the Gabor kernel has not been created in get_image()!\n");return Mat();}Mat pImage(Width,Width,CV_32F,1);Mat newimage(Width,Width,CV_32FC1);Mat kernel(Width,Width,CV_32FC1);Mat re(Width,Width,CV_32FC1);Mat im(Width,Width,CV_32FC1);Mat ve;int rows=kernel.rows;int cols=kernel.cols;switch (Type){case 1://realReal.copyTo(kernel);kernel.copyTo(pImage);pImage=pImage.t();break;case 2://ImagImag.copyTo(kernel);kernel.copyTo(pImage);pImage=pImage.t();break;case 3://MagnitudeReal.copyTo(re);Imag.copyTo(im);pow(re,2,re);pow(im,2,im);pow(im+re,0.5,ve);ve.copyTo(pImage);pImage=pImage.t();break;default:break;}normalize(pImage,pImage,0,255,CV_MINMAX,NULL);pImage.convertTo(pImage,CV_8U);convertScaleAbs(pImage,newimage,1,0);newimage.convertTo(newimage,CV_8U);return newimage;
}
/*!
\fn Gabor::IsKernelCreate()
Determine the gabor kernel is created or notParameters:
NoneReturns:
a boolean value, TRUE is created or FALSE is non-created.Determine whether a gabor kernel is created.
*/
bool Gabor::IsKernelCreate()
{return bKernel;
}
/*!
\fn Gabor::get_mask_width()
Reads the width of MaskParameters:
NoneReturns:
Pointer to long type width of mask.
*/
long Gabor::get_mask_width()
{return Width;
}
/*!
\fn Gabor::Init(int iMu, int iNu, double dSigma, double dF)
Initilize the.gaborParameters:
iMu 	The orientations which is iMu*PI.8
iNu 	The scale can be from -5 to infinit
dSigma 	The Sigma value of gabor, Normally set to 2*PI
dF 	The spatial frequence , normally is sqrt(2)Returns:Initilize the.gabor with the orientation iMu, the scale iNu, the sigma dSigma, the frequency dF, it will call the function creat_kernel(); So a gabor is created.
*/
void Gabor::Init(int iMu,int iNu,double dSigma,double dF)
{bInitialised=false;bKernel=false;F=dF;Sigma=dSigma;Kmax=CV_PI/2;// Absolute value of KK = Kmax / pow(F, (double)iNu);Phi = CV_PI*iMu/8;bInitialised = true;Width = mask_width();Real = Mat( Width, Width, CV_32FC1);Imag = Mat( Width, Width, CV_32FC1);creat_kernel();  }
/*!
\fn CvGabor::CvGabor(int iMu, int iNu)
*/
Gabor::Gabor(int iMu, int iNu)
{double dSigma = 2*CV_PI; F = sqrt(2.0);Init(iMu, iNu, dSigma, F);
}
/*!
\fn Gabor::Init(double dPhi, int iNu, double dSigma, double dF)
Initilize the.gaborParameters:
dPhi 	The orientations 
iNu 	The scale can be from -5 to infinit
dSigma 	The Sigma value of gabor, Normally set to 2*PI
dF 	The spatial frequence , normally is sqrt(2)Returns:
NoneInitilize the.gabor with the orientation dPhi, the scale iNu, the sigma dSigma, the frequency dF, it will call the function creat_kernel(); So a gabor is created.filename 	The name of the image file
file_format 	The format of the file, e.g. GAN_PNG_FORMAT
image 	The image structure to be written to the file
octrlstr 	Format-dependent control structure*/
void Gabor::Init(double dPhi, int iNu, double dSigma, double dF)
{bInitialised = false;bKernel = false;Sigma = dSigma;F = dF;Kmax =CV_PI/2;// Absolute value of KK = Kmax / pow(F, (double)iNu);Phi = dPhi;bInitialised = true;Width = mask_width();Real = Mat( Width, Width, CV_32FC1);Imag = Mat( Width, Width, CV_32FC1);creat_kernel();  
}
/*!
\fn Gabor::get_matrix(int Type)
Get a matrix by the type of kernelParameters:
Type		The type of kernel, e.g. REAL, IMAG, MAG, PHASEReturns:
Pointer to matrix structure, or NULL on failure.Return the gabor kernel.
*/
Mat Gabor::get_matrix(int Type)
{if (!IsKernelCreate()) {perror("Error: the gabor kernel has not been created!\n"); return Mat();}switch (Type){case CV_GABOR_REAL:return Real;break;case CV_GABOR_IMAG:return Imag;break;case CV_GABOR_MAG:return Mat();break;case CV_GABOR_PHASE:return Mat();break;}
}
/*!
\fn Gabor::output_file(const char *filename, Gan_ImageFileFormat file_format, int Type)
Writes a gabor kernel as an image file.Parameters:
filename 	The name of the image file
file_format 	The format of the file, e.g. GAN_PNG_FORMAT
Type		The Type of gabor kernel, e.g. REAL, IMAG, MAG, PHASE   
Returns:
NoneWrites an image from the provided image structure into the given file and the type of gabor kernel.
*/
void Gabor::output_file(const char *filename, int Type)
{Mat pImage;pImage = get_image(Type);if(pImage .data!= NULL){if( imwrite(filename, pImage )) printf("%s has been written successfully!\n", filename);else printf("Error: writting %s has failed!\n", filename);}else perror("Error: the image is empty in output_file()!\n"); }
/*!
\fn CvGabor::conv_img_a(IplImage *src, IplImage *dst, int Type)
*/
void Gabor::conv_img(Mat &src, Mat &dst, int Type)
{Mat mat;src.copyTo(mat);mat.convertTo(mat,CV_32FC1);mat=mat.t();Mat temp;Mat rmat(src.rows,src.cols,CV_32FC1);Mat imat(src.rows,src.cols,CV_32FC1);Mat kernel(Width,Width,CV_32FC1);switch (Type){case CV_GABOR_REAL:Real.copyTo(kernel);filter2D( mat, mat,mat.depth(),kernel, Point( (Width-1)/2, (Width-1)/2));break;case CV_GABOR_IMAG:Imag.copyTo(kernel);filter2D( mat, mat,mat.depth(),kernel, Point( (Width-1)/2, (Width-1)/2));break;case CV_GABOR_MAG:/* Real Response */Real.copyTo(kernel);filter2D( mat, rmat,rmat.depth(),kernel, Point( (Width-1)/2, (Width-1)/2));/* Imag Response */Imag.copyTo(kernel);filter2D( mat, imat,imat.depth(),kernel, Point( (Width-1)/2, (Width-1)/2));/* Magnitude response is the square root of the sum of the square of real response and imaginary response */pow(rmat,2,rmat);pow(imat,2,imat);add(rmat,imat,temp);pow(temp,0.5,temp);temp.copyTo(mat);break;case CV_GABOR_PHASE:break;}normalize(mat,mat,0,255,CV_MINMAX,NULL);mat.convertTo(mat,CV_8U);mat=mat.t();mat.copyTo(dst);
}

这篇关于Gabor变换的opencv实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/697975

相关文章

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

Java实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

如何通过Python实现一个消息队列

《如何通过Python实现一个消息队列》这篇文章主要为大家详细介绍了如何通过Python实现一个简单的消息队列,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录如何通过 python 实现消息队列如何把 http 请求放在队列中执行1. 使用 queue.Queue 和 reque

Python如何实现PDF隐私信息检测

《Python如何实现PDF隐私信息检测》随着越来越多的个人信息以电子形式存储和传输,确保这些信息的安全至关重要,本文将介绍如何使用Python检测PDF文件中的隐私信息,需要的可以参考下... 目录项目背景技术栈代码解析功能说明运行结php果在当今,数据隐私保护变得尤为重要。随着越来越多的个人信息以电子形

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景

使用Python快速实现链接转word文档

《使用Python快速实现链接转word文档》这篇文章主要为大家详细介绍了如何使用Python快速实现链接转word文档功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 演示代码展示from newspaper import Articlefrom docx import