Commit 5cff6b95 authored by Davis King's avatar Davis King

Made extract_image_chips() much faster when extracting an unscaled and

unrotated chip.  Also added another constructor to chip_details that takes just
a rectangle.
parent ae826a40
...@@ -1426,6 +1426,7 @@ namespace dlib ...@@ -1426,6 +1426,7 @@ namespace dlib
struct chip_details struct chip_details
{ {
chip_details() : angle(0), rows(0), cols(0) {} chip_details() : angle(0), rows(0), cols(0) {}
chip_details(const rectangle& rect_) : rect(rect_),angle(0), rows(rect_.height()), cols(rect_.width()) {}
chip_details(const rectangle& rect_, unsigned long size) : rect(rect_),angle(0) chip_details(const rectangle& rect_, unsigned long size) : rect(rect_),angle(0)
{ compute_dims_from_size(size); } { compute_dims_from_size(size); }
chip_details(const rectangle& rect_, unsigned long size, double angle_) : rect(rect_),angle(angle_) chip_details(const rectangle& rect_, unsigned long size, double angle_) : rect(rect_),angle(angle_)
...@@ -1530,6 +1531,49 @@ namespace dlib ...@@ -1530,6 +1531,49 @@ namespace dlib
return res; return res;
} }
// ----------------------------------------------------------------------------------------
namespace impl
{
template <
typename image_type1,
typename image_type2
>
void basic_extract_image_chip (
const image_type1& img,
const rectangle& location,
image_type2& chip
)
/*!
ensures
- This function doesn't do any scaling or rotating. It just pulls out the
chip in the given rectangle. This also means the output image has the
same dimensions as the location rectangle.
!*/
{
const_image_view<image_type1> vimg(img);
image_view<image_type2> vchip(chip);
vchip.set_size(location.height(), location.width());
// location might go outside img so clip it
rectangle area = location.intersect(get_rect(img));
// find the part of the chip that corresponds to area in img.
rectangle chip_area = translate_rect(area, -location.tl_corner());
zero_border_pixels(chip, chip_area);
// now pull out the contents of area/chip_area.
for (long r = chip_area.top(), rr = area.top(); r <= chip_area.bottom(); ++r,++rr)
{
for (long c = chip_area.left(), cc = area.left(); c <= chip_area.right(); ++c,++cc)
{
assign_pixel(vchip[r][c], vimg[rr][cc]);
}
}
}
}
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
template < template <
...@@ -1588,6 +1632,16 @@ namespace dlib ...@@ -1588,6 +1632,16 @@ namespace dlib
// now pull out the chips // now pull out the chips
chips.resize(chip_locations.size()); chips.resize(chip_locations.size());
for (unsigned long i = 0; i < chips.size(); ++i) for (unsigned long i = 0; i < chips.size(); ++i)
{
// If the chip doesn't have any rotation or scaling then use the basic version
// of chip extraction that just does a fast copy.
if (chip_locations[i].angle == 0 &&
chip_locations[i].rows == chip_locations[i].rect.height() &&
chip_locations[i].cols == chip_locations[i].rect.width())
{
impl::basic_extract_image_chip(img, chip_locations[i].rect, chips[i]);
}
else
{ {
set_image_size(chips[i], chip_locations[i].rows, chip_locations[i].cols); set_image_size(chips[i], chip_locations[i].rows, chip_locations[i].cols);
...@@ -1616,6 +1670,7 @@ namespace dlib ...@@ -1616,6 +1670,7 @@ namespace dlib
transform_image(levels[level],chips[i],interpolate_bilinear(),trns); transform_image(levels[level],chips[i],interpolate_bilinear(),trns);
} }
} }
}
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
...@@ -1628,12 +1683,23 @@ namespace dlib ...@@ -1628,12 +1683,23 @@ namespace dlib
const chip_details& location, const chip_details& location,
image_type2& chip image_type2& chip
) )
{
// If the chip doesn't have any rotation or scaling then use the basic version of
// chip extraction that just does a fast copy.
if (location.angle == 0 &&
location.rows == location.rect.height() &&
location.cols == location.rect.width())
{
impl::basic_extract_image_chip(img, location.rect, chip);
}
else
{ {
std::vector<chip_details> chip_locations(1,location); std::vector<chip_details> chip_locations(1,location);
dlib::array<image_type2> chips; dlib::array<image_type2> chips;
extract_image_chips(img, chip_locations, chips); extract_image_chips(img, chip_locations, chips);
swap(chips[0], chip); swap(chips[0], chip);
} }
}
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
......
...@@ -907,8 +907,8 @@ namespace dlib ...@@ -907,8 +907,8 @@ namespace dlib
contained within the rectangle this->rect and that prior to extraction the contained within the rectangle this->rect and that prior to extraction the
image should be rotated counter-clockwise by this->angle radians. Finally, image should be rotated counter-clockwise by this->angle radians. Finally,
the extracted chip should have this->rows rows and this->cols columns in it the extracted chip should have this->rows rows and this->cols columns in it
regardless of the shape of this->rect. regardless of the shape of this->rect. This means that the extracted chip
will be stretched to fit via bilinear interpolation when necessary.
!*/ !*/
chip_details( chip_details(
...@@ -922,6 +922,18 @@ namespace dlib ...@@ -922,6 +922,18 @@ namespace dlib
- #cols == 0 - #cols == 0
!*/ !*/
chip_details(
const rectangle& rect_
);
/*!
ensures
- #rect == rect_
- #size() == rect_.area()
- #angle == 0
- #rows == rect_.height()
- #cols == rect_.width()
!*/
chip_details( chip_details(
const rectangle& rect_, const rectangle& rect_,
unsigned long size_ unsigned long size_
...@@ -1012,7 +1024,7 @@ namespace dlib ...@@ -1012,7 +1024,7 @@ namespace dlib
- #cols == dims.cols - #cols == dims.cols
- #size() == dims.rows*dims.cols - #size() == dims.rows*dims.cols
- #rect and #angle are computed based on the given size of the output chip - #rect and #angle are computed based on the given size of the output chip
(specified by dims) and the similairty transform between the chip and (specified by dims) and the similarity transform between the chip and
image (specified by chip_points and img_points). image (specified by chip_points and img_points).
!*/ !*/
......
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