如何在OpenCV中结合Gabor过滤器的结果

时间:2015-08-12 15:05:17

标签: java filter opencv3.0

我尝试使用带有4个过滤器(4 theta)的OpenCV for Java中的gabor过滤器来增强指纹图像:

private Mat enhanceImg(Mat myImg){

      // prepare the output matrix for filters
    Mat gabor1 = new Mat (myImg.width(), myImg.height(), CvType.CV_8UC1);
    Mat gabor2 = new Mat (myImg.width(), myImg.height(), CvType.CV_8UC1);
    Mat gabor3 = new Mat (myImg.width(), myImg.height(), CvType.CV_8UC1);
    Mat gabor4 = new Mat (myImg.width(), myImg.height(), CvType.CV_8UC1);
    Mat enhanced = new Mat (myImg.width(), myImg.height(), CvType.CV_8UC1);

      //predefine parameters for Gabor kernel 
    Size kSize = new Size(31,31);

    double theta1 = 0;
    double theta2 = 45;
    double theta3 = 90;
    double theta4 = 135;

    double lambda = 30;
    double sigma = 24;  
    double gamma = 1;
    double psi =  0;

       // the filters kernel
    Mat kernel1 = Imgproc.getGaborKernel(kSize, sigma, theta1, lambda, gamma, psi, CvType.CV_32F);
    Mat kernel2 = Imgproc.getGaborKernel(kSize, sigma, theta2, lambda, gamma, psi, CvType.CV_32F);
    Mat kernel3 = Imgproc.getGaborKernel(kSize, sigma, theta3, lambda, gamma, psi, CvType.CV_32F);
    Mat kernel4 = Imgproc.getGaborKernel(kSize, sigma, theta4, lambda, gamma, psi, CvType.CV_32F);

      // apply filters on my image. The result is stored in gabor1...4
    Imgproc.filter2D(myImg, gabor1, -1, kernel1);
    Imgproc.filter2D(myImg, gabor2, -1, kernel2);
    Imgproc.filter2D(myImg, gabor3, -1, kernel3);
    Imgproc.filter2D(myImg, gabor4, -1, kernel4);

    //enhanced = gabor1+gabor2+gabor3+gabor4 - something like that

    return enhanced;
}

现在,我的问题是如何组合这些结果,gabor1,gabor2,gabor3和gabor4,以获得整个增强图像。
我尝试使用Core.add(src1,src2,dst)

Core.addWeighted(enhanced , 0, gabor1, 1, 0, enhanced );
Core.addWeighted(enhanced , 1, gabor2, 1, 0, enhanced );
Core.addWeighted(enhanced , 1, gabor3, 1, 0, enhanced );
Core.addWeighted(enhanced , 1, gabor4, 1, 0, enhanced );

,但似乎做了一种减法,最后我得到的图像几乎是白色的 希望有人可以帮助我。我使用Java但C ++实现非常相似。

修改
以下是输入和结果的图像:

输入Img:
enter image description here

Img gabor1:
enter image description here



Img gabor2:
enter image description here



Img gabor3:
enter image description here



Img gabor4:
enter image description here



最后添加(加重)Img:
enter image description here

1 个答案:

答案 0 :(得分:5)

好的,我想我得到了解决问题的方法。
我的输入图像(myImg)的类型为CV_8UC1,但gabor内核的类型为CV_32F或CV_64F。所以我必须在处理之前将myImg转换为该类型:

myImg.convertTo(myImg, CvType.CV_32F);

另外,我将gabor1 ... 4定义为CV_32F类型而不是CV_8UC1 然后,我将Core.addWeighted(...)应用于所有4个gabor过滤器(如上所述),我得到了预期的结果。
这有点傻,但这是一个常见的错误(我认为)。