Commit f21ac080 authored by Davis King's avatar Davis King

Added hough_transform, remove_incoherent_edge_pixels(),

normalize_image_gradients(), line, signed_distance_to_line(),
distance_to_line(), reverse(), intersect(), count_points_on_side_of_line(),
count_points_between_lines(), dot(), and normalize() to Python API.
parent ecde075d
......@@ -6,7 +6,7 @@ set(USE_SSE4_INSTRUCTIONS ON CACHE BOOL "Use SSE4 instructions")
# Set this to disable link time optimization. The only reason for
# doing this to make the compile faster which is nice when developing
# new modules.
# set(PYBIND11_LTO_CXX_FLAGS "")
set(PYBIND11_LTO_CXX_FLAGS "")
# Avoid cmake warnings about changes in behavior of some Mac OS X path
......@@ -71,6 +71,7 @@ set(python_srcs
src/global_optimization.cpp
src/image_dataset_metadata.cpp
src/numpy_returns.cpp
src/line.cpp
)
# Only add the GUI module if requested
......
......@@ -29,6 +29,7 @@ void bind_cnn_face_detection(py::module& m);
void bind_global_optimization(py::module& m);
void bind_numpy_returns(py::module& m);
void bind_image_dataset_metadata(py::module& m);
void bind_line(py::module& m);
#ifndef DLIB_NO_GUI_SUPPORT
void bind_gui(py::module& m);
......@@ -85,15 +86,16 @@ PYBIND11_MODULE(dlib, m)
bind_vector(m);
bind_basic_types(m);
bind_other(m);
bind_line(m);
bind_svm_rank_trainer(m);
bind_decision_functions(m);
bind_cca(m);
bind_sequence_segmenter(m);
bind_svm_struct(m);
bind_rectangles(m);
bind_image_classes(m);
bind_image_classes2(m);
bind_rectangles(m);
bind_object_detection(m);
bind_shape_predictors(m);
bind_correlation_tracker(m);
......
......@@ -70,6 +70,15 @@ void add_overlay_parts (
win.add_overlay(render_face_detections(detection, color));
}
void add_overlay_line (
image_window& win,
const line& l,
const rgb_pixel& color
)
{
win.add_overlay(l,color);
}
template <typename T>
std::shared_ptr<image_window> make_image_window_from_image(const numpy_image<T>& img)
{
......@@ -150,6 +159,8 @@ void bind_gui(py::module& m)
"Add a rectangle to the image_window. It will be displayed as a red box by default, but the color can be passed.")
.def("add_overlay", add_overlay_parts, py::arg("detection"), py::arg("color")=rgb_pixel(0, 0, 255),
"Add full_object_detection parts to the image window. They will be displayed as blue lines by default, but the color can be passed.")
.def("add_overlay", add_overlay_line, py::arg("line"), py::arg("color")=rgb_pixel(255, 0, 0),
"Add line to the image window.")
.def("wait_until_closed", &type::wait_until_closed,
"This function blocks until the window is closed.");
}
......
// Copyright (C) 2018 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#include "opaque_types.h"
#include <dlib/python.h>
#include "dlib/pixel.h"
......
// Copyright (C) 2018 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#include "opaque_types.h"
#include <dlib/python.h>
#include "dlib/pixel.h"
......@@ -38,6 +40,485 @@ numpy_image<T> py_equalize_histogram (
// ----------------------------------------------------------------------------------------
class py_hough_transform
{
public:
py_hough_transform(
unsigned long size
) : ht(size)
{
DLIB_CASSERT(size > 0);
}
unsigned long size(
) const { return ht.size(); }
long nr(
) const { return ht.nr(); }
long nc(
) const { return ht.nc(); }
line get_line (
const point& p
) const
{
DLIB_CASSERT(rectangle(0,0,size()-1,size()-1).contains(p));
auto temp = ht.get_line(p);
return line(temp.first, temp.second);
}
double get_line_angle_in_degrees (
const point& p
) const
{
DLIB_CASSERT(rectangle(0,0,size()-1,size()-1).contains(p));
return ht.get_line_angle_in_degrees(p);
}
py::tuple get_line_properties (
const point& p
) const
{
DLIB_CASSERT(rectangle(0,0,size()-1,size()-1).contains(p));
double angle_in_degrees;
double radius;
ht.get_line_properties(p, angle_in_degrees, radius);
return py::make_tuple(angle_in_degrees, radius);
}
point get_best_hough_point (
const point& p,
const numpy_image<float>& himg
)
{
DLIB_ASSERT(himg.nr() == size() && himg.nc() == size() &&
rectangle(0,0,size()-1,size()-1).contains(p) == true,
"\t point hough_transform::get_best_hough_point()"
<< "\n\t Invalid arguments given to this function."
<< "\n\t himg.nr(): " << himg.nr()
<< "\n\t himg.nc(): " << himg.nc()
<< "\n\t size(): " << size()
<< "\n\t p: " << p
);
return ht.get_best_hough_point(p,himg);
}
template <
typename T
>
numpy_image<float> compute_ht (
const numpy_image<T>& img,
const rectangle& box
) const
{
numpy_image<float> out;
ht(img, box, out);
return out;
}
template <
typename T
>
numpy_image<float> compute_ht2 (
const numpy_image<T>& img
) const
{
numpy_image<float> out;
ht(img, out);
return out;
}
template <
typename T
>
py::list find_pixels_voting_for_lines (
const numpy_image<T>& img,
const rectangle& box,
const std::vector<point>& hough_points,
const unsigned long angle_window_size = 1,
const unsigned long radius_window_size = 1
) const
{
return vector_to_python_list(ht.find_pixels_voting_for_lines(img, box, hough_points, angle_window_size, radius_window_size));
}
template <
typename T
>
py::list find_pixels_voting_for_lines2 (
const numpy_image<T>& img,
const std::vector<point>& hough_points,
const unsigned long angle_window_size = 1,
const unsigned long radius_window_size = 1
) const
{
return vector_to_python_list(ht.find_pixels_voting_for_lines(img, hough_points, angle_window_size, radius_window_size));
}
std::vector<point> find_strong_hough_points(
const numpy_image<float>& himg,
const float hough_count_threshold,
const double angle_nms_thresh,
const double radius_nms_thresh
)
{
return ht.find_strong_hough_points(himg, hough_count_threshold, angle_nms_thresh, radius_nms_thresh);
}
private:
hough_transform ht;
};
// ----------------------------------------------------------------------------------------
void register_hough_transform(py::module& m)
{
const char* class_docs =
"This object is a tool for computing the line finding version of the Hough transform \n\
given some kind of edge detection image as input. It also allows the edge pixels \n\
to be weighted such that higher weighted edge pixels contribute correspondingly \n\
more to the output of the Hough transform, allowing stronger edges to create \n\
correspondingly stronger line detections in the final Hough transform.";
const char* doc_constr =
"requires \n\
- size_ > 0 \n\
ensures \n\
- This object will compute Hough transforms that are size_ by size_ pixels. \n\
This is in terms of both the Hough accumulator array size as well as the \n\
input image size. \n\
- size() == size_";
/*!
requires
- size_ > 0
ensures
- This object will compute Hough transforms that are size_ by size_ pixels.
This is in terms of both the Hough accumulator array size as well as the
input image size.
- size() == size_
!*/
py::class_<py_hough_transform>(m, "hough_transform", class_docs)
.def(py::init<unsigned long>(), doc_constr, py::arg("size_"))
.def("size", &py_hough_transform::size,
"returns the size of the Hough transforms generated by this object. In particular, this object creates Hough transform images that are size() by size() pixels in size.")
.def("get_line", &py_hough_transform::get_line, py::arg("p"),
"requires \n\
- rectangle(0,0,size()-1,size()-1).contains(p) == true \n\
(i.e. p must be a point inside the Hough accumulator array) \n\
ensures \n\
- returns the line segment in the original image space corresponding \n\
to Hough transform point p. \n\
- The returned points are inside rectangle(0,0,size()-1,size()-1).")
/*!
requires
- rectangle(0,0,size()-1,size()-1).contains(p) == true
(i.e. p must be a point inside the Hough accumulator array)
ensures
- returns the line segment in the original image space corresponding
to Hough transform point p.
- The returned points are inside rectangle(0,0,size()-1,size()-1).
!*/
.def("get_line_angle_in_degrees", &py_hough_transform::get_line_angle_in_degrees, py::arg("p"),
"requires \n\
- rectangle(0,0,size()-1,size()-1).contains(p) == true \n\
(i.e. p must be a point inside the Hough accumulator array) \n\
ensures \n\
- returns the angle, in degrees, of the line corresponding to the Hough \n\
transform point p.")
/*!
requires
- rectangle(0,0,size()-1,size()-1).contains(p) == true
(i.e. p must be a point inside the Hough accumulator array)
ensures
- returns the angle, in degrees, of the line corresponding to the Hough
transform point p.
!*/
.def("get_line_properties", &py_hough_transform::get_line_properties, py::arg("p"),
"requires \n\
- rectangle(0,0,size()-1,size()-1).contains(p) == true \n\
(i.e. p must be a point inside the Hough accumulator array) \n\
ensures \n\
- Converts a point in the Hough transform space into an angle, in degrees, \n\
and a radius, measured in pixels from the center of the input image. \n\
- let ANGLE_IN_DEGREES == the angle of the line corresponding to the Hough \n\
transform point p. Moreover: -90 <= ANGLE_IN_DEGREES < 90. \n\
- RADIUS == the distance from the center of the input image, measured in \n\
pixels, and the line corresponding to the Hough transform point p. \n\
Moreover: -sqrt(size()*size()/2) <= RADIUS <= sqrt(size()*size()/2) \n\
- returns a tuple of (ANGLE_IN_DEGREES, RADIUS)" )
/*!
requires
- rectangle(0,0,size()-1,size()-1).contains(p) == true
(i.e. p must be a point inside the Hough accumulator array)
ensures
- Converts a point in the Hough transform space into an angle, in degrees,
and a radius, measured in pixels from the center of the input image.
- let ANGLE_IN_DEGREES == the angle of the line corresponding to the Hough
transform point p. Moreover: -90 <= ANGLE_IN_DEGREES < 90.
- RADIUS == the distance from the center of the input image, measured in
pixels, and the line corresponding to the Hough transform point p.
Moreover: -sqrt(size()*size()/2) <= RADIUS <= sqrt(size()*size()/2)
- returns a tuple of (ANGLE_IN_DEGREES, RADIUS)
!*/
.def("get_best_hough_point", &py_hough_transform::get_best_hough_point, py::arg("p"), py::arg("himg"),
"requires \n\
- himg has size() rows and columns. \n\
- rectangle(0,0,size()-1,size()-1).contains(p) == true \n\
ensures \n\
- This function interprets himg as a Hough image and p as a point in the \n\
original image space. Given this, it finds the maximum scoring line that \n\
passes though p. That is, it checks all the Hough accumulator bins in \n\
himg corresponding to lines though p and returns the location with the \n\
largest score. \n\
- returns a point X such that get_rect(himg).contains(X) == true")
/*!
requires
- himg has size() rows and columns.
- rectangle(0,0,size()-1,size()-1).contains(p) == true
ensures
- This function interprets himg as a Hough image and p as a point in the
original image space. Given this, it finds the maximum scoring line that
passes though p. That is, it checks all the Hough accumulator bins in
himg corresponding to lines though p and returns the location with the
largest score.
- returns a point X such that get_rect(himg).contains(X) == true
!*/
.def("__call__", &py_hough_transform::compute_ht<uint8_t>, py::arg("img"), py::arg("box"))
.def("__call__", &py_hough_transform::compute_ht<uint16_t>, py::arg("img"), py::arg("box"))
.def("__call__", &py_hough_transform::compute_ht<uint32_t>, py::arg("img"), py::arg("box"))
.def("__call__", &py_hough_transform::compute_ht<uint64_t>, py::arg("img"), py::arg("box"))
.def("__call__", &py_hough_transform::compute_ht<int8_t>, py::arg("img"), py::arg("box"))
.def("__call__", &py_hough_transform::compute_ht<int16_t>, py::arg("img"), py::arg("box"))
.def("__call__", &py_hough_transform::compute_ht<int32_t>, py::arg("img"), py::arg("box"))
.def("__call__", &py_hough_transform::compute_ht<int64_t>, py::arg("img"), py::arg("box"))
.def("__call__", &py_hough_transform::compute_ht<float>, py::arg("img"), py::arg("box"))
.def("__call__", &py_hough_transform::compute_ht<double>, py::arg("img"), py::arg("box"),
"requires \n\
- box.width() == size() \n\
- box.height() == size() \n\
ensures \n\
- Computes the Hough transform of the part of img contained within box. \n\
In particular, we do a grayscale version of the Hough transform where any \n\
non-zero pixel in img is treated as a potential component of a line and \n\
accumulated into the returned Hough accumulator image. However, rather than \n\
adding 1 to each relevant accumulator bin we add the value of the pixel \n\
in img to each Hough accumulator bin. This means that, if all the \n\
pixels in img are 0 or 1 then this routine performs a normal Hough \n\
transform. However, if some pixels have larger values then they will be \n\
weighted correspondingly more in the resulting Hough transform. \n\
- The returned hough transform image will be size() rows by size() columns. \n\
- The returned image is the Hough transform of the part of img contained in \n\
box. Each point in the Hough image corresponds to a line in the input box. \n\
In particular, the line for hough_image[y][x] is given by get_line(point(x,y)). \n\
Also, when viewing the Hough image, the x-axis gives the angle of the line \n\
and the y-axis the distance of the line from the center of the box. The \n\
conversion between Hough coordinates and angle and pixel distance can be \n\
obtained by calling get_line_properties()." )
/*!
requires
- box.width() == size()
- box.height() == size()
ensures
- Computes the Hough transform of the part of img contained within box.
In particular, we do a grayscale version of the Hough transform where any
non-zero pixel in img is treated as a potential component of a line and
accumulated into the returned Hough accumulator image. However, rather than
adding 1 to each relevant accumulator bin we add the value of the pixel
in img to each Hough accumulator bin. This means that, if all the
pixels in img are 0 or 1 then this routine performs a normal Hough
transform. However, if some pixels have larger values then they will be
weighted correspondingly more in the resulting Hough transform.
- The returned hough transform image will be size() rows by size() columns.
- The returned image is the Hough transform of the part of img contained in
box. Each point in the Hough image corresponds to a line in the input box.
In particular, the line for hough_image[y][x] is given by get_line(point(x,y)).
Also, when viewing the Hough image, the x-axis gives the angle of the line
and the y-axis the distance of the line from the center of the box. The
conversion between Hough coordinates and angle and pixel distance can be
obtained by calling get_line_properties().
!*/
.def("__call__", &py_hough_transform::compute_ht2<uint8_t>, py::arg("img"))
.def("__call__", &py_hough_transform::compute_ht2<uint16_t>, py::arg("img"))
.def("__call__", &py_hough_transform::compute_ht2<uint32_t>, py::arg("img"))
.def("__call__", &py_hough_transform::compute_ht2<uint64_t>, py::arg("img"))
.def("__call__", &py_hough_transform::compute_ht2<int8_t>, py::arg("img"))
.def("__call__", &py_hough_transform::compute_ht2<int16_t>, py::arg("img"))
.def("__call__", &py_hough_transform::compute_ht2<int32_t>, py::arg("img"))
.def("__call__", &py_hough_transform::compute_ht2<int64_t>, py::arg("img"))
.def("__call__", &py_hough_transform::compute_ht2<float>, py::arg("img"))
.def("__call__", &py_hough_transform::compute_ht2<double>, py::arg("img"),
" simply performs: return self(img, get_rect(img)). That is, just runs the hough transform on the whole input image.")
.def("find_pixels_voting_for_lines", &py_hough_transform::find_pixels_voting_for_lines<uint8_t>, py::arg("img"), py::arg("box"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1)
.def("find_pixels_voting_for_lines", &py_hough_transform::find_pixels_voting_for_lines<uint16_t>, py::arg("img"), py::arg("box"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1)
.def("find_pixels_voting_for_lines", &py_hough_transform::find_pixels_voting_for_lines<uint32_t>, py::arg("img"), py::arg("box"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1)
.def("find_pixels_voting_for_lines", &py_hough_transform::find_pixels_voting_for_lines<uint64_t>, py::arg("img"), py::arg("box"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1)
.def("find_pixels_voting_for_lines", &py_hough_transform::find_pixels_voting_for_lines<int8_t>, py::arg("img"), py::arg("box"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1)
.def("find_pixels_voting_for_lines", &py_hough_transform::find_pixels_voting_for_lines<int16_t>, py::arg("img"), py::arg("box"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1)
.def("find_pixels_voting_for_lines", &py_hough_transform::find_pixels_voting_for_lines<int32_t>, py::arg("img"), py::arg("box"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1)
.def("find_pixels_voting_for_lines", &py_hough_transform::find_pixels_voting_for_lines<int64_t>, py::arg("img"), py::arg("box"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1)
.def("find_pixels_voting_for_lines", &py_hough_transform::find_pixels_voting_for_lines<float>, py::arg("img"), py::arg("box"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1)
.def("find_pixels_voting_for_lines", &py_hough_transform::find_pixels_voting_for_lines<double>, py::arg("img"), py::arg("box"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1,
"requires \n\
- box.width() == size() \n\
- box.height() == size() \n\
- for all valid i: \n\
- rectangle(0,0,size()-1,size()-1).contains(hough_points[i]) == true \n\
(i.e. hough_points must contain points in the output Hough transform \n\
space generated by this object.) \n\
- angle_window_size >= 1 \n\
- radius_window_size >= 1 \n\
ensures \n\
- This function computes the Hough transform of the part of img contained \n\
within box. It does the same computation as __call__() defined above, \n\
except instead of accumulating into an image we create an explicit list \n\
of all the points in img that contributed to each line (i.e each point in \n\
the Hough image). To do this we take a list of Hough points as input and \n\
only record hits on these specifically identified Hough points. A \n\
typical use of find_pixels_voting_for_lines() is to first run the normal \n\
Hough transform using __call__(), then find the lines you are interested \n\
in, and then call find_pixels_voting_for_lines() to determine which \n\
pixels in the input image belong to those lines. \n\
- This routine returns a vector, CONSTITUENT_POINTS, with the following \n\
properties: \n\
- CONSTITUENT_POINTS.size() == hough_points.size() \n\
- for all valid i: \n\
- Let HP[i] = centered_rect(hough_points[i], angle_window_size, radius_window_size) \n\
- Any point in img with a non-zero value that lies on a line \n\
corresponding to one of the Hough points in HP[i] is added to \n\
CONSTITUENT_POINTS[i]. Therefore, when this routine finishes, \n\
#CONSTITUENT_POINTS[i] will contain all the points in img that \n\
voted for the lines associated with the Hough accumulator bins in \n\
HP[i]. \n\
- #CONSTITUENT_POINTS[i].size() == the number of points in img that \n\
voted for any of the lines HP[i] in Hough space. Note, however, \n\
that if angle_window_size or radius_window_size are made so large \n\
that HP[i] overlaps HP[j] for i!=j then the overlapping regions \n\
of Hough space are assign to HP[i] or HP[j] arbitrarily. \n\
Therefore, all points in CONSTITUENT_POINTS are unique, that is, \n\
there is no overlap in points between any two elements of \n\
CONSTITUENT_POINTS." )
/*!
requires
- box.width() == size()
- box.height() == size()
- for all valid i:
- rectangle(0,0,size()-1,size()-1).contains(hough_points[i]) == true
(i.e. hough_points must contain points in the output Hough transform
space generated by this object.)
- angle_window_size >= 1
- radius_window_size >= 1
ensures
- This function computes the Hough transform of the part of img contained
within box. It does the same computation as __call__() defined above,
except instead of accumulating into an image we create an explicit list
of all the points in img that contributed to each line (i.e each point in
the Hough image). To do this we take a list of Hough points as input and
only record hits on these specifically identified Hough points. A
typical use of find_pixels_voting_for_lines() is to first run the normal
Hough transform using __call__(), then find the lines you are interested
in, and then call find_pixels_voting_for_lines() to determine which
pixels in the input image belong to those lines.
- This routine returns a vector, CONSTITUENT_POINTS, with the following
properties:
- CONSTITUENT_POINTS.size() == hough_points.size()
- for all valid i:
- Let HP[i] = centered_rect(hough_points[i], angle_window_size, radius_window_size)
- Any point in img with a non-zero value that lies on a line
corresponding to one of the Hough points in HP[i] is added to
CONSTITUENT_POINTS[i]. Therefore, when this routine finishes,
#CONSTITUENT_POINTS[i] will contain all the points in img that
voted for the lines associated with the Hough accumulator bins in
HP[i].
- #CONSTITUENT_POINTS[i].size() == the number of points in img that
voted for any of the lines HP[i] in Hough space. Note, however,
that if angle_window_size or radius_window_size are made so large
that HP[i] overlaps HP[j] for i!=j then the overlapping regions
of Hough space are assign to HP[i] or HP[j] arbitrarily.
Therefore, all points in CONSTITUENT_POINTS are unique, that is,
there is no overlap in points between any two elements of
CONSTITUENT_POINTS.
!*/
.def("find_pixels_voting_for_lines", &py_hough_transform::find_pixels_voting_for_lines2<uint8_t>, py::arg("img"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1)
.def("find_pixels_voting_for_lines", &py_hough_transform::find_pixels_voting_for_lines2<uint16_t>, py::arg("img"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1)
.def("find_pixels_voting_for_lines", &py_hough_transform::find_pixels_voting_for_lines2<uint32_t>, py::arg("img"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1)
.def("find_pixels_voting_for_lines", &py_hough_transform::find_pixels_voting_for_lines2<uint64_t>, py::arg("img"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1)
.def("find_pixels_voting_for_lines", &py_hough_transform::find_pixels_voting_for_lines2<int8_t>, py::arg("img"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1)
.def("find_pixels_voting_for_lines", &py_hough_transform::find_pixels_voting_for_lines2<int16_t>, py::arg("img"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1)
.def("find_pixels_voting_for_lines", &py_hough_transform::find_pixels_voting_for_lines2<int32_t>, py::arg("img"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1)
.def("find_pixels_voting_for_lines", &py_hough_transform::find_pixels_voting_for_lines2<int64_t>, py::arg("img"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1)
.def("find_pixels_voting_for_lines", &py_hough_transform::find_pixels_voting_for_lines2<float>, py::arg("img"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1)
.def("find_pixels_voting_for_lines", &py_hough_transform::find_pixels_voting_for_lines2<double>, py::arg("img"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1,
" performs: return find_pixels_voting_for_lines(img, get_rect(img), hough_points, angle_window_size, radius_window_size); \n\
That is, just runs the routine on the whole input image." )
.def("find_strong_hough_points", &py_hough_transform::find_strong_hough_points, py::arg("himg"), py::arg("hough_count_threshold"), py::arg("angle_nms_thresh"), py::arg("radius_nms_thresh"),
"requires \n\
- himg has size() rows and columns. \n\
- angle_nms_thresh >= 0 \n\
- radius_nms_thresh >= 0 \n\
ensures \n\
- This routine finds strong lines in a Hough transform and performs \n\
non-maximum suppression on the detected lines. Recall that each point in \n\
Hough space is associated with a line. Therefore, this routine finds all \n\
the pixels in himg (a Hough transform image) with values >= \n\
hough_count_threshold and performs non-maximum suppression on the \n\
identified list of pixels. It does this by discarding lines that are \n\
within angle_nms_thresh degrees of a stronger line or within \n\
radius_nms_thresh distance (in terms of radius as defined by \n\
get_line_properties()) to a stronger Hough point. \n\
- The identified lines are returned as a list of coordinates in himg." );
/*!
requires
- himg has size() rows and columns.
- angle_nms_thresh >= 0
- radius_nms_thresh >= 0
ensures
- This routine finds strong lines in a Hough transform and performs
non-maximum suppression on the detected lines. Recall that each point in
Hough space is associated with a line. Therefore, this routine finds all
the pixels in himg (a Hough transform image) with values >=
hough_count_threshold and performs non-maximum suppression on the
identified list of pixels. It does this by discarding lines that are
within angle_nms_thresh degrees of a stronger line or within
radius_nms_thresh distance (in terms of radius as defined by
get_line_properties()) to a stronger Hough point.
- The identified lines are returned as a list of coordinates in himg.
!*/
}
// ----------------------------------------------------------------------------------------
std::vector<point> py_remove_incoherent_edge_pixels (
const std::vector<point>& line,
const numpy_image<float>& horz_gradient,
const numpy_image<float>& vert_gradient,
double angle_threshold
)
{
DLIB_CASSERT(num_rows(horz_gradient) == num_rows(vert_gradient));
DLIB_CASSERT(num_columns(horz_gradient) == num_columns(vert_gradient));
DLIB_CASSERT(angle_threshold >= 0);
for (auto& p : line)
DLIB_CASSERT(get_rect(horz_gradient).contains(p), "All line points must be inside the given images.");
return remove_incoherent_edge_pixels(line, horz_gradient, vert_gradient, angle_threshold);
}
// ----------------------------------------------------------------------------------------
void bind_image_classes2(py::module& m)
{
......@@ -59,6 +540,69 @@ void bind_image_classes2(py::module& m)
docs = "Returns a histogram equalized version of img.";
m.def("equalize_histogram", &py_equalize_histogram<uint8_t>, py::arg("img"));
m.def("equalize_histogram", &py_equalize_histogram<uint16_t>, docs, py::arg("img"));
register_hough_transform(m);
m.def("normalize_image_gradients", normalize_image_gradients<numpy_image<double>>, py::arg("img1"), py::arg("img2"));
m.def("normalize_image_gradients", normalize_image_gradients<numpy_image<float>>, py::arg("img1"), py::arg("img2"),
"requires \n\
- img1 and img2 have the same dimensions. \n\
ensures \n\
- This function assumes img1 and img2 are the two gradient images produced by a \n\
function like sobel_edge_detector(). It then unit normalizes the gradient \n\
vectors. That is, for all valid r and c, this function ensures that: \n\
- img1[r][c]*img1[r][c] + img2[r][c]*img2[r][c] == 1 \n\
unless both img1[r][c] and img2[r][c] were 0 initially, then they stay zero.");
/*!
requires
- img1 and img2 have the same dimensions.
ensures
- This function assumes img1 and img2 are the two gradient images produced by a
function like sobel_edge_detector(). It then unit normalizes the gradient
vectors. That is, for all valid r and c, this function ensures that:
- img1[r][c]*img1[r][c] + img2[r][c]*img2[r][c] == 1
unless both img1[r][c] and img2[r][c] were 0 initially, then they stay zero.
!*/
m.def("remove_incoherent_edge_pixels", &py_remove_incoherent_edge_pixels, py::arg("line"), py::arg("horz_gradient"),
py::arg("vert_gradient"), py::arg("angle_thresh"),
"requires \n\
- horz_gradient and vert_gradient have the same dimensions. \n\
- horz_gradient and vert_gradient represent unit normalized vectors. That is, \n\
you should have called normalize_image_gradients(horz_gradient,vert_gradient) \n\
or otherwise caused all the gradients to have unit norm. \n\
- for all valid i: \n\
get_rect(horz_gradient).contains(line[i]) \n\
ensures \n\
- This routine looks at all the points in the given line and discards the ones that \n\
have outlying gradient directions. To be specific, this routine returns a set \n\
of points PTS such that: \n\
- for all valid i,j: \n\
- The difference in angle between the gradients for PTS[i] and PTS[j] is \n\
less than angle_threshold degrees. \n\
- len(PTS) <= len(line) \n\
- PTS is just line with some elements removed." );
/*!
requires
- horz_gradient and vert_gradient have the same dimensions.
- horz_gradient and vert_gradient represent unit normalized vectors. That is,
you should have called normalize_image_gradients(horz_gradient,vert_gradient)
or otherwise caused all the gradients to have unit norm.
- for all valid i:
get_rect(horz_gradient).contains(line[i])
ensures
- This routine looks at all the points in the given line and discards the ones that
have outlying gradient directions. To be specific, this routine returns a set
of points PTS such that:
- for all valid i,j:
- The difference in angle between the gradients for PTS[i] and PTS[j] is
less than angle_threshold degrees.
- len(PTS) <= len(line)
- PTS is just line with some elements removed.
!*/
}
// Copyright (C) 2018 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#include "opaque_types.h"
#include <dlib/python.h>
#include <dlib/matrix.h>
#include <dlib/geometry/vector.h>
#include <pybind11/stl_bind.h>
#include "indexing.h"
using namespace dlib;
using namespace std;
string line__repr__ (const line& p)
{
std::ostringstream sout;
sout << "line(" << p.p1() << ", " << p.p2() << ")";
return sout.str();
}
string line__str__(const line& p)
{
std::ostringstream sout;
sout << "(" << p.p1() << ", " << p.p2() << ")";
return sout.str();
}
// ----------------------------------------------------------------------------------------
void bind_line(py::module& m)
{
const char* class_docs =
"This object represents a line in the 2D plane. The line is defined by two points \n\
running through it, p1 and p2. This object also includes a unit normal vector that \n\
is perpendicular to the line.";
py::class_<line>(m, "line", class_docs)
.def(py::init<>(), "p1, p2, and normal are all the 0 vector.")
.def(py::init<dpoint,dpoint>(), py::arg("a"), py::arg("b"),
"ensures \n\
- #p1 == a \n\
- #p2 == b \n\
- #normal == A vector normal to the line passing through points a and b. \n\
Therefore, the normal vector is the vector (a-b) but unit normalized and rotated clockwise 90 degrees."
/*!
ensures
- #p1 == a
- #p2 == b
- #normal == A vector normal to the line passing through points a and b.
Therefore, the normal vector is the vector (a-b) but unit normalized and rotated clockwise 90 degrees.
!*/
)
.def(py::init<point,point>(), py::arg("a"), py::arg("b"),
"ensures \n\
- #p1 == a \n\
- #p2 == b \n\
- #normal == A vector normal to the line passing through points a and b. \n\
Therefore, the normal vector is the vector (a-b) but unit normalized and rotated clockwise 90 degrees."
/*!
ensures
- #p1 == a
- #p2 == b
- #normal == A vector normal to the line passing through points a and b.
Therefore, the normal vector is the vector (a-b) but unit normalized and rotated clockwise 90 degrees.
!*/
)
.def_property_readonly("normal", &line::normal, "returns a unit vector that is normal to the line passing through p1 and p2.")
.def("__repr__", &line__repr__)
.def("__str__", &line__str__)
.def_property_readonly("p1", &line::p1, "returns the first endpoint of the line.")
.def_property_readonly("p2", &line::p2, "returns the second endpoint of the line.");
m.def("signed_distance_to_line", &signed_distance_to_line<long>, py::arg("l"), py::arg("p"));
m.def("signed_distance_to_line", &signed_distance_to_line<double>, py::arg("l"), py::arg("p"),
"ensures \n\
- returns how far p is from the line l. This is a signed distance. The sign \n\
indicates which side of the line the point is on and the magnitude is the \n\
distance. Moreover, the direction of positive sign is pointed to by the \n\
vector l.normal. \n\
- To be specific, this routine returns dot(p-l.p1, l.normal)"
/*!
ensures
- returns how far p is from the line l. This is a signed distance. The sign
indicates which side of the line the point is on and the magnitude is the
distance. Moreover, the direction of positive sign is pointed to by the
vector l.normal.
- To be specific, this routine returns dot(p-l.p1, l.normal)
!*/
);
m.def("distance_to_line", &distance_to_line<long>, py::arg("l"), py::arg("p"));
m.def("distance_to_line", &distance_to_line<double>, py::arg("l"), py::arg("p"),
"returns abs(signed_distance_to_line(l,p))" );
m.def("reverse", [](const line& a){return reverse(a);}, py::arg("l"),
"ensures \n\
- returns line(l.p2, l.p1) \n\
(i.e. returns a line object that represents the same line as l but with the \n\
endpoints, and therefore, the normal vector flipped. This means that the \n\
signed distance of operator() is also flipped)."
/*!
ensures
- returns line(l.p2, l.p1)
(i.e. returns a line object that represents the same line as l but with the
endpoints, and therefore, the normal vector flipped. This means that the
signed distance of operator() is also flipped).
!*/
);
m.def("intersect", [](const line& a, const line& b){return intersect(a,b);}, py::arg("a"), py::arg("b"),
"ensures \n\
- returns the point of intersection between lines a and b. If no such point \n\
exists then this function returns a point with Inf values in it."
/*!
ensures
- returns the point of intersection between lines a and b. If no such point
exists then this function returns a point with Inf values in it.
!*/
);
m.def("count_points_on_side_of_line", &count_points_on_side_of_line<long>, py::arg("l"), py::arg("reference_point"), py::arg("pts"), py::arg("dist_thresh"));
m.def("count_points_on_side_of_line", &count_points_on_side_of_line<double>, py::arg("l"), py::arg("reference_point"), py::arg("pts"), py::arg("dist_thresh"),
"ensures \n\
- Returns a count of how many points in pts are on the same side of l as \n\
reference_point, but also no more than dist_thresh distance from the line."
/*!
ensures
- Returns a count of how many points in pts are on the same side of l as
reference_point, but also no more than dist_thresh distance from the line.
!*/
);
m.def("count_points_between_lines", &count_points_between_lines<long>, py::arg("l1"), py::arg("l2"), py::arg("reference_point"), py::arg("pts"));
m.def("count_points_between_lines", &count_points_between_lines<double>, py::arg("l1"), py::arg("l2"), py::arg("reference_point"), py::arg("pts"),
"ensures \n\
- Counts and returns the number of points in pts that are between lines l1 and \n\
l2. Since a pair of lines will, in the general case, divide the plane into 4 \n\
regions, we identify the region of interest as the one that contains the \n\
reference_point. Therefore, this function counts the number of points in pts \n\
that appear in the same region as reference_point."
/*!
ensures
- Counts and returns the number of points in pts that are between lines l1 and
l2. Since a pair of lines will, in the general case, divide the plane into 4
regions, we identify the region of interest as the one that contains the
reference_point. Therefore, this function counts the number of points in pts
that appear in the same region as reference_point.
!*/
);
}
......@@ -184,6 +184,7 @@ void bind_vector(py::module& m)
.def(py::init<dpoint>(), py::arg("p"))
.def("__repr__", &point__repr__)
.def("__str__", &point__str__)
.def("normalize", &type::normalize, "Returns a unit normalized copy of this vector.")
.def_property("x", &point_x, [](point& p, long x){p.x()=x;}, "The x-coordinate of the point.")
.def_property("y", &point_y, [](point& p, long y){p.x()=y;}, "The y-coordinate of the point.")
.def(py::pickle(&getstate<type>, &setstate<type>));
......@@ -204,6 +205,7 @@ void bind_vector(py::module& m)
.def(py::init<point>(), py::arg("p"))
.def("__repr__", &dpoint__repr__)
.def("__str__", &dpoint__str__)
.def("normalize", &type::normalize, "Returns a unit normalized copy of this vector.")
.def_property("x", &dpoint_x, [](dpoint& p, double x){p.x()=x;}, "The x-coordinate of the dpoint.")
.def_property("y", &dpoint_y, [](dpoint& p, double y){p.x()=y;}, "The y-coordinate of the dpoint.")
.def(py::pickle(&getstate<type>, &setstate<type>));
......@@ -221,4 +223,8 @@ void bind_vector(py::module& m)
"returns the distance from p to the origin, i.e. the L2 norm of p.", py::arg("p"));
m.def("length", [](const dpoint& p){return length(p); },
"returns the distance from p to the origin, i.e. the L2 norm of p.", py::arg("p"));
m.def("dot", [](const point& a, const point& b){return dot(a,b); }, "Returns the dot product of the points a and b.", py::arg("a"), py::arg("b"));
m.def("dot", [](const dpoint& a, const dpoint& b){return dot(a,b); }, "Returns the dot product of the points a and b.", py::arg("a"), py::arg("b"));
}
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