Commit 183a3de5 authored by Davis King's avatar Davis King

Added set_aspect_ratio()

parent bff364dc
...@@ -602,6 +602,41 @@ namespace dlib ...@@ -602,6 +602,41 @@ namespace dlib
return rectangle(x, y, x+rect.width()-1, y+rect.height()-1); return rectangle(x, y, x+rect.width()-1, y+rect.height()-1);
} }
// ----------------------------------------------------------------------------------------
inline rectangle set_aspect_ratio (
const rectangle& rect,
double ratio
)
{
DLIB_ASSERT(ratio > 0,
"\t rectangle set_aspect_ratio()"
<< "\n\t ratio: " << ratio
<< "\n\t this: " << this
);
// aspect ratio is w/h
// we need to find the rectangle that is nearest to rect in area but
// with an aspect ratio of ratio.
// w/h == ratio
// w*h == rect.area()
if (ratio >= 1)
{
const long h = static_cast<long>(std::sqrt(rect.area()/ratio) + 0.5);
const long w = static_cast<long>(h*ratio + 0.5);
return centered_rect(rect, w, h);
}
else
{
const long w = static_cast<long>(std::sqrt(rect.area()*ratio) + 0.5);
const long h = static_cast<long>(w/ratio + 0.5);
return centered_rect(rect, w, h);
}
}
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
template < template <
......
...@@ -477,6 +477,24 @@ namespace dlib ...@@ -477,6 +477,24 @@ namespace dlib
and height) and height)
!*/ !*/
// ----------------------------------------------------------------------------------------
inline rectangle set_aspect_ratio (
const rectangle& rect,
double ratio
);
/*!
requires
- ratio > 0
ensures
- This function reshapes the given rectangle so that it has the given aspect
ratio. In particular, this means we return a rectangle R such that the
following equations are as true as possible:
- R.width()/R.height() == ratio
- R.area() == rect.area()
- center(rect) == center(R)
!*/
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
inline rectangle intersect ( inline rectangle intersect (
......
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