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"
......
This diff is collapsed.
// 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