Commit 74042bf6 authored by Davis King's avatar Davis King

Cleaned up hysteresis_threshold() code a bit and also removed the hard coded

limit on pixel chain recursion depth.
parent 66faa30e
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "thresholding_abstract.h" #include "thresholding_abstract.h"
#include "equalize_histogram.h" #include "equalize_histogram.h"
#include "../enable_if.h" #include "../enable_if.h"
#include <vector>
namespace dlib namespace dlib
{ {
...@@ -594,9 +595,8 @@ namespace dlib ...@@ -594,9 +595,8 @@ namespace dlib
out_img.set_size(in_img.nr(),in_img.nc()); out_img.set_size(in_img.nr(),in_img.nc());
assign_all_pixels(out_img, off_pixel); assign_all_pixels(out_img, off_pixel);
const long size = 1000; std::vector<std::pair<long,long>> stack;
long rstack[size]; using std::make_pair;
long cstack[size];
// now do the thresholding // now do the thresholding
for (long r = 0; r < in_img.nr(); ++r) for (long r = 0; r < in_img.nr(); ++r)
...@@ -609,15 +609,13 @@ namespace dlib ...@@ -609,15 +609,13 @@ namespace dlib
{ {
// now do line following for pixels >= lower_thresh. // now do line following for pixels >= lower_thresh.
// set the stack position to 0. // set the stack position to 0.
long pos = 1; stack.push_back(make_pair(r,c));
rstack[0] = r;
cstack[0] = c;
while (pos > 0) while (stack.size() > 0)
{ {
--pos; const long r = stack.back().first;
const long r = rstack[pos]; const long c = stack.back().second;
const long c = cstack[pos]; stack.pop_back();
// This is the base case of our recursion. We want to stop if we hit a // This is the base case of our recursion. We want to stop if we hit a
// pixel we have already visited. // pixel we have already visited.
...@@ -629,63 +627,30 @@ namespace dlib ...@@ -629,63 +627,30 @@ namespace dlib
// put the neighbors of this pixel on the stack if they are bright enough // put the neighbors of this pixel on the stack if they are bright enough
if (r-1 >= 0) if (r-1 >= 0)
{ {
if (pos < size && get_pixel_intensity(in_img[r-1][c]) >= lower_thresh) if (get_pixel_intensity(in_img[r-1][c]) >= lower_thresh)
{ stack.push_back(make_pair(r-1, c));
rstack[pos] = r-1; if (c-1 >= 0 && get_pixel_intensity(in_img[r-1][c-1]) >= lower_thresh)
cstack[pos] = c; stack.push_back(make_pair(r-1, c-1));
++pos; if (c+1 < in_img.nc() && get_pixel_intensity(in_img[r-1][c+1]) >= lower_thresh)
} stack.push_back(make_pair(r-1, c+1));
if (pos < size && c-1 >= 0 && get_pixel_intensity(in_img[r-1][c-1]) >= lower_thresh)
{
rstack[pos] = r-1;
cstack[pos] = c-1;
++pos;
}
if (pos < size && c+1 < in_img.nc() && get_pixel_intensity(in_img[r-1][c+1]) >= lower_thresh)
{
rstack[pos] = r-1;
cstack[pos] = c+1;
++pos;
}
} }
if (pos < size && c-1 >= 0 && get_pixel_intensity(in_img[r][c-1]) >= lower_thresh) if (c-1 >= 0 && get_pixel_intensity(in_img[r][c-1]) >= lower_thresh)
{ stack.push_back(make_pair(r,c-1));
rstack[pos] = r; if (c+1 < in_img.nc() && get_pixel_intensity(in_img[r][c+1]) >= lower_thresh)
cstack[pos] = c-1; stack.push_back(make_pair(r,c+1));
++pos;
}
if (pos < size && c+1 < in_img.nc() && get_pixel_intensity(in_img[r][c+1]) >= lower_thresh)
{
rstack[pos] = r;
cstack[pos] = c+1;
++pos;
}
if (r+1 < in_img.nr()) if (r+1 < in_img.nr())
{ {
if (pos < size && get_pixel_intensity(in_img[r+1][c]) >= lower_thresh) if (get_pixel_intensity(in_img[r+1][c]) >= lower_thresh)
{ stack.push_back(make_pair(r+1,c));
rstack[pos] = r+1; if (c-1 >= 0 && get_pixel_intensity(in_img[r+1][c-1]) >= lower_thresh)
cstack[pos] = c; stack.push_back(make_pair(r+1,c-1));
++pos; if (c+1 < in_img.nc() && get_pixel_intensity(in_img[r+1][c+1]) >= lower_thresh)
} stack.push_back(make_pair(r+1,c+1));
if (pos < size && c-1 >= 0 && get_pixel_intensity(in_img[r+1][c-1]) >= lower_thresh)
{
rstack[pos] = r+1;
cstack[pos] = c-1;
++pos;
}
if (pos < size && c+1 < in_img.nc() && get_pixel_intensity(in_img[r+1][c+1]) >= lower_thresh)
{
rstack[pos] = r+1;
cstack[pos] = c+1;
++pos;
}
} }
} // end while (pos >= 0) } // end while (stack.size() > 0)
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment