Skip to content

Commit 70122ce

Browse files
committed
Optimize HomographicTransform for 1-dimensional case.
1 parent 222525f commit 70122ce

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

filters/output/DewarpingView.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,12 @@ DewarpingView::onPaint(QPainter& painter, InteractionState const& interaction)
124124
for (int j = 0; j < num_vert_grid_lines; ++j) {
125125
double const x = j / (num_vert_grid_lines - 1.0);
126126
CylindricalSurfaceDewarper::Generatrix const gtx(dewarper.mapGeneratrix(x, state));
127-
QPointF const gtx_p0(gtx.imgLine.pointAt(gtx.pln2img(Vec1d(0))[0]));
128-
QPointF const gtx_p1(gtx.imgLine.pointAt(gtx.pln2img(Vec1d(1))[0]));
127+
QPointF const gtx_p0(gtx.imgLine.pointAt(gtx.pln2img(0)));
128+
QPointF const gtx_p1(gtx.imgLine.pointAt(gtx.pln2img(1)));
129129
painter.drawLine(gtx_p0, gtx_p1);
130130
for (int i = 0; i < num_hor_grid_lines; ++i) {
131131
double const y = i / (num_hor_grid_lines - 1.0);
132-
curves[i].push_back(gtx.imgLine.pointAt(gtx.pln2img(Vec1d(y))[0]));
132+
curves[i].push_back(gtx.imgLine.pointAt(gtx.pln2img(y)));
133133
}
134134
}
135135

imageproc/HomographicTransform.h

+43-8
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,18 @@
2626
namespace imageproc
2727
{
2828

29+
template<size_t N, typename T> class HomographicTransform;
30+
2931
template<size_t N, typename T>
30-
class HomographicTransform
32+
class HomographicTransformBase
3133
{
34+
public:
3235
typedef VecNT<N, T> Vec;
3336
typedef VecNT<(N+1)*(N+1), T> Mat;
34-
public:
35-
explicit HomographicTransform(Mat const& mat) : m_mat(mat) {}
3637

37-
HomographicTransform inv() const;
38+
explicit HomographicTransformBase(Mat const& mat) : m_mat(mat) {}
39+
40+
HomographicTransform<N, T> inv() const;
3841

3942
Vec operator()(Vec const& from) const;
4043

@@ -43,19 +46,42 @@ class HomographicTransform
4346
Mat m_mat;
4447
};
4548

49+
50+
template<size_t N, typename T>
51+
class HomographicTransform : public HomographicTransformBase<N, T>
52+
{
53+
public:
54+
explicit HomographicTransform(Mat const& mat) : HomographicTransformBase<N, T>(mat) {}
55+
};
56+
57+
58+
/** An optimized, both in terms of API and performance, 1D version. */
59+
template<typename T>
60+
class HomographicTransform<1, T> : public HomographicTransformBase<1, T>
61+
{
62+
public:
63+
explicit HomographicTransform(Mat const& mat) : HomographicTransformBase<1, T>(mat) {}
64+
65+
T operator()(T from) const;
66+
67+
// Prevent it's shadowing by the above one.
68+
using HomographicTransformBase<1, T>::operator();
69+
};
70+
71+
4672
template<size_t N, typename T>
4773
HomographicTransform<N, T>
48-
HomographicTransform<N, T>::inv() const
74+
HomographicTransformBase<N, T>::inv() const
4975
{
5076
MatrixCalc<T, 4*(N+1)*(N+1), N+1> mc;
5177
Mat inv_mat;
5278
mc(N+1, N+1, m_mat).inv().write(inv_mat);
53-
return HomographicTransform(inv_mat);
79+
return HomographicTransform<N, T>(inv_mat);
5480
}
5581

5682
template<size_t N, typename T>
57-
typename HomographicTransform<N, T>::Vec
58-
HomographicTransform<N, T>::operator()(Vec const& from) const
83+
typename HomographicTransformBase<N, T>::Vec
84+
HomographicTransformBase<N, T>::operator()(Vec const& from) const
5985
{
6086
MatrixCalc<T, N+1, 1> mc;
6187
VecNT<N+1, T> const hsrc(from, T(1));
@@ -66,6 +92,15 @@ HomographicTransform<N, T>::operator()(Vec const& from) const
6692
return res;
6793
}
6894

95+
template<typename T>
96+
T
97+
HomographicTransform<1, T>::operator()(T from) const
98+
{
99+
// Optimized version for 1D case.
100+
T const* m = mat();
101+
return (from * m[0] + m[2]) / (from * m[1] + m[3]);
102+
}
103+
69104
} // namespace imageproc
70105

71106
#endif

0 commit comments

Comments
 (0)