C++ 如何将Gabor滤波器与Sobel和DFT滤波器输出结合使用
我将在图像上使用Gabor滤波器来增强它,遵循图像增强的主要思想。目标是获得输入图像的方向和频率,然后使用它们进行Gabor滤波。 首先,我将图像转换为灰度,然后为了获得方向图像,我使用Sobel滤波器,对于频率图像,我使用DFT滤波器。我的代码类似于OpenCV文档示例 索贝尔: 并将此部分添加到上面链接中的代码中,以获得方向图像:C++ 如何将Gabor滤波器与Sobel和DFT滤波器输出结合使用,c++,image,opencv,image-processing,filter,C++,Image,Opencv,Image Processing,Filter,我将在图像上使用Gabor滤波器来增强它,遵循图像增强的主要思想。目标是获得输入图像的方向和频率,然后使用它们进行Gabor滤波。 首先,我将图像转换为灰度,然后为了获得方向图像,我使用Sobel滤波器,对于频率图像,我使用DFT滤波器。我的代码类似于OpenCV文档示例 索贝尔: 并将此部分添加到上面链接中的代码中,以获得方向图像: cv::Mat calculateOrientations(cv::Mat gradientX, cv::Mat gradientY) { // Cre
cv::Mat calculateOrientations(cv::Mat gradientX, cv::Mat gradientY)
{
// Create container element
cv::Mat orientation = cv::Mat(gradientX.rows, gradientX.cols, CV_32F);
// Calculate orientations of gradients --> in degrees
// Loop over all matrix values and calculate the accompagnied orientation
for (int i = 0; i < gradientX.rows; i++)
{
for (int j = 0; j < gradientX.cols; j++)
{
// Retrieve a single value
float valueX = gradientX.at<float>(i, j);
float valueY = gradientY.at<float>(i, j);
// Calculate the corresponding single direction, done by applying the arctangens function
float result = cv::fastAtan2(valueX, valueY);
// Store in orientation matrix element
orientation.at<float>(i, j) = result;
}
}
return orientation;
}
cv::Mat计算方向(cv::Mat gradientX,cv::Mat gradientY)
{
//创建容器元素
cv::Mat方向=cv::Mat(gradientX.rows,gradientX.cols,cv_32F);
//计算渐变的方向-->以度为单位
//循环所有矩阵值并计算伴随方向
对于(int i=0;i
对于DFT:
然后,我存储这两个滤波器的输出,并将它们作为Gabor滤波器的输入参数传递。我的Gabor滤波器的输入图像是我的灰度图像的二值掩码。对于这一部分,我的代码如下:
cv::Mat filter(cv::Mat inputImg, cv::Mat orientationImg, cv::Mat frequency, double theta_x, double theta_y)
{
cv::Mat gaborResult = cv::Mat::zeros(inputImg.rows, inputImg.cols, inputImg.type());
for (int i = 0; i < inputImg.rows; i++)
{
for (int j = 0; j < inputImg.cols; j++)
{
float xTheta = i*cos(orientationImg.at<float>(i, j)) + j*sin(orientationImg.at<float>(i, j));
float yTheta = -i*sin(orientationImg.at<float>(i, j)) + j*cos(orientationImg.at<float>(i, j));
float num1 = (xTheta * xTheta) / (theta_x * theta_x);
float num2 = (yTheta * yTheta) / (theta_y * theta_y);
float res = exp((-1 / 2) * (num1 + num2)) * cos(2 * pi * frequency.at<float>(i, j) * xTheta);
gaborResult.at<float>(i, j) = res;
}
}
cv::Mat result = mult(inputImg, gaborResult);
return result;
}
cv::Mat mult(cv::Mat inputImage, cv::Mat gaborResult)
{
cv::Mat enhancedImg = cv::Mat::zeros(inputImage.rows, inputImage.cols, inputImage.type());;
float sum = 0.0;
for (int i = 0; i < inputImage.rows; i++)
{
for (int j = 0; j < inputImage.cols; j++)
{
enhancedImg.at<float>(i, j) = gaborResult.at<float>(i, j) * inputImage.at<float>(i, j);
}
}
return enhancedImg;
}
cv::Mat过滤器(cv::Mat输入、cv::Mat方向、cv::Mat频率、双θx、双θy)
{
cv::Mat gaborResult=cv::Mat::Zero(inputImg.rows、inputImg.cols、inputImg.type());
对于(int i=0;i
正如文章建议的那样,theta_x
和theta_y
的参数是4.0。orientationImg
和frequency
分别来自Sobel和DFT操作。
我做了一个像素级的Gabor滤波,如上图所示。然而,输出图像非常奇怪,甚至不接近我的增强目标
这是Sobel和DFT过滤器的原始输入图像:
这是Gabor滤波器的输入二进制掩码:
这是我的Gabor滤波器的最终输出:
显然,我在这里没有正确使用Gabor滤波器。关于这个话题,我在网上搜索了很多,但没有一个能解决我的问题。他们中的大多数人都强调获得一个Gabor核并在整个图像上使用它,这在我的例子中是不同的。有人能更好地改进我的代码/找出我的错误吗?先谢谢你 看起来像未规范化的浮点输出。 尝试将其规格化为间隔[0,1]
cv::normalize(src,dst,0,1,cv::NORM_MINMAX);
我已经试过那种方法,但它没有解决我的问题。