Commit 786c93cd authored by Davis King's avatar Davis King

Added rectangle_transform

parent 4de8678b
......@@ -9,6 +9,7 @@
#include "../matrix.h"
#include "../matrix/matrix_la.h"
#include "../optimization/optimization.h"
#include "rectangle.h"
#include <vector>
namespace dlib
......@@ -190,6 +191,74 @@ namespace dlib
dlib::vector<double,2> b;
};
// ----------------------------------------------------------------------------------------
class rectangle_transform
{
public:
rectangle_transform (
)
{
}
rectangle_transform (
const point_transform_affine& tform_
) :tform(tform_)
{
}
drectangle operator() (
const drectangle& r
) const
{
dpoint tl = r.tl_corner();
dpoint tr = r.tr_corner();
dpoint bl = r.bl_corner();
dpoint br = r.br_corner();
// The new rectangle wouold ideally have this area if we could actually rotrate
// the box.
double new_area = length(tform(tl)-tform(tr))*length(tform(tl)-tform(bl));
// But if we rotate the coners of the rectangle and then find the rectangle
// that contains them we get this, which might have a much larger area than we
// want.
drectangle temp;
temp += tform(tl);
temp += tform(tr);
temp += tform(bl);
temp += tform(br);
// so we adjust the area to match the target area and have the same center as
// the above box.
double scale = std::sqrt(new_area/temp.area());
return centered_rect(center(temp), std::round(temp.width()*scale), std::round(temp.height()*scale));
}
rectangle operator() (
const rectangle& r
) const
{
return (*this)(drectangle(r));
}
const point_transform_affine& get_tform(
) const { return tform; }
inline friend void serialize (const rectangle_transform& item, std::ostream& out)
{
serialize(item.tform, out);
}
inline friend void deserialize (rectangle_transform& item, std::istream& in)
{
deserialize(item.tform, in);
}
private:
point_transform_affine tform;
};
// ----------------------------------------------------------------------------------------
inline point_transform_affine operator* (
......
......@@ -5,6 +5,8 @@
#include "../matrix/matrix_abstract.h"
#include "vector_abstract.h"
#include "rectangle_abstract.h"
#include "drectangle_abstract.h"
#include <vector>
namespace dlib
......@@ -146,6 +148,77 @@ namespace dlib
example, an equilateral triangle to turn into an isosceles triangle.
!*/
// ----------------------------------------------------------------------------------------
class rectangle_transform
{
/*!
WHAT THIS OBJECT REPRESENTS
This object is just a point_transform_affine wrapped up so that it can
transform rectangle objects. It will take a rectangle and transform it
according to an affine transformation.
THREAD SAFETY
It is safe for multiple threads to make concurrent accesses to this object
without synchronization.
!*/
public:
rectangle_transform (
);
/*!
ensures
- This object will perform the identity transform. That is, given a rectangle
as input it will return the same rectangle as output.
!*/
rectangle_transform (
const point_transform_affine& tform
);
/*!
ensures
- #get_tform() == tform
!*/
drectangle operator() (
const drectangle& r
) const;
/*!
ensures
- Applies the transformation get_tform() to r and returns the resulting
rectangle. If the transformation doesn't have any rotation then the
transformation simply maps the corners of the rectangle according to
get_tform() and returns the exact result. However, since
dlib::drectangle can't represent rotated rectangles, if there is any
rotation in the affine transform we will attempt to produce the most
faithful possible outputs by ensuring the output rectangle has the
correct center point and that its area and aspect ratio match the correct
rotated rectangle's as much as possible.
!*/
rectangle operator() (
const rectangle& r
) const;
/*!
ensures
- returns (*this)(drectangle(r))
!*/
const point_transform_affine& get_tform(
) const;
/*!
ensures
- returns the affine transformation this object uses to transform rectangles.
!*/
};
void serialize (const rectangle_transform& item, std::ostream& out);
void deserialize (rectangle_transform& item, std::istream& in);
/*!
provides serialization support
!*/
// ----------------------------------------------------------------------------------------
class point_transform_projective
......
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