aboutsummaryrefslogtreecommitdiff
path: root/gabor.cpp
diff options
context:
space:
mode:
authorFranklin Wei <franklin@rockbox.org>2019-12-11 23:50:53 -0500
committerFranklin Wei <franklin@rockbox.org>2019-12-11 23:50:53 -0500
commit2cf36d440812c33f07d90c251ec5c736f17a7fe9 (patch)
treee4f6f4d12d2780f7f23f4c636948755c088e3f92 /gabor.cpp
parent3174c6d75e02103302576d64f9c9ac08a1dfe55a (diff)
downloadsloreg-2cf36d440812c33f07d90c251ec5c736f17a7fe9.zip
sloreg-2cf36d440812c33f07d90c251ec5c736f17a7fe9.tar.gz
sloreg-2cf36d440812c33f07d90c251ec5c736f17a7fe9.tar.bz2
sloreg-2cf36d440812c33f07d90c251ec5c736f17a7fe9.tar.xz
Add a second step to mask cleaning
We now flood fill from the inside to remove disconnected regions.
Diffstat (limited to 'gabor.cpp')
-rw-r--r--gabor.cpp57
1 files changed, 41 insertions, 16 deletions
diff --git a/gabor.cpp b/gabor.cpp
index 7265d7a..e79af7a 100644
--- a/gabor.cpp
+++ b/gabor.cpp
@@ -151,6 +151,7 @@ int getThreshold(const int *hist) {
}
return -1;
#else
+ // use this version
int peak_proms[512];
getPeakProminences(hist, peak_proms);
qsort(peak_proms, 256, sizeof(int) * 2, compare_pair);
@@ -199,11 +200,12 @@ bool inBounds(int x, int y, Size sz) {
#define ARRAYLEN(x) (sizeof(x) / sizeof(x[0]))
-void floodFill(int x, int y, Mat map, Mat visited) {
+void floodFill(int x, int y, Mat map, Mat visited, unsigned char target, unsigned char replace) {
queue<pair<int, int> > q;
q.push(pair<int, int>(x, y));
+ // BFS
while(q.size() > 0) {
pair<int, int> p = q.front();
q.pop();
@@ -211,9 +213,11 @@ void floodFill(int x, int y, Mat map, Mat visited) {
y = p.second;
if(!inBounds(x, y, map.size()))
continue;
- if(!visited.at<char>(y, x) || map.at<char>(y, x))
+
+ if(visited.at<unsigned char>(y, x) == replace || map.at<unsigned char>(y, x) == target)
continue;
- visited.at<char>(y, x) = 0;
+
+ visited.at<unsigned char>(y, x) = replace;
int delts[] = { -1, 0, 1, 0, 0, -1, 0, 1 };
for(int i = 0; i < ARRAYLEN(delts); i += 2) {
@@ -221,9 +225,15 @@ void floodFill(int x, int y, Mat map, Mat visited) {
yp = y + delts[i + 1];
q.push(pair<int, int>(xp, yp));
}
+
+ //imshow("progress", visited);
+ //waitKey(1);
}
/*
+ 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;
@@ -233,24 +243,45 @@ void floodFill(int x, int y, Mat map, Mat visited) {
floodFill(x, y - 1, map, visited);
*/
- //imshow("progress", visited);
- //waitKey(1);
}
+// flood fill in from the border to isolate the center region
// in visited, != 0 means not visited, 0 means visited
void borderFlood(Mat map, Mat visited) {
Size sz = map.size();
int w = sz.width, h = sz.height;
for(int x = 0; x < w; x++) {
- floodFill(x, 0, map, visited);
- floodFill(x, h - 1, map, visited);
+ floodFill(x, 0, map, visited, 255, 0);
+ floodFill(x, h - 1, map, visited, 255, 0);
}
for(int y = 1; y < h - 1; y++) {
- floodFill(0, y, map, visited);
- floodFill(w - 1, y, map, visited);
+ floodFill(0, y, map, visited, 255, 0);
+ floodFill(w - 1, y, map, visited, 255, 0);
}
}
+Mat cleanMask(Mat initial) {
+ // now filter the thresholded mask to extract the region
+ // we care about
+ int h = initial.size().height, w = initial.size().width;
+
+ Mat mask = Mat(h, w, CV_8U, Scalar(255));
+
+ // 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
+ // the center (for now we assume the center point is in
+ // the largest region -- this can be changed with a "real"
+ // algorithm to find the largest connected region)
+ Mat mask2 = Mat(h, w, CV_8U, Scalar(0));
+ floodFill(w / 2, h / 2, mask, mask2, 0, 255);
+
+ return mask2;
+}
+
int main()
{
const int nfilts = 10;
@@ -312,13 +343,7 @@ int main()
threshold(blurred, threshed, t, 255, THRESH_BINARY);
imshow("thresholded", threshed);
- // now filter the thresholded mask to extract the region
- // we care about
- Mat mask = Mat(threshed.size().height, threshed.size().width, CV_8U, Scalar(0xff));
-
- // flood fill from the four boundaries to get the region
- // we want
- borderFlood(threshed, mask);
+ Mat mask = cleanMask(threshed);
imshow("mask", mask);