OpenCV Java API で Gabor Filter

OpenCV 3.0 Java API で Gabor Filter を動かしてみました。
以下サンプルソースの下に説明を書きます。

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;
//
// Applying the Gabor filter to the input image.
//
class GaborFilter {
 double height = 10;
 double width = 10;
 double sigma = 1.4;
 double thetaDeg = 135;
 double lambda = 4;
 double gamma = 1.0;
 double psiDeg = 0;
 int ktype;

 public void apply() {
            System.out.println("Applying the Gabor filter");
            Size ksize = new Size(height, width);
            // Reading an image
            Mat image = Highgui.imread("lena.png");
            double theta = thetaDeg*Math.PI/180; // radian
            double psi = psiDeg*Math.PI/180;  // radian
             // Making the source black and white.
            Mat mat1 = new Mat(image.rows(), image.cols(), CvType.CV_8UC1);
            Imgproc.cvtColor(image, mat1, Imgproc.COLOR_RGB2GRAY);
             // Applying the Gabor filter.
            Mat kernel = Imgproc.getGaborKernel(ksize, sigma, theta, lambda, gamma);
            Mat dest = new Mat(mat1.rows(), mat1.cols(), image.type());
            Imgproc.filter2D(mat1, dest, mat1.type(), kernel);
            // Save the result.
            String filename = "gaborFiltered.png";
            System.out.println(String.format("Writing %s", filename));
            Highgui.imwrite(filename, dest);
        }
 
        public static void main(String[] args) {
          // Load the native library.
          System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
          new GaborFilter().apply();
        }
 }

Imgproc クラスの getGaborKernel でカーネル(フィルタの行列)を取得し、同じクラスの filter2D メソッドでカーネルを適用しています。
サンプルなのでパラメータは直打ちしています(sigma など)。
パラメータの意味については下記参考文献1を見てください。
パラメータはいろいろ試してみて、エッジ検出の観点からよさそうなものを選びましたが、下記参考文献1の結果には及ばないようです。
結果の画像をあげておきます。
theta=0
theta=45
theta=90
theta=135

参考にしたページ:
  1. 画像処理 (2) ガボール・フィルタ
  2. OpenCV/Gabor Filter (日本語)
  3. OpenCV GrayScale Conversion Tutorial

Commenti

Post popolari in questo blog

Aipo に Let's Encrypt の SSL証明書を適用

ES611の電池交換

マケドニア語・ブルガリア語同時学習その5(名詞の性と数、後置冠詞形)