Commit 18ec043d authored by Davis King's avatar Davis King

Upgraded the pyramid_down function object so it can create color

pyramids.  Not just grayscale ones.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%404240
parent a6e0ceea
......@@ -93,11 +93,20 @@ namespace dlib
// -----------------------------
private:
template <typename T, typename U>
struct both_images_rgb
{
const static bool value = pixel_traits<typename T::type>::rgb &&
pixel_traits<typename U::type>::rgb;
};
public:
template <
typename in_image_type,
typename out_image_type
>
void operator() (
typename disable_if<both_images_rgb<in_image_type,out_image_type> >::type operator() (
const in_image_type& original,
out_image_type& down
) const
......@@ -122,7 +131,7 @@ namespace dlib
down.set_size((original.nr()-3)/2, (original.nc()-3)/2);
// This function applies a 5x5 gaussian filter to the image. It
// This function applies a 5x5 Gaussian filter to the image. It
// does this by separating the filter into its horizontal and vertical
// components and then downsamples the image by dropping every other
// row and column. Note that we can do these things all together in
......@@ -175,6 +184,134 @@ namespace dlib
}
private:
struct rgbptype
{
uint16 red;
uint16 green;
uint16 blue;
};
public:
// ------------------------------------------
// OVERLOAD FOR RGB TO RGB IMAGES
// ------------------------------------------
template <
typename in_image_type,
typename out_image_type
>
typename enable_if<both_images_rgb<in_image_type,out_image_type> >::type operator() (
const in_image_type& original,
out_image_type& down
) const
{
// make sure requires clause is not broken
DLIB_ASSERT(original.nr() > 10 && original.nc() > 10 &&
is_same_object(original, down) == false,
"\t void pyramid_down::operator()"
<< "\n\t original.nr(): " << original.nr()
<< "\n\t original.nc(): " << original.nc()
<< "\n\t is_same_object(original, down): " << is_same_object(original, down)
<< "\n\t this: " << this
);
COMPILE_TIME_ASSERT( pixel_traits<typename in_image_type::type>::has_alpha == false );
COMPILE_TIME_ASSERT( pixel_traits<typename out_image_type::type>::has_alpha == false );
array2d<rgbptype>::kernel_1a temp_img;
temp_img.set_size(original.nr(), (original.nc()-3)/2);
down.set_size((original.nr()-3)/2, (original.nc()-3)/2);
// This function applies a 5x5 Gaussian filter to the image. It
// does this by separating the filter into its horizontal and vertical
// components and then downsamples the image by dropping every other
// row and column. Note that we can do these things all together in
// one step.
// apply row filter
for (long r = 0; r < temp_img.nr(); ++r)
{
long oc = 0;
for (long c = 0; c < temp_img.nc(); ++c)
{
rgbptype pix1;
rgbptype pix2;
rgbptype pix3;
rgbptype pix4;
rgbptype pix5;
pix1.red = original[r][oc].red;
pix2.red = original[r][oc+1].red;
pix3.red = original[r][oc+2].red;
pix4.red = original[r][oc+3].red;
pix5.red = original[r][oc+4].red;
pix1.green = original[r][oc].green;
pix2.green = original[r][oc+1].green;
pix3.green = original[r][oc+2].green;
pix4.green = original[r][oc+3].green;
pix5.green = original[r][oc+4].green;
pix1.blue = original[r][oc].blue;
pix2.blue = original[r][oc+1].blue;
pix3.blue = original[r][oc+2].blue;
pix4.blue = original[r][oc+3].blue;
pix5.blue = original[r][oc+4].blue;
pix2.red *= 4;
pix3.red *= 6;
pix4.red *= 4;
pix2.green *= 4;
pix3.green *= 6;
pix4.green *= 4;
pix2.blue *= 4;
pix3.blue *= 6;
pix4.blue *= 4;
rgbptype temp;
temp.red = pix1.red + pix2.red + pix3.red + pix4.red + pix5.red;
temp.green = pix1.green + pix2.green + pix3.green + pix4.green + pix5.green;
temp.blue = pix1.blue + pix2.blue + pix3.blue + pix4.blue + pix5.blue;
temp_img[r][c] = temp;
oc += 2;
}
}
// apply column filter
long dr = 0;
for (long r = 2; r < temp_img.nr()-2; r += 2)
{
for (long c = 0; c < temp_img.nc(); ++c)
{
rgbptype temp;
temp.red = temp_img[r-2][c].red +
temp_img[r-1][c].red*4 +
temp_img[r ][c].red*6 +
temp_img[r-1][c].red*4 +
temp_img[r-2][c].red;
temp.green = temp_img[r-2][c].green +
temp_img[r-1][c].green*4 +
temp_img[r ][c].green*6 +
temp_img[r-1][c].green*4 +
temp_img[r-2][c].green;
temp.blue = temp_img[r-2][c].blue +
temp_img[r-1][c].blue*4 +
temp_img[r ][c].blue*6 +
temp_img[r-1][c].blue*4 +
temp_img[r-2][c].blue;
down[dr][c].red = temp.red/256;
down[dr][c].green = temp.green/256;
down[dr][c].blue = temp.blue/256;
}
++dr;
}
}
private:
......
......@@ -49,6 +49,8 @@ namespace dlib
- 1. Applies a 5x5 Gaussian filter to the original image to smooth it a little.
- 2. Every other row and column is discarded to create an image half the size
of the original. This smaller image is stored in #down.
- if both input and output images contain RGB pixels then the downsampled image will
be in color. Otherwise, the downsampling will be performed in a grayscale mode.
- The location of a point P in original image will show up at point point_down(P)
in the #down image.
- Note that some points on the border of the original image will correspond to
......
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