Skip to content

Commit eefb01a

Browse files
authored
Merge pull request #12 from clockj/shijie
Shijie
2 parents a8b6ae1 + c63ac58 commit eefb01a

15 files changed

+1269
-66
lines changed

demo/python/demo_camera_refraction.ipynb

Lines changed: 299 additions & 0 deletions
Large diffs are not rendered by default.

demo/python/demo_lpt.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 1,
5+
"execution_count": null,
66
"metadata": {},
77
"outputs": [],
88
"source": [
@@ -2199,7 +2199,7 @@
21992199
],
22002200
"metadata": {
22012201
"kernelspec": {
2202-
"display_name": "OpenLPTGUI",
2202+
"display_name": "OpenLPT",
22032203
"language": "python",
22042204
"name": "python3"
22052205
},

demo/python/demo_math.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@
432432
],
433433
"metadata": {
434434
"kernelspec": {
435-
"display_name": "OpenLPTGUI",
435+
"display_name": "OpenLPT",
436436
"language": "python",
437437
"name": "python3"
438438
},

inc/libMath/Camera.h

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <iostream>
1515
#include <sstream>
1616
#include <limits>
17+
#include <tuple>
1718

1819
#include "myMATH.h"
1920
#include "Matrix.h"
@@ -54,10 +55,24 @@ struct PolyParam
5455
// dv/dvar2 coeff,x_power, y_power, z_power
5556
};
5657

58+
struct PinPlateParam: public PinholeParam
59+
{
60+
Plane3D plane; // reference refractive plane (farthest plane to camera), normal vector is pointing away from the camera
61+
Pt3D pt3d_closest; // closest point to the camera
62+
std::vector<double> refract_array; // refractive index array (from farthest to nearest)
63+
std::vector<double> w_array; // width of the refractive plate (from farthest to nearest)
64+
int n_plate; // number of refractive plates
65+
double proj_tol; // projection tolerance squatre
66+
int proj_nmax; // maximum number of iterations for projection
67+
double lr; // learning rate
68+
double refract_ratio_max; // max(refract_array[0]/[i])
69+
};
70+
5771
enum CameraType
5872
{
5973
PINHOLE = 0,
60-
POLYNOMIAL
74+
POLYNOMIAL,
75+
PINPLATE
6176
};
6277

6378
class Camera
@@ -66,6 +81,7 @@ class Camera
6681
CameraType _type;
6782
PinholeParam _pinhole_param;
6883
PolyParam _poly_param;
84+
PinPlateParam _pinplate_param;
6985

7086
Camera ();
7187
Camera (const Camera& c); // Camera deep copy
@@ -97,15 +113,17 @@ class Camera
97113
// //
98114
// Projection //
99115
// //
100-
Pt2D project (Pt3D const& pt_world) const;
116+
Pt2D project (Pt3D const& pt_world, bool is_print_detail = false) const;
101117

102118
// Project world coordinate [mm] to image coordinate [mm]:
103119
// (xw,yw,zw) -> (x,y,z) -> (Xu,Yu,0)
104120
// (x,y,z) = R @ (xw,yw,zw) + T
105121
// (Xu,Yu,0) = (fx*x/z + cx, fy*y/z + cy)
106122
// input: pt_world: point location in world coordinate
107123
// output
108-
Pt2D worldToUndistImg (Pt3D const& pt_world) const;
124+
Pt2D worldToUndistImg (Pt3D const& pt_world, PinholeParam const& param) const;
125+
126+
std::tuple<bool, Pt3D, double> refractPlate (Pt3D const& pt_world) const;
109127

110128
// Project Image in camera coordinate [mm] to pixel:
111129
// (Xu,Yu,0) -> (Xd,Yd,0) -> (Xf,Yf,0)
@@ -117,7 +135,7 @@ class Camera
117135
// |
118136
// |
119137
// \/ y direction (downwards)
120-
Pt2D distort (Pt2D const& pt_img_undist) const;
138+
Pt2D distort (Pt2D const& pt_img_undist, PinholeParam const& param) const;
121139

122140
// Polynomial model
123141
// Project world coordinate [mm] to distorted image [px]:
@@ -135,14 +153,16 @@ class Camera
135153
// input: pt_pix: point location in pixel unit on image
136154
// (no use of z coordinate of pt_pix)
137155
// output: Matrix (camera coordinate) (Xu,Yu,0)
138-
Pt2D undistort (Pt2D const& pt_img_dist) const;
156+
Pt2D undistort (Pt2D const& pt_img_dist, PinholeParam const& param) const;
139157

140158
// Project image coordinate [mm] to world coordinate [mm]:
141159
// (Xu,Yu,0) -> (x,y,z) -> (xw,yw,zw)
142160
// input: pt_img_undist: undistorted image coordinate
143161
// output: line of sight
144162
Line3D pinholeLine (Pt2D const& pt_img_undist) const;
145163

