Commit 0072afbc authored by samuarl's avatar samuarl Committed by Davis E. King

Python Image and Rectangle Scaling (#1488)

* added scale_image python binding which returns an image resized by the provided scale factor

* added scale_rect for rectangle and drectangle. returns a rectangle scaled by the provided scaling factor

* - do not cast scale_rect coordinates to long for drectangle
- document scale_rect for drectangle and rectangle in _abstract.h
- change python binding scale_image to resize_image
- update documentation for resize_image to describe behaviour
parent 8234080b
......@@ -441,6 +441,20 @@ namespace dlib
return shrink_rect(rect, -width, -height);
}
inline const drectangle scale_rect (
const drectangle& rect,
double scale
)
{
DLIB_ASSERT(scale > 0, "scale factor must be > 0");
double l = rect.left() * scale;
double t = rect.top() * scale;
double r = rect.right() * scale;
double b = rect.bottom() * scale;
return drectangle(l, t, r, b);
}
inline drectangle set_rect_area (
const drectangle& rect,
double area
......
......@@ -585,6 +585,20 @@ namespace dlib
(i.e. grows the given drectangle by expanding its border)
!*/
// ----------------------------------------------------------------------------------------
const drectangle scale_rect (
const drectangle& rect,
double scale
);
/*!
requires
- scale > 0
ensures
- return drectangle(rect.left() * scale, rect.top() * scale, rect.right() * scale, rect.bottom() * scale)
(i.e. resizes the given rectangle by multiplying all side coordinates with a scale factor)
!*/
// ----------------------------------------------------------------------------------------
drectangle set_rect_area (
......
......@@ -641,6 +641,22 @@ namespace dlib
return shrink_rect(rect, -width, -height);
}
// ----------------------------------------------------------------------------------------
inline const rectangle scale_rect (
const rectangle& rect,
double scale
)
{
DLIB_ASSERT(scale > 0, "scale factor must be > 0");
long l = (long)std::round(rect.left()*scale);
long t = (long)std::round(rect.top()*scale);
long r = (long)std::round(rect.right()*scale);
long b = (long)std::round(rect.bottom()*scale);
return rectangle(l, t, r, b);
}
// ----------------------------------------------------------------------------------------
inline const rectangle translate_rect (
......
......@@ -609,6 +609,20 @@ namespace dlib
(i.e. grows the given rectangle by expanding its border)
!*/
// ----------------------------------------------------------------------------------------
const rectangle scale_rect (
const rectangle& rect,
double scale
);
/*!
requires
- scale > 0
ensures
- return rectangle(rect.left() * scale, rect.top() * scale, rect.right() * scale, rect.bottom() * scale)
(i.e. resizes the given rectangle by multiplying all side coordinates with a scale factor)
!*/
// ----------------------------------------------------------------------------------------
const rectangle translate_rect (
......
......@@ -28,6 +28,20 @@ numpy_image<T> py_resize_image (
// ----------------------------------------------------------------------------------------
template <typename T>
numpy_image<T> py_scale_image (
const numpy_image<T>& img,
double scale
)
{
DLIB_CASSERT(scale > 0, "Scale factor must be greater than 0");
numpy_image<T> out = img;
resize_image(scale, out);
return out;
}
// ----------------------------------------------------------------------------------------
template <typename T>
numpy_image<T> py_equalize_histogram (
const numpy_image<T>& img
......@@ -535,6 +549,15 @@ void bind_image_classes2(py::module& m)
m.def("resize_image", &py_resize_image<float>, py::arg("img"), py::arg("rows"), py::arg("cols"));
m.def("resize_image", &py_resize_image<double>, docs, py::arg("img"), py::arg("rows"), py::arg("cols"));
m.def("resize_image", &py_resize_image<rgb_pixel>, docs, py::arg("img"), py::arg("rows"), py::arg("cols"));
m.def("resize_image", &py_scale_image<int8_t>, py::arg("img"), py::arg("scale"));
m.def("resize_image", &py_scale_image<int16_t>, py::arg("img"), py::arg("scale"));
m.def("resize_image", &py_scale_image<int32_t>, py::arg("img"), py::arg("scale"));
m.def("resize_image", &py_scale_image<int64_t>, py::arg("img"), py::arg("scale"));
m.def("resize_image", &py_scale_image<float>, py::arg("img"), py::arg("scale"));
m.def("resize_image", &py_scale_image<double>, py::arg("img"), py::arg("scale"));
m.def("resize_image", &py_scale_image<rgb_pixel>, py::arg("img"), py::arg("scale"),
"Resizes img, using bilinear interpolation, to have the new size (img rows * scale, img cols * scale)"
);
register_extract_image_chip(m);
......
......@@ -290,6 +290,11 @@ ensures \n\
(i.e. grows the given rectangle by expanding its border by num)",
py::arg("rect"), py::arg("num"));
m.def("scale_rect", [](const rectangle& rect, double scale){return scale_rect(rect,scale);},
"- return scale_rect(rect, scale) \n\
(i.e. resizes the given rectangle by a scale factor)",
py::arg("rect"), py::arg("scale"));
m.def("centered_rect", [](const point& p, unsigned long width, unsigned long height) {
return centered_rect(p, width, height); },
py::arg("p"), py::arg("width"), py::arg("height"));
......
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