首页 文章

如何使用带有Sobel和DFT滤波器输出的Gabor滤波器

提问于
浏览
1

我将在图像上使用Gabor滤镜来增强它,遵循this article关于图像增强的主要思想 . 目标是获得输入图像的方向和频率,然后将它们用于Gabor滤波 . 首先,我将图像转换为灰度,然后为了获得方向图像,我使用了Sobel滤镜,而对于频率图像,我使用了DFT滤镜 . 我的代码类似于OpenCV文档示例 .

索贝尔:

http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/sobel_derivatives/sobel_derivatives.html

并将此部分添加到上面链接中的代码以获取方向图像:

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;
}

对于DFT:

http://docs.opencv.org/doc/tutorials/core/discrete_fourier_transform/discrete_fourier_transform.html

然后,我存储这两个滤波器的输出,并将它们作为我的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;
}

文章建议,params, theta_xtheta_y 是4.0 . orientationImgfrequency 分别来自Sobel和DFT运营 . 我做了一个像素方面的Gabor过滤,如上所示 . 但是,输出图像非常奇怪,甚至没有接近我的增强目标 .

这是Sobel和DFT滤镜的原始输入图像:

enter image description here

这是Gabor过滤器的输入二进制掩码:

enter image description here

这是我的Gabor过滤器的最终输出:

enter image description here

显然我在这里没有正确使用Gabor过滤器 . 我在互联网上搜索了很多关于这个主题的内容,但没有一个能解决我的问题 . 他们中的大多数都强调获取一个Gabor内核并在整个图像上使用它,这在我的情况下是不同的 . 有人可以更好地改进我的代码/找出我的错误吗?先感谢您 .

1 回答

  • 0

    看起来像非标准化的浮点输出 . 尝试将其标准化为区间[0,1] .

    cv::normalize(src,dst,0,1,cv::NORM_MINMAX);
    

相关问题