164+
Line3D pinplateLine (Pt2D const& pt_img_undist) const;
165+
146166
// Polynomial Model
147167
Pt3D polyImgToWorld (Pt2D const& pt_img_dist, double plane_world) const;
148168

inc/libMath/Matrix.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,13 @@ struct Line2D
150150
Pt2D unit_vector;
151151
};
152152

153+
// Structure to store 3D plane
154+
struct Plane3D
155+
{
156+
Pt3D pt;
157+
Pt3D norm_vector;
158+
};
159+
153160
// Image: matrix with double type
154161
// Image(row_id, col_id) = intensity
155162
// row_id = img_y, col_id = img_x

inc/libMath/myMATH.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ double dist2 (Pt3D const& pt, Line3D const& line);
135135
double dist2 (Pt2D const& pt, Line2D const& line);
136136

137137

138+
// Calculate the distance square between point and plane
139+
double dist2 (Pt3D const& pt, Plane3D const& plane);
140+
141+
138142
// Calculate the distance between two points
139143
double dist (Pt3D const& pt1, Pt3D const& pt2);
140144
double dist (Pt2D const& pt1, Pt2D const& pt2);
@@ -145,6 +149,10 @@ double dist (Pt3D const& pt, Line3D const& line);
145149
double dist (Pt2D const& pt, Line2D const& line);
146150

147151

152+
// Calculate the distance between point and plane
153+
double dist (Pt3D const& pt, Plane3D const& plane);
154+
155+
148156
// Triangulation
149157
void triangulation (Pt3D& pt_world, double& error,
150158
std::vector<Line3D> const& line_of_sight_list);
@@ -153,6 +161,8 @@ void triangulation (Pt3D& pt_world, double& error,
153161
// Find cross points of two 2d lines
154162
bool crossPoint (Pt2D& pt2d, Line2D const& line1, Line2D const& line2);
155163

164+
// Find cross points of 3d line and 3d plane
165+
bool crossPoint (Pt3D& pt3d, Line3D const& line, Plane3D const& plane);
156166

157167
// Create identity matrix
158168
template<class T>

src/pybind_OpenLPT/pyCamera.cpp

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,53 @@ void init_Camera(py::module &m)
6060
);
6161
})
6262
.doc() = "PolyParam struct";
63+
64+
py::class_<PinPlateParam, PinholeParam>(m, "PinPlateParam")
65+
.def(py::init<>())
66+
.def_readwrite("plane", &PinPlateParam::plane)
67+
.def_readwrite("pt3d_closest", &PinPlateParam::pt3d_closest)
68+
.def_readwrite("refract_array", &PinPlateParam::refract_array)
69+
.def_readwrite("w_array", &PinPlateParam::w_array)
70+
.def_readwrite("n_plate", &PinPlateParam::n_plate)
71+
.def_readwrite("proj_tol", &PinPlateParam::proj_tol)
72+
.def_readwrite("proj_nmax", &PinPlateParam::proj_nmax)
73+
.def_readwrite("lr", &PinPlateParam::lr)
74+
.def_readwrite("refract_ratio_max", &PinPlateParam::refract_ratio_max)
75+
.def("to_dict", [](PinPlateParam const& self){
76+
return py::dict(
77+
"n_row"_a=self.n_row,
78+
"n_col"_a=self.n_col,
79+
"cam_mtx"_a=self.cam_mtx,
80+
"is_distorted"_a=self.is_distorted,
81+
"n_dist_coeff"_a=self.n_dist_coeff,
82+
"dist_coeff"_a=self.dist_coeff,
83+
"r_mtx"_a=self.r_mtx,
84+
"t_vec"_a=self.t_vec,
85+
"r_mtx_inv"_a=self.r_mtx_inv,
86+
"t_vec_inv"_a=self.t_vec_inv,
87+
"plane"_a=self.plane,
88+
"refract_array"_a=self.refract_array,
89+
"w_array"_a=self.w_array,
90+
"n_plate"_a=self.n_plate,
91+
"proj_tol"_a=self.proj_tol,
92+
"proj_nmax"_a=self.proj_nmax,
93+
"lr"_a=self.lr,
94+
"refract_ratio_max"_a=self.refract_ratio_max
95+
);
96+
})
97+
.doc() = "PinPlateParam struct";
6398

6499
py::enum_<CameraType>(m, "CameraType")
65100
.value("PINHOLE", CameraType::PINHOLE)
66101
.value("POLYNOMIAL", CameraType::POLYNOMIAL)
102+
.value("PINPLATE", CameraType::PINPLATE)
67103
.export_values();
68104

