Commit 88f8a525 authored by Davis King's avatar Davis King

Moved sum_filter() into the file with all the other filtering routines.

parent 10307342
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "../algs.h" #include "../algs.h"
#include "../rand.h" #include "../rand.h"
#include "../array2d.h" #include "../array2d.h"
#include "../image_transforms/spatial_filtering.h"
namespace dlib namespace dlib
{ {
...@@ -107,100 +108,6 @@ namespace dlib ...@@ -107,100 +108,6 @@ namespace dlib
return static_cast<double>(temp); return static_cast<double>(temp);
} }
// ----------------------------------------------------------------------------------------
template <
typename image_type1,
typename image_type2
>
void sum_filter (
const image_type1& img,
image_type2& out,
const rectangle& rect
)
{
DLIB_ASSERT(img.nr() == out.nr() &&
img.nc() == out.nc(),
"\t void sum_filter()"
<< "\n\t Invalid arguments given to this function."
<< "\n\t img.nr(): " << img.nr()
<< "\n\t img.nc(): " << img.nc()
<< "\n\t out.nr(): " << out.nr()
<< "\n\t out.nc(): " << out.nc()
);
typedef typename image_type1::type pixel_type;
typedef typename promote<pixel_type>::type ptype;
std::vector<ptype> column_sum;
column_sum.resize(img.nc() + rect.width(),0);
const long top = -1 + rect.top();
const long bottom = -1 + rect.bottom();
long left = rect.left()-1;
// initialize column_sum at row -1
for (unsigned long j = 0; j < column_sum.size(); ++j)
{
rectangle strip(left,top,left,bottom);
strip = strip.intersect(get_rect(img));
if (!strip.is_empty())
{
column_sum[j] = sum(matrix_cast<ptype>(subm(array_to_matrix(img),strip)));
}
++left;
}
const rectangle area = get_rect(img);
// save width to avoid computing them over and over
const long width = rect.width();
// Now do the bulk of the scanning work.
for (long r = 0; r < img.nr(); ++r)
{
// set to sum at point(-1,r). i.e. should be equal to sum_of_rects_in_images(images, rects, point(-1,r))
// We compute it's value in the next loop.
ptype cur_sum = 0;
// Update the first part of column_sum since we only work on the c+width part of column_sum
// in the main loop.
const long top = r + rect.top() - 1;
const long bottom = r + rect.bottom();
for (long k = 0; k < width; ++k)
{
const long right = k-width + rect.right();
const ptype br_corner = area.contains(right,bottom) ? img[bottom][right] : 0;
const ptype tr_corner = area.contains(right,top) ? img[top][right] : 0;
// update the sum in this column now that we are on the next row
column_sum[k] = column_sum[k] + br_corner - tr_corner;
cur_sum += column_sum[k];
}
for (long c = 0; c < img.nc(); ++c)
{
const long top = r + rect.top() - 1;
const long bottom = r + rect.bottom();
const long right = c + rect.right();
const ptype br_corner = area.contains(right,bottom) ? img[bottom][right] : 0;
const ptype tr_corner = area.contains(right,top) ? img[top][right] : 0;
// update the sum in this column now that we are on the next row
column_sum[c+width] = column_sum[c+width] + br_corner - tr_corner;
// add in the new right side of the rect and subtract the old right side.
cur_sum = cur_sum + column_sum[c+width] - column_sum[c];
out[r][c] += cur_sum;
}
}
}
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
template < template <
......
...@@ -10,31 +10,6 @@ ...@@ -10,31 +10,6 @@
namespace dlib namespace dlib
{ {
// ----------------------------------------------------------------------------------------
template <
typename image_type1,
typename image_type2
>
void sum_filter (
const image_type1& img,
image_type2& out,
const rectangle& rect
)
/*!
requires
- out.nr() == img.nr()
- out.nc() == img.nc()
- image_type1 == an implementation of array2d/array2d_kernel_abstract.h
and it must contain a scalar type
- image_type2 == an implementation of array2d/array2d_kernel_abstract.h
and it must contain a scalar type
ensures
- for all valid r and c:
- let SUM(r,c) == sum of pixels inside the rectangle translate_rect(rect, point(c,r))
- #out[r][c] == out[r][c] + SUM(r,c)
!*/
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
template < template <
......
...@@ -619,6 +619,100 @@ namespace dlib ...@@ -619,6 +619,100 @@ namespace dlib
} }
// ----------------------------------------------------------------------------------------
template <
typename image_type1,
typename image_type2
>
void sum_filter (
const image_type1& img,
image_type2& out,
const rectangle& rect
)
{
DLIB_ASSERT(img.nr() == out.nr() &&
img.nc() == out.nc(),
"\t void sum_filter()"
<< "\n\t Invalid arguments given to this function."
<< "\n\t img.nr(): " << img.nr()
<< "\n\t img.nc(): " << img.nc()
<< "\n\t out.nr(): " << out.nr()
<< "\n\t out.nc(): " << out.nc()
);
typedef typename image_type1::type pixel_type;
typedef typename promote<pixel_type>::type ptype;
std::vector<ptype> column_sum;
column_sum.resize(img.nc() + rect.width(),0);
const long top = -1 + rect.top();
const long bottom = -1 + rect.bottom();
long left = rect.left()-1;
// initialize column_sum at row -1
for (unsigned long j = 0; j < column_sum.size(); ++j)
{
rectangle strip(left,top,left,bottom);
strip = strip.intersect(get_rect(img));
if (!strip.is_empty())
{
column_sum[j] = sum(matrix_cast<ptype>(subm(array_to_matrix(img),strip)));
}
++left;
}
const rectangle area = get_rect(img);
// save width to avoid computing them over and over
const long width = rect.width();
// Now do the bulk of the scanning work.
for (long r = 0; r < img.nr(); ++r)
{
// set to sum at point(-1,r). i.e. should be equal to sum_of_rects_in_images(images, rects, point(-1,r))
// We compute it's value in the next loop.
ptype cur_sum = 0;
// Update the first part of column_sum since we only work on the c+width part of column_sum
// in the main loop.
const long top = r + rect.top() - 1;
const long bottom = r + rect.bottom();
for (long k = 0; k < width; ++k)
{
const long right = k-width + rect.right();
const ptype br_corner = area.contains(right,bottom) ? img[bottom][right] : 0;
const ptype tr_corner = area.contains(right,top) ? img[top][right] : 0;
// update the sum in this column now that we are on the next row
column_sum[k] = column_sum[k] + br_corner - tr_corner;
cur_sum += column_sum[k];
}
for (long c = 0; c < img.nc(); ++c)
{
const long top = r + rect.top() - 1;
const long bottom = r + rect.bottom();
const long right = c + rect.right();
const ptype br_corner = area.contains(right,bottom) ? img[bottom][right] : 0;
const ptype tr_corner = area.contains(right,top) ? img[top][right] : 0;
// update the sum in this column now that we are on the next row
column_sum[c+width] = column_sum[c+width] + br_corner - tr_corner;
// add in the new right side of the rect and subtract the old right side.
cur_sum = cur_sum + column_sum[c+width] - column_sum[c];
out[r][c] += cur_sum;
}
}
}
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
} }
......
...@@ -311,6 +311,31 @@ namespace dlib ...@@ -311,6 +311,31 @@ namespace dlib
- #out_img.nr() == in_img.nr() - #out_img.nr() == in_img.nr()
!*/ !*/
// ----------------------------------------------------------------------------------------
template <
typename image_type1,
typename image_type2
>
void sum_filter (
const image_type1& img,
image_type2& out,
const rectangle& rect
)
/*!
requires
- out.nr() == img.nr()
- out.nc() == img.nc()
- image_type1 == an implementation of array2d/array2d_kernel_abstract.h
and it must contain a scalar type
- image_type2 == an implementation of array2d/array2d_kernel_abstract.h
and it must contain a scalar type
ensures
- for all valid r and c:
- let SUM(r,c) == sum of pixels inside the rectangle translate_rect(rect, point(c,r))
- #out[r][c] == out[r][c] + SUM(r,c)
!*/
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
} }
......
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