Commit 200ee395 authored by Davis King's avatar Davis King
Browse files

Updated the block spatial filtering functions so they can operate on

rectangular areas instead of just square areas.
parent 45fd6ea5
...@@ -105,12 +105,13 @@ namespace dlib ...@@ -105,12 +105,13 @@ namespace dlib
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
template < template <
long size, long NR,
long NC,
typename T, typename T,
typename in_image_type typename in_image_type
> >
inline void separable_3x3_filter_block_grayscale ( inline void separable_3x3_filter_block_grayscale (
T (&block)[size][size], T (&block)[NR][NC],
const in_image_type& img, const in_image_type& img,
const long& r, const long& r,
const long& c, const long& c,
...@@ -121,19 +122,19 @@ namespace dlib ...@@ -121,19 +122,19 @@ namespace dlib
{ {
// make sure requires clause is not broken // make sure requires clause is not broken
DLIB_ASSERT(shrink_rect(get_rect(img),1).contains(c,r) && DLIB_ASSERT(shrink_rect(get_rect(img),1).contains(c,r) &&
shrink_rect(get_rect(img),1).contains(c+size-1,r+size-1), shrink_rect(get_rect(img),1).contains(c+NC-1,r+NR-1),
"\t void separable_3x3_filter_block_grayscale()" "\t void separable_3x3_filter_block_grayscale()"
<< "\n\t The sub-window doesn't fit inside the given image." << "\n\t The sub-window doesn't fit inside the given image."
<< "\n\t get_rect(img): " << get_rect(img) << "\n\t get_rect(img): " << get_rect(img)
<< "\n\t (c,r): " << point(c,r) << "\n\t (c,r): " << point(c,r)
<< "\n\t (c+size-1,r+size-1): " << point(c+size-1,r+size-1) << "\n\t (c+NC-1,r+NR-1): " << point(c+NC-1,r+NR-1)
); );
T row_filt[size+2][size]; T row_filt[NR+2][NC];
for (long rr = 0; rr < size+2; ++rr) for (long rr = 0; rr < NR+2; ++rr)
{ {
for (long cc = 0; cc < size; ++cc) for (long cc = 0; cc < NC; ++cc)
{ {
row_filt[rr][cc] = get_pixel_intensity(img[r+rr-1][c+cc-1])*fe1 + row_filt[rr][cc] = get_pixel_intensity(img[r+rr-1][c+cc-1])*fe1 +
get_pixel_intensity(img[r+rr-1][c+cc])*fm + get_pixel_intensity(img[r+rr-1][c+cc])*fm +
...@@ -141,9 +142,9 @@ namespace dlib ...@@ -141,9 +142,9 @@ namespace dlib
} }
} }
for (long rr = 0; rr < size; ++rr) for (long rr = 0; rr < NR; ++rr)
{ {
for (long cc = 0; cc < size; ++cc) for (long cc = 0; cc < NC; ++cc)
{ {
block[rr][cc] = (row_filt[rr][cc]*fe1 + block[rr][cc] = (row_filt[rr][cc]*fe1 +
row_filt[rr+1][cc]*fm + row_filt[rr+1][cc]*fm +
...@@ -156,13 +157,14 @@ namespace dlib ...@@ -156,13 +157,14 @@ namespace dlib
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
template < template <
long size, long NR,
long NC,
typename T, typename T,
typename U, typename U,
typename in_image_type typename in_image_type
> >
inline void separable_3x3_filter_block_rgb ( inline void separable_3x3_filter_block_rgb (
T (&block)[size][size], T (&block)[NR][NC],
const in_image_type& img, const in_image_type& img,
const long& r, const long& r,
const long& c, const long& c,
...@@ -173,18 +175,18 @@ namespace dlib ...@@ -173,18 +175,18 @@ namespace dlib
{ {
// make sure requires clause is not broken // make sure requires clause is not broken
DLIB_ASSERT(shrink_rect(get_rect(img),1).contains(c,r) && DLIB_ASSERT(shrink_rect(get_rect(img),1).contains(c,r) &&
shrink_rect(get_rect(img),1).contains(c+size-1,r+size-1), shrink_rect(get_rect(img),1).contains(c+NC-1,r+NR-1),
"\t void separable_3x3_filter_block_grayscale()" "\t void separable_3x3_filter_block_grayscale()"
<< "\n\t The sub-window doesn't fit inside the given image." << "\n\t The sub-window doesn't fit inside the given image."
<< "\n\t get_rect(img): " << get_rect(img) << "\n\t get_rect(img): " << get_rect(img)
<< "\n\t (c,r): " << point(c,r) << "\n\t (c,r): " << point(c,r)
<< "\n\t (c+size-1,r+size-1): " << point(c+size-1,r+size-1) << "\n\t (c+NC-1,r+NR-1): " << point(c+NC-1,r+NR-1)
); );
T row_filt[size+2][size]; T row_filt[NR+2][NC];
for (long rr = 0; rr < size+2; ++rr) for (long rr = 0; rr < NR+2; ++rr)
{ {
for (long cc = 0; cc < size; ++cc) for (long cc = 0; cc < NC; ++cc)
{ {
row_filt[rr][cc].red = img[r+rr-1][c+cc-1].red*fe1 + img[r+rr-1][c+cc].red*fm + img[r+rr-1][c+cc+1].red*fe2; row_filt[rr][cc].red = img[r+rr-1][c+cc-1].red*fe1 + img[r+rr-1][c+cc].red*fm + img[r+rr-1][c+cc+1].red*fe2;
row_filt[rr][cc].green = img[r+rr-1][c+cc-1].green*fe1 + img[r+rr-1][c+cc].green*fm + img[r+rr-1][c+cc+1].green*fe2; row_filt[rr][cc].green = img[r+rr-1][c+cc-1].green*fe1 + img[r+rr-1][c+cc].green*fm + img[r+rr-1][c+cc+1].green*fe2;
...@@ -192,9 +194,9 @@ namespace dlib ...@@ -192,9 +194,9 @@ namespace dlib
} }
} }
for (long rr = 0; rr < size; ++rr) for (long rr = 0; rr < NR; ++rr)
{ {
for (long cc = 0; cc < size; ++cc) for (long cc = 0; cc < NC; ++cc)
{ {
block[rr][cc].red = row_filt[rr][cc].red*fe1 + row_filt[rr+1][cc].red*fm + row_filt[rr+2][cc].red*fe2; block[rr][cc].red = row_filt[rr][cc].red*fe1 + row_filt[rr+1][cc].red*fm + row_filt[rr+2][cc].red*fe2;
block[rr][cc].green = row_filt[rr][cc].green*fe1 + row_filt[rr+1][cc].green*fm + row_filt[rr+2][cc].green*fe2; block[rr][cc].green = row_filt[rr][cc].green*fe1 + row_filt[rr+1][cc].green*fm + row_filt[rr+2][cc].green*fe2;
......
...@@ -56,12 +56,13 @@ namespace dlib ...@@ -56,12 +56,13 @@ namespace dlib
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
template < template <
long size, long NR,
long NC,
typename T, typename T,
typename in_image_type typename in_image_type
> >
inline void separable_3x3_filter_block_grayscale ( inline void separable_3x3_filter_block_grayscale (
T (&block)[size][size], T (&block)[NR][NC],
const in_image_type& img, const in_image_type& img,
const long& r, const long& r,
const long& c, const long& c,
...@@ -74,10 +75,10 @@ namespace dlib ...@@ -74,10 +75,10 @@ namespace dlib
- in_image_type == is an implementation of array2d/array2d_kernel_abstract.h - in_image_type == is an implementation of array2d/array2d_kernel_abstract.h
- T should be a scalar type - T should be a scalar type
- shrink_rect(get_rect(img),1).contains(c,r) - shrink_rect(get_rect(img),1).contains(c,r)
- shrink_rect(get_rect(img),1).contains(c+size-1,r+size-1) - shrink_rect(get_rect(img),1).contains(c+NC-1,r+NR-1)
ensures ensures
- Filters the image in the sub-window of img defined by a rectangle - Filters the image in the sub-window of img defined by a rectangle
with its upper left corner at (c,r) and lower right at (c+size-1,r+size-1). with its upper left corner at (c,r) and lower right at (c+NC-1,r+NR-1).
- The output of the filter is stored in #block. Note that img will be - The output of the filter is stored in #block. Note that img will be
interpreted as a grayscale image. interpreted as a grayscale image.
- The filter used is defined by the separable filter [fe1 fm fe2]. So the - The filter used is defined by the separable filter [fe1 fm fe2]. So the
...@@ -90,13 +91,14 @@ namespace dlib ...@@ -90,13 +91,14 @@ namespace dlib
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
template < template <
long size, long NR,
long NC,
typename T, typename T,
typename U, typename U,
typename in_image_type typename in_image_type
> >
inline void separable_3x3_filter_block_rgb ( inline void separable_3x3_filter_block_rgb (
T (&block)[size][size], T (&block)[NR][NC],
const in_image_type& img, const in_image_type& img,
const long& r, const long& r,
const long& c, const long& c,
...@@ -111,10 +113,10 @@ namespace dlib ...@@ -111,10 +113,10 @@ namespace dlib
- T should be a struct with .red .green and .blue members. - T should be a struct with .red .green and .blue members.
- U should be a scalar type - U should be a scalar type
- shrink_rect(get_rect(img),1).contains(c,r) - shrink_rect(get_rect(img),1).contains(c,r)
- shrink_rect(get_rect(img),1).contains(c+size-1,r+size-1) - shrink_rect(get_rect(img),1).contains(c+NC-1,r+NR-1)
ensures ensures
- Filters the image in the sub-window of img defined by a rectangle - Filters the image in the sub-window of img defined by a rectangle
with its upper left corner at (c,r) and lower right at (c+size-1,r+size-1). with its upper left corner at (c,r) and lower right at (c+NC-1,r+NR-1).
- The output of the filter is stored in #block. Note that the filter is applied - The output of the filter is stored in #block. Note that the filter is applied
to each color component independently. to each color component independently.
- The filter used is defined by the separable filter [fe1 fm fe2]. So the - The filter used is defined by the separable filter [fe1 fm fe2]. So the
......
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