Commit ff82e91c authored by Davis King's avatar Davis King

Added add_image_rotations() and also made add_image_left_right_flips() work

with full_object_detection objects in addition to rectangles.
parent 033cf733
......@@ -9,6 +9,7 @@
#include "assign_image.h"
#include "image_pyramid.h"
#include "../simd.h"
#include "../image_processing/full_object_detection.h"
namespace dlib
{
......@@ -920,14 +921,39 @@ namespace dlib
temp.left() = temp.right()-rect.width()+1;
return temp;
}
inline rectangle tform_object (
const point_transform_affine& tran,
const rectangle& rect
)
{
return centered_rect(tran(center(rect)), rect.width(), rect.height());
}
inline full_object_detection tform_object(
const point_transform_affine& tran,
const full_object_detection& obj
)
{
std::vector<point> parts;
parts.reserve(obj.num_parts());
for (unsigned long i = 0; i < obj.num_parts(); ++i)
{
parts.push_back(tran(obj.part(i)));
}
return full_object_detection(tform_object(tran,obj.get_rect()), parts);
}
}
// ----------------------------------------------------------------------------------------
template <
typename image_type
typename image_type,
typename T
>
void add_image_left_right_flips (
dlib::array<image_type>& images,
std::vector<std::vector<rectangle> >& objects
std::vector<std::vector<T> >& objects
)
{
// make sure requires clause is not broken
......@@ -939,16 +965,16 @@ namespace dlib
);
image_type temp;
std::vector<rectangle> rects;
std::vector<T> rects;
const unsigned long num = images.size();
for (unsigned long j = 0; j < num; ++j)
{
flip_image_left_right(images[j], temp);
const point_transform_affine tran = flip_image_left_right(images[j], temp);
rects.clear();
for (unsigned long i = 0; i < objects[j].size(); ++i)
rects.push_back(impl::flip_rect_left_right(objects[j][i], get_rect(images[j])));
rects.push_back(impl::tform_object(tran, objects[j][i]));
images.push_back(temp);
objects.push_back(rects);
......@@ -958,12 +984,14 @@ namespace dlib
// ----------------------------------------------------------------------------------------
template <
typename image_type
typename image_type,
typename T,
typename U
>
void add_image_left_right_flips (
dlib::array<image_type>& images,
std::vector<std::vector<rectangle> >& objects,
std::vector<std::vector<rectangle> >& objects2
std::vector<std::vector<T> >& objects,
std::vector<std::vector<U> >& objects2
)
{
// make sure requires clause is not broken
......@@ -977,23 +1005,24 @@ namespace dlib
);
image_type temp;
std::vector<rectangle> rects;
std::vector<T> rects;
std::vector<U> rects2;
const unsigned long num = images.size();
for (unsigned long j = 0; j < num; ++j)
{
flip_image_left_right(images[j], temp);
const point_transform_affine tran = flip_image_left_right(images[j], temp);
images.push_back(temp);
rects.clear();
for (unsigned long i = 0; i < objects[j].size(); ++i)
rects.push_back(impl::flip_rect_left_right(objects[j][i], get_rect(images[j])));
rects.push_back(impl::tform_object(tran, objects[j][i]));
objects.push_back(rects);
rects.clear();
rects2.clear();
for (unsigned long i = 0; i < objects2[j].size(); ++i)
rects.push_back(impl::flip_rect_left_right(objects2[j][i], get_rect(images[j])));
objects2.push_back(rects);
rects2.push_back(impl::tform_object(tran, objects2[j][i]));
objects2.push_back(rects2);
}
}
......@@ -1195,6 +1224,84 @@ namespace dlib
}
}
// ----------------------------------------------------------------------------------------
template <
typename image_type,
typename EXP,
typename T,
typename U
>
void add_image_rotations (
const matrix_exp<EXP>& angles,
dlib::array<image_type>& images,
std::vector<std::vector<T> >& objects,
std::vector<std::vector<U> >& objects2
)
{
// make sure requires clause is not broken
DLIB_ASSERT( is_vector(angles) && angles.size() > 0 &&
images.size() == objects.size() &&
images.size() == objects2.size(),
"\t void add_image_rotations()"
<< "\n\t Invalid inputs were given to this function."
<< "\n\t is_vector(angles): " << is_vector(angles)
<< "\n\t angles.size(): " << angles.size()
<< "\n\t images.size(): " << images.size()
<< "\n\t objects.size(): " << objects.size()
<< "\n\t objects2.size(): " << objects2.size()
);
dlib::array<image_type> new_images;
std::vector<std::vector<T> > new_objects;
std::vector<std::vector<U> > new_objects2;
using namespace impl;
std::vector<T> objtemp;
std::vector<U> objtemp2;
image_type temp;
for (long i = 0; i < angles.size(); ++i)
{
for (unsigned long j = 0; j < images.size(); ++j)
{
const point_transform_affine tran = rotate_image(images[j], temp, angles(i));
new_images.push_back(temp);
objtemp.clear();
for (unsigned long k = 0; k < objects[j].size(); ++k)
objtemp.push_back(tform_object(tran, objects[j][k]));
new_objects.push_back(objtemp);
objtemp2.clear();
for (unsigned long k = 0; k < objects2[j].size(); ++k)
objtemp2.push_back(tform_object(tran, objects2[j][k]));
new_objects2.push_back(objtemp2);
}
}
new_images.swap(images);
new_objects.swap(objects);
new_objects2.swap(objects2);
}
// ----------------------------------------------------------------------------------------
template <
typename image_type,
typename EXP,
typename T
>
void add_image_rotations (
const matrix_exp<EXP>& angles,
dlib::array<image_type>& images,
std::vector<std::vector<T> >& objects
)
{
std::vector<std::vector<T> > objects2(objects.size());
add_image_rotations(angles, images, objects, objects2);
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
......
......@@ -4,6 +4,7 @@
#ifdef DLIB_INTERPOlATION_ABSTRACT_
#include "../pixel.h"
#include "../image_processing/full_object_detection_abstract.h"
namespace dlib
{
......@@ -429,16 +430,18 @@ namespace dlib
// ----------------------------------------------------------------------------------------
template <
typename image_type
typename image_type,
typename T
>
void add_image_left_right_flips (
dlib::array<image_type>& images,
std::vector<std::vector<rectangle> >& objects
std::vector<std::vector<T> >& objects
);
/*!
requires
- image_type == is an implementation of array2d/array2d_kernel_abstract.h
- pixel_traits<typename image_type::type> is defined
- T == rectangle or full_object_detection
- images.size() == objects.size()
ensures
- This function computes all the left/right flips of the contents of images and
......@@ -456,12 +459,14 @@ namespace dlib
// ----------------------------------------------------------------------------------------
template <
typename image_type
typename image_type,
typename T,
typename U
>
void add_image_left_right_flips (
dlib::array<image_type>& images,
std::vector<std::vector<rectangle> >& objects,
std::vector<std::vector<rectangle> >& objects2
std::vector<std::vector<T> >& objects,
std::vector<std::vector<U> >& objects2
);
/*!
requires
......@@ -469,6 +474,8 @@ namespace dlib
- pixel_traits<typename image_type::type> is defined
- images.size() == objects.size()
- images.size() == objects2.size()
- T == rectangle or full_object_detection
- U == rectangle or full_object_detection
ensures
- This function computes all the left/right flips of the contents of images and
then appends them onto the end of the images array. It also finds the
......@@ -484,6 +491,71 @@ namespace dlib
That is, this function only appends new elements to each of these containers.
!*/
// ----------------------------------------------------------------------------------------
template <
typename image_type,
typename EXP,
typename T,
typename U
>
void add_image_rotations (
const matrix_exp<EXP>& angles,
dlib::array<image_type>& images,
std::vector<std::vector<T> >& objects,
std::vector<std::vector<U> >& objects2
);
/*!
requires
- is_vector(angles) == true
- angles.size() > 0
- image_type == is an implementation of array2d/array2d_kernel_abstract.h
- pixel_traits<typename image_type::type> is defined
- images.size() == objects.size()
- images.size() == objects2.size()
- T == rectangle or full_object_detection
- U == rectangle or full_object_detection
ensures
- This function computes angles.size() different rotations of all the given
images and then replaces the contents of images with those rotations of the
input dataset. We will also adjust the rectangles inside objects and
objects2 so that they still bound the same objects in the new rotated images.
That is, we assume objects[i] and objects2[i] are bounding boxes for things
in images[i]. So we will adjust the positions of the boxes in objects and
objects2 accordingly.
- The elements of angles are interpreted as angles in radians and we will
rotate the images around their center using the values in angles. Moreover,
the rotation is done counter clockwise.
- #images.size() == images.size()*angles.size()
- #objects.size() == objects.size()*angles.size()
- #objects2.size() == objects2.size()*angles.size()
!*/
// ----------------------------------------------------------------------------------------
template <
typename image_type,
typename EXP,
typename T
>
void add_image_rotations (
const matrix_exp<EXP>& angles,
dlib::array<image_type>& images,
std::vector<std::vector<T> >& objects
);
/*!
requires
- is_vector(angles) == true
- angles.size() > 0
- image_type == is an implementation of array2d/array2d_kernel_abstract.h
- pixel_traits<typename image_type::type> is defined
- images.size() == objects.size()
- T == rectangle or full_object_detection
ensures
- This function is identical to the add_image_rotations() define above except
that it doesn't have objects2 as an argument.
!*/
// ----------------------------------------------------------------------------------------
template <
......
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