forked from hustvl/SparseTrack
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpython_module.cpp
144 lines (118 loc) · 4.72 KB
/
python_module.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#define PY_ARRAY_UNIQUE_SYMBOL pbcvt_ARRAY_API
#include <boost/python.hpp>
#include <pyboostcvconverter/pyboostcvconverter.hpp>
#include "opencv2/opencv.hpp"
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/video.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/videostab.hpp"
namespace pbcvt {
using namespace boost::python;
/**
* @brief Example function. Basic inner matrix product using explicit matrix conversion.
* @param left left-hand matrix operand (NdArray required)
* @param right right-hand matrix operand (NdArray required)
* @return an NdArray representing the dot-product of the left and right operands
*/
PyObject *dot(PyObject *left, PyObject *right) {
cv::Mat leftMat, rightMat;
leftMat = pbcvt::fromNDArrayToMat(left);
rightMat = pbcvt::fromNDArrayToMat(right);
auto c1 = leftMat.cols, r2 = rightMat.rows;
// Check that the 2-D matrices can be legally multiplied.
if (c1 != r2) {
PyErr_SetString(PyExc_TypeError,
"Incompatible sizes for matrix multiplication.");
throw_error_already_set();
}
cv::Mat result = leftMat * rightMat;
PyObject *ret = pbcvt::fromMatToNDArray(result);
return ret;
}
/**
* @brief Example function. Simply makes a new CV_16UC3 matrix and returns it as a numpy array.
* @return The resulting numpy array.
*/
PyObject* makeCV_16UC3Matrix(){
cv::Mat image = cv::Mat::zeros(240,320, CV_16UC3);
PyObject* py_image = pbcvt::fromMatToNDArray(image);
return py_image;
}
//
/**
* @brief Example function. Basic inner matrix product using implicit matrix conversion.
* @details This example uses Mat directly, but we won't need to worry about the conversion in the body of the function.
* @param leftMat left-hand matrix operand
* @param rightMat right-hand matrix operand
* @return an NdArray representing the dot-product of the left and right operands
*/
cv::Mat dot2(cv::Mat leftMat, cv::Mat rightMat) {
auto c1 = leftMat.cols, r2 = rightMat.rows;
if (c1 != r2) {
PyErr_SetString(PyExc_TypeError,
"Incompatible sizes for matrix multiplication.");
throw_error_already_set();
}
cv::Mat result = leftMat * rightMat;
return result;
}
/**
* \brief Example function. Increments all elements of the given matrix by one.
* @details This example uses Mat directly, but we won't need to worry about the conversion anywhere at all,
* it is handled automatically by boost.
* \param matrix (numpy array) to increment
* \return
*/
cv::Mat increment_elements_by_one(cv::Mat matrix){
matrix += 1.0;
return matrix;
}
cv::Mat GMC(cv::Mat currFrame, cv::Mat prevFrame, int downscale){
cv::Mat frame, preframe;
cv::Ptr<cv::videostab::MotionEstimatorRansacL2> est = cv::makePtr<cv::videostab::MotionEstimatorRansacL2>(cv::videostab::MM_SIMILARITY);
cv::Ptr<cv::videostab::KeypointBasedMotionEstimator> kbest = cv::makePtr<cv::videostab::KeypointBasedMotionEstimator>(est);
// timer.start();
cv::Size downSize(currFrame.cols / downscale, currFrame.rows / downscale);
cv::resize(currFrame, frame, downSize, cv::INTER_LINEAR);//
cv::Mat warp = cv::Mat::eye(3, 3, CV_32F);
if (!prevFrame.empty())
{
cv::resize(prevFrame, preframe, downSize, cv::INTER_LINEAR);
bool ok;
warp = kbest->estimate(preframe, frame, &ok);
if (!ok)
{
std::cout << "WARNING: Warp not ok" << std::endl;
}
warp.convertTo(warp, CV_32F);
warp.at<float>(0, 2) *= downscale;
warp.at<float>(1, 2) *= downscale;
}
return warp;
}
#if (PY_VERSION_HEX >= 0x03000000)
static void* init_ar() {
#else
static void init_ar(){
#endif
Py_Initialize();
import_array();
return NUMPY_IMPORT_ARRAY_RETVAL;
}
BOOST_PYTHON_MODULE (pbcvt) {
//using namespace XM;
init_ar();
//initialize converters
to_python_converter<cv::Mat,pbcvt::matToNDArrayBoostConverter>();
pbcvt::matFromNDArrayBoostConverter();
//expose module-level functions
def("dot", dot);
def("dot2", dot2);
def("makeCV_16UC3Matrix", makeCV_16UC3Matrix);
//from PEP8 (https://www.python.org/dev/peps/pep-0008/?#prescriptive-naming-conventions)
//"Function names should be lowercase, with words separated by underscores as necessary to improve readability."
def("increment_elements_by_one", increment_elements_by_one);
def("GMC", GMC);
}
} //end namespace pbcvt