aboutsummaryrefslogtreecommitdiff
path: root/gabor.cpp
diff options
context:
space:
mode:
authorFranklin Wei <franklin@rockbox.org>2019-12-12 23:59:43 -0500
committerFranklin Wei <franklin@rockbox.org>2019-12-12 23:59:43 -0500
commit24e7095d3a8bcf907393c1e48d2e337ddf8796b4 (patch)
tree8662b878ec3670e4970aba55521ffb7c26ffb803 /gabor.cpp
parent2cf36d440812c33f07d90c251ec5c736f17a7fe9 (diff)
downloadsloreg-24e7095d3a8bcf907393c1e48d2e337ddf8796b4.zip
sloreg-24e7095d3a8bcf907393c1e48d2e337ddf8796b4.tar.gz
sloreg-24e7095d3a8bcf907393c1e48d2e337ddf8796b4.tar.bz2
sloreg-24e7095d3a8bcf907393c1e48d2e337ddf8796b4.tar.xz
Code cleanup
Diffstat (limited to 'gabor.cpp')
-rw-r--r--gabor.cpp72
1 files changed, 37 insertions, 35 deletions
diff --git a/gabor.cpp b/gabor.cpp
index e79af7a..400257e 100644
--- a/gabor.cpp
+++ b/gabor.cpp
@@ -1,3 +1,11 @@
+/***************************************************************************
+ *
+ * Real-time SLO image registration
+ *
+ * Copyright (C) 2019 Franklin Wei
+ *
+ ****************************************************************************/
+
#include <algorithm>
#include <cassert>
#include <cmath>
@@ -19,6 +27,9 @@ Mat contrastLUT(unsigned char thresh) {
}
#endif
+// uncomment for headless mode -- for benchmarking
+//#define imshow(a, b)
+
// parameters were lifted off the internet
Mat maximalGaborFilter(Mat in, int nfilts = 10,
int ksize = 9,
@@ -226,23 +237,11 @@ void floodFill(int x, int y, Mat map, Mat visited, unsigned char target, unsigne
q.push(pair<int, int>(xp, yp));
}
- //imshow("progress", visited);
- //waitKey(1);
+#if 0
+ imshow("progress", visited);
+ waitKey(1);
+#endif
}
-
- /*
- broken DFS
- */
- /*
- if(!inBounds(x, y, map.size()) || !visited.at<unsigned char>(y, x) || map.at<unsigned char>(y, x))
- return;
- visited.at<unsigned char>(y, x) = 0;
- floodFill(x + 1, y, map, visited);
- floodFill(x - 1, y, map, visited);
- floodFill(x, y + 1, map, visited);
- floodFill(x, y - 1, map, visited);
- */
-
}
// flood fill in from the border to isolate the center region
@@ -270,6 +269,7 @@ Mat cleanMask(Mat initial) {
// flood fill from the four boundaries to get the region
// we want
borderFlood(initial, mask);
+
//return mask;
// now to get rid of disconnected blobs we flood fill from
@@ -282,6 +282,11 @@ Mat cleanMask(Mat initial) {
return mask2;
}
+void dumpHist(int *hist) {
+ for(int i = 0; i < 256; i+=1)
+ cout << setw(4) << i << " " << hist[i] << endl;
+}
+
int main()
{
const int nfilts = 10;
@@ -298,7 +303,7 @@ int main()
resize(img_c3, img_c3, Size(512, 512));
- // grayscale
+ // grayscale conversion
Mat img;
cvtColor(img_c3, img, COLOR_BGR2GRAY);
@@ -306,59 +311,56 @@ int main()
Mat inverted = Scalar::all(255) - img;
- // alpha and beta values boost contrast
+ // convert to float
inverted.convertTo(inverted, CV_32F, 1.0, 0);
normalize(inverted, inverted, 0, 1, NORM_MINMAX);
- /*
-
- for(int i = 5; i < 15; i += 2)
- {
- Mat result = maximalGaborFilter(inverted, 10, i);
-
- imshow(string("filtered") + to_string(i), result);
- }
- */
+ // Gabor filtering
Mat result = summedGaborFilter(inverted, nfilts, 5, 13, 2);
result.convertTo(result, CV_8UC1, -255, 255); // uninvert while we're at it
imshow("filtered", result);
- // calculate and display histogram
+ // calculate histogram
int hist[256];
int ymax = getHist(result, hist);
- for(int i = 0; i < 256; i+=1)
- cout << setw(4) << i << " " << hist[i] << endl;
+ //dumpHist(hist);
+ // blur filtered result
Mat blurred;
GaussianBlur(result, blurred, Size(5, 5), 0, 0);
+
+ // threshold out background
Mat threshed;
-// double t = threshold(blurred, threshed, 0, 255, THRESH_BINARY | THRESH_OTSU);
-// imshow("thresholded", threshed);
+
+ // Otsu's method doesn't work -- too high of a threshold
+ //double t = threshold(blurred, threshed, 0, 255, THRESH_BINARY | THRESH_OTSU);
+ //imshow("thresholded", threshed);
//cout << "Otsu threshold: " << t << endl;
int t = getThreshold(hist);
cout << "Our threshold: " << t << endl;
+ // get initial mask by thresholding the filtered image
threshold(blurred, threshed, t, 255, THRESH_BINARY);
imshow("thresholded", threshed);
+ // clean up mask -- remove islands and holes
Mat mask = cleanMask(threshed);
imshow("mask", mask);
+ // if you want a magenta background
//Mat masked = Mat(mask.size(), CV_8UC3, Scalar(0xff, 0, 0xff));
Mat masked;
img.copyTo(masked, mask);
imshow("extracted", masked);
+ // plot histogram
Mat histplot = plotHist(hist, ymax, t);
imshow("hist", histplot);
-
- //calcHist(&result, 1, &chans, Mat(), hist, 2, &histSize, &histRange, true, false);
-
waitKey(0);
}
}