Commit 91ae3de1 authored by Davis King's avatar Davis King

Made extract_image_4points() use std::array instead of std::vector and also

made it more robust to funky inputs.
parent e3a9ddb2
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "../simd.h" #include "../simd.h"
#include "../image_processing/full_object_detection.h" #include "../image_processing/full_object_detection.h"
#include <limits> #include <limits>
#include <array>
#include "../rand.h" #include "../rand.h"
namespace dlib namespace dlib
...@@ -2146,6 +2147,7 @@ namespace dlib ...@@ -2146,6 +2147,7 @@ namespace dlib
} }
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
template < template <
typename image_type typename image_type
...@@ -2153,11 +2155,9 @@ namespace dlib ...@@ -2153,11 +2155,9 @@ namespace dlib
void extract_image_4points ( void extract_image_4points (
const image_type& img_, const image_type& img_,
image_type& out_, image_type& out_,
const std::vector<dpoint>& pts const std::array<dpoint,4>& pts
) )
{ {
DLIB_CASSERT(pts.size() == 4);
const_image_view<image_type> img(img_); const_image_view<image_type> img(img_);
image_view<image_type> out(out_); image_view<image_type> out(out_);
if (out.size() == 0) if (out.size() == 0)
...@@ -2202,39 +2202,10 @@ namespace dlib ...@@ -2202,39 +2202,10 @@ namespace dlib
void extract_image_4points ( void extract_image_4points (
const image_type& img, const image_type& img,
image_type& out, image_type& out,
const std::vector<line>& lines const std::array<line,4>& lines
) )
{ {
DLIB_CASSERT(lines.size() == 4); extract_image_4points(img, out, find_convex_quadrilateral(lines));
// first find the pair of lines that are most parallel to each other
size_t best_i=0, best_j=1;
double best_angle = 1000;
for (size_t i = 0; i < lines.size(); ++i)
{
for (size_t j = i+1; j < lines.size(); ++j)
{
double angle = angle_between_lines(lines[i],lines[j]);
if (angle < best_angle)
{
best_angle = angle;
best_i = i;
best_j = j;
}
}
}
std::vector<dpoint> pts;
// now find the corners of the best quadrilateral we can make from these lines.
for (size_t k = 0; k < lines.size(); ++k)
{
if (k == best_i || k == best_j)
continue;
pts.emplace_back(intersect(lines[k], lines[best_i]));
pts.emplace_back(intersect(lines[k], lines[best_j]));
}
extract_image_4points(img, out, pts);
} }
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include "../pixel.h" #include "../pixel.h"
#include "../image_processing/full_object_detection_abstract.h" #include "../image_processing/full_object_detection_abstract.h"
#include "../image_processing/generic_image.h" #include "../image_processing/generic_image.h"
#include <array>
namespace dlib namespace dlib
{ {
...@@ -1456,14 +1457,13 @@ namespace dlib ...@@ -1456,14 +1457,13 @@ namespace dlib
void extract_image_4points ( void extract_image_4points (
const image_type& img, const image_type& img,
image_type& out, image_type& out,
const std::vector<dpoint>& pts const std::array<dpoint,4>& pts
); );
/*! /*!
requires requires
- image_type == an image object that implements the interface defined in - image_type == an image object that implements the interface defined in
dlib/image_processing/generic_image.h dlib/image_processing/generic_image.h
- pixel_traits<typename image_traits<image_type>::pixel_type>::has_alpha == false - pixel_traits<typename image_traits<image_type>::pixel_type>::has_alpha == false
- pts.size() == 4
ensures ensures
- The 4 points in pts define a convex quadrilateral and this function extracts - The 4 points in pts define a convex quadrilateral and this function extracts
that part of the input image img and stores it into #out. Therefore, each that part of the input image img and stores it into #out. Therefore, each
...@@ -1484,21 +1484,21 @@ namespace dlib ...@@ -1484,21 +1484,21 @@ namespace dlib
void extract_image_4points ( void extract_image_4points (
const image_type& img, const image_type& img,
image_type& out, image_type& out,
const std::vector<line>& lines const std::array<line,4>& lines
); );
/*! /*!
requires requires
- image_type == an image object that implements the interface defined in - image_type == an image object that implements the interface defined in
dlib/image_processing/generic_image.h dlib/image_processing/generic_image.h
- pixel_traits<typename image_traits<image_type>::pixel_type>::has_alpha == false - pixel_traits<typename image_traits<image_type>::pixel_type>::has_alpha == false
- lines.size() == 4
ensures ensures
- This routine simply finds the 4 intersecting points of the given lines and - This routine finds the 4 intersecting points of the given lines which form a
uses them in a call to the version of extract_image_4points() defined above. convex quadrilateral and uses them in a call to the version of
i.e. extract_image_4points(img, out, intersections_between_lines) extract_image_4points() defined above. i.e. extract_image_4points(img, out,
- Since 4 lines might intersect at more than 4 locations, we select the intersections_between_lines)
intersections that give a quadrilateral with opposing sides that are as throws
parallel as possible. - no_convex_quadrilateral: this is thrown if you can't make a convex
quadrilateral out of the given lines.
!*/ !*/
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
......
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