69105
py::class_<Camera>(m, "Camera")
70106
.def_readwrite("_type", &Camera::_type)
71107
.def_readwrite("_pinhole_param", &Camera::_pinhole_param)
72108
.def_readwrite("_poly_param", &Camera::_poly_param)
109+
.def_readwrite("_pinplate_param", &Camera::_pinplate_param)
73110
.def(py::init<>())
74111
.def(py::init<const Camera&>())
75112
.def(py::init<std::string>())
@@ -81,7 +118,9 @@ void init_Camera(py::module &m)
81118
.def("rmtxTorvec", &Camera::rmtxTorvec)
82119
.def("getNRow", &Camera::getNRow)
83120
.def("getNCol", &Camera::getNCol)
84-
.def("project", &Camera::project)
121+
.def("project", [](Camera const& self, Pt3D const& pt3d, bool is_print_detail){
122+
return self.project(pt3d, is_print_detail);
123+
}, py::arg("pt3d"), py::arg("is_print_detail")=false)
85124
.def("project", [](Camera const& self, std::vector<Pt3D> const& pt3d_list){
86125
std::vector<Pt2D> pt2d_list(pt3d_list.size());
87126
#pragma omp parallel for
@@ -92,7 +131,28 @@ void init_Camera(py::module &m)
92131
return pt2d_list;
93132
})
94133
.def("worldToUndistImg", &Camera::worldToUndistImg)
134+
.def("worldToUndistImg", [](Camera const& self, std::vector<Pt3D> const& pt3d_list, PinholeParam const& param){
135+
int npts = pt3d_list.size();
136+
std::vector<Pt2D> pt2d_list(npts);
137+
#pragma omp parallel for
138+
for (int i = 0; i < npts; i++)
139+
{
140+
pt2d_list[i] = self.worldToUndistImg(pt3d_list[i], param);
141+
}
142+
return pt2d_list;
143+
})
144+
.def("refractPlate", &Camera::refractPlate)
95145
.def("distort", &Camera::distort)
146+
.def("distort", [](Camera const& self, std::vector<Pt2D> const& pt2d_list, PinholeParam const& param){
147+
int npts = pt2d_list.size();
148+
std::vector<Pt2D> pt2d_list_dist(npts);
149+
#pragma omp parallel for
150+
for (int i = 0; i < npts; i++)
151+
{
152+
pt2d_list_dist[i] = self.distort(pt2d_list[i], param);
153+
}
154+
return pt2d_list_dist;
155+
})
96156
.def("polyProject", &Camera::polyProject)
97157
.def("lineOfSight", &Camera::lineOfSight)
98158
.def("lineOfSight", [](Camera const& self, std::vector<Pt2D> const& pt2d_list){
@@ -105,14 +165,26 @@ void init_Camera(py::module &m)
105165
return line_list;
106166
})
107167
.def("undistort", &Camera::undistort)
168+
.def("undistort", [](Camera const& self, std::vector<Pt2D> const& pt2d_list, PinholeParam const& param){
169+
int npts = pt2d_list.size();
170+
std::vector<Pt2D> pt2d_list_undist(npts);
171+
#pragma omp parallel for
172+
for (int i = 0; i < npts; i++)
173+
{
174+
pt2d_list_undist[i] = self.undistort(pt2d_list[i], param);
175+
}
176+
return pt2d_list_undist;
177+
})
108178
.def("pinholeLine", &Camera::pinholeLine)
109179
.def("polyImgToWorld", &Camera::polyImgToWorld)
110180
.def("polyLineOfSight", &Camera::polyLineOfSight)
181+
.def("pinplateLine", &Camera::pinplateLine)
111182
.def("to_dict", [](Camera const& self){
112183
return py::dict(
113184
"_type"_a=self._type,
114185
"_pinhole_param"_a=self._pinhole_param,
115-
"_poly_param"_a=self._poly_param
186+
"_poly_param"_a=self._poly_param,
187+
"_pinplate_param"_a=self._pinplate_param
116188
);
117189
})
118190
.doc() = "Camera class";

src/pybind_OpenLPT/pyInterface.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <pybind11/stl.h>
44
#include <iostream>
55
#include <string>
6+
#include <tuple>
67
#include <sstream>
78
#include <fstream>
89

src/pybind_OpenLPT/pyMatrix.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,19 @@ void init_Matrix(py::module &m)
244244
})
245245
.doc() = "Line2D struct";
246246

247+
py::class_<Plane3D>(m, "Plane3D")
248+
.def(py::init<>())
249+
.def(py::init<const Pt3D&, const Pt3D&>())
250+
.def_readwrite("pt", &Plane3D::pt)
251+
.def_readwrite("norm_vector", &Plane3D::norm_vector)
252+
.def("to_dict", [](Plane3D const& self){
253+
return py::dict(
254+
"pt"_a=self.pt,
255+
"norm_vector"_a=self.norm_vector
256+
);
257+
})
258+
.doc() = "Plane3D struct";
259+
247260
py::class_<Image, Matrix<double>>(m, "Image")
248261
.def(py::init<>())
249262
.def(py::init<int, int, double>())

0 commit comments

Comments
 (0)