aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFranklin Wei <franklin@rockbox.org>2019-12-09 01:19:46 -0500
committerFranklin Wei <franklin@rockbox.org>2019-12-09 01:19:46 -0500
commiteda3927401e78429fde5c7afbe506dda2f658c14 (patch)
tree27c4815a2a2363bb6b52b3713bbf7269603b53ff
downloadsloreg-eda3927401e78429fde5c7afbe506dda2f658c14.zip
sloreg-eda3927401e78429fde5c7afbe506dda2f658c14.tar.gz
sloreg-eda3927401e78429fde5c7afbe506dda2f658c14.tar.bz2
sloreg-eda3927401e78429fde5c7afbe506dda2f658c14.tar.xz
Add gabor filtering
-rw-r--r--Makefile5
-rw-r--r--gabor.cpp113
2 files changed, 118 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..0198ce4
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,5 @@
+all: gabor Makefile
+
+CXXFLAGS+=$(shell pkg-config --cflags --libs opencv4)
+CXXFLAGS+=-O2
+gabor: gabor.cpp
diff --git a/gabor.cpp b/gabor.cpp
new file mode 100644
index 0000000..f465912
--- /dev/null
+++ b/gabor.cpp
@@ -0,0 +1,113 @@
+#include <algorithm>
+#include <cassert>
+#include <cmath>
+#include <cstdio>
+#include <iostream>
+#include <opencv2/opencv.hpp>
+#include <string>
+
+using namespace cv;
+using namespace std;
+
+#if 0
+Mat contrastLUT(unsigned char thresh) {
+ Mat ret = Mat(256, 1, CV_8U);
+ for(int i = 0; i < 255; i++)
+ ret = (i < thresh) ? 0 : i;
+ return ret;
+}
+#endif
+
+// parameters were lifted off the internet
+Mat maximalGaborFilter(Mat in, int nfilts = 10,
+ int ksize = 9,
+ double sig = 3,
+ double lm = 8,
+ double gm = 0.02,
+ double ps = 0) {
+ Mat filtered[nfilts];
+ double dtheta = 2 * M_PI / nfilts;
+
+ // Gabor filtering
+ for(int i = 0; i < nfilts; i++)
+ {
+ //double theta = 0;
+ Mat kern = getGaborKernel(Size(ksize, ksize), sig, i * dtheta, lm, gm, ps, CV_32F);
+ Mat dest;
+ filter2D(in, dest, CV_32FC1, kern);
+ normalize(dest, dest, 0, 1, NORM_MINMAX);
+ //imshow(string("filt") + to_string(theta), dest);
+ filtered[i] = dest;
+ }
+
+ Size sz = filtered[0].size();
+
+ cout << "Result size: " << sz << endl;
+
+ Mat result = Mat(sz, CV_32F, Scalar(0));
+
+ for(int y = 0; y < sz.height; y++) {
+ for(int x = 0; x < sz.width; x++) {
+ float v = -1;
+ for(int i = 0; i < nfilts; i++)
+ v = std::max(v, filtered[i].at<float>(y, x));
+ //cout << y << ", " << x << endl;
+ result.at<float>(y, x) = v;
+ //outrow[x] = v;
+ }
+ }
+ return result;
+}
+
+// normalized sum of maximal gabor filtered image with varied kernel
+// size
+Mat summedGaborFilter(Mat in, int start, int stop, int step) {
+ assert(!(step & 1) && (start & 1) && (stop & 1));
+ int n = (stop - start) / step + 2;
+ double sf = 1. / n;
+
+ Mat result = Mat(in.size(), CV_32F, Scalar(0));
+ for(int i = start; i <= stop; i += step) {
+ Mat m = maximalGaborFilter(in, 10, i);
+ result += sf * m;
+ }
+ return result;
+}
+
+int main()
+{
+ while(1)
+ {
+ for(int i = 1; i < 23; i++)
+ {
+ char buf[64];
+ snprintf(buf, sizeof(buf), "SLO Data for registration/SLO001/SLO_subject001_frame%d.png", i);
+
+ Mat img;
+ img = imread(buf);
+ cvtColor(img, img, COLOR_BGR2GRAY);
+
+ resize(img, img, Size(512, 512));
+
+ imshow("orig", img);
+
+ Mat img_invert = Scalar::all(255) - img;
+
+ img_invert.convertTo(img_invert, CV_8UC1, 1, 0);
+
+ /*
+
+ for(int i = 5; i < 15; i += 2)
+ {
+ Mat result = maximalGaborFilter(img_invert, 10, i);
+
+ imshow(string("filtered") + to_string(i), result);
+ }
+ */
+ Mat result = summedGaborFilter(img_invert, 3, 13, 2);
+ imshow("filtered", result);
+
+ waitKey(0);
+ }
+ }
+}