Commit 09893704 authored by Davis King's avatar Davis King

Added apply_random_color_offset()

parent a758fb09
......@@ -92,6 +92,63 @@ namespace dlib
}
}
// ----------------------------------------------------------------------------------------
template <typename image_type>
void apply_random_color_offset (
image_type& img_,
dlib::rand& rnd
)
{
// Make a random color offset. This tform matrix came from looking at the
// covariance matrix of RGB values in a bunch of images. In particular, if you
// multiply Gaussian random vectors by tform it will result in vectors with the
// same covariance matrix as the original RGB data. Also, this color transform is
// what is suggested by the paper:
// Krizhevsky, Alex, Ilya Sutskever, and Geoffrey E. Hinton. "Imagenet
// classification with deep convolutional neural networks." Advances in neural
// information processing systems. 2012.
// Except that we used the square root of the eigenvalues (which I'm pretty sure is
// what the authors intended).
matrix<double,3,3> tform;
tform = -66.379, 25.094, 6.79698,
-68.0492, -0.302309, -13.9539,
-68.4907, -24.0199, 7.27653;
matrix<double,3,1> v;
v = rnd.get_random_gaussian(),rnd.get_random_gaussian(),rnd.get_random_gaussian();
v = round(tform*0.1*v);
const int roffset = v(0);
const int goffset = v(1);
const int boffset = v(2);
// Make up lookup tables that apply the color mapping so we don't have to put a
// bunch of complicated conditional branches in the loop below.
unsigned char rtable[256];
unsigned char gtable[256];
unsigned char btable[256];
for (int i = 0; i < 256; ++i)
{
rtable[i] = put_in_range(0, 255, i+roffset);
gtable[i] = put_in_range(0, 255, i+goffset);
btable[i] = put_in_range(0, 255, i+boffset);
}
// now transform the image.
image_view<image_type> img(img_);
for (long r = 0; r < img.nr(); ++r)
{
for (long c = 0; c < img.nc(); ++c)
{
rgb_pixel temp;
assign_pixel(temp, img[r][c]);
temp.red = rtable[temp.red];
temp.green = rtable[temp.green];
temp.blue = rtable[temp.blue];
assign_pixel(img[r][c], temp);
}
}
}
// ----------------------------------------------------------------------------------------
}
......
......@@ -71,7 +71,24 @@ namespace dlib
// ----------------------------------------------------------------------------------------
}
template <typename image_type>
void apply_random_color_offset (
image_type& img,
dlib::rand& rnd
);
/*!
ensures
- Picks a random color offset vector and adds it to the given image. The offset
vector is selected using the method described in the paper:
Krizhevsky, Alex, Ilya Sutskever, and Geoffrey E. Hinton. "Imagenet
classification with deep convolutional neural networks." Advances in neural
information processing systems. 2012.
In particular, we sample an RGB value from the typical distribution of RGB
values, assuming it has a Gaussian distribution, and then divide it by 10.
This sampled RGB vector is added to each pixel of img.
!*/
// ----------------------------------------------------------------------------------------
#endif // DLIB_RANDOM_cOLOR_TRANSFORM_ABSTRACT_Hh_
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