Skip to content

Commit 88a97f9

Browse files
committed
1.2024.11.17: fix #47: dewarping: 3 slider for correct model
1 parent f0e124c commit 88a97f9

29 files changed

+524
-190
lines changed

src/acceleration/opencl/tests/TestDewarp.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ BOOST_AUTO_TEST_CASE(test_argb)
8282
CylindricalSurfaceDewarper const distortion_model(
8383
std::vector<QPointF> {QPointF(200, -50), QPointF(1050, 200)},
8484
std::vector<QPointF> {QPointF(-50, 800), QPointF(800, 1050)},
85-
1.5, 1.5
85+
1.5,
86+
1.5,
87+
1.5
8688
);
8789

8890
#if LOG_PERFORMANCE

src/dewarping/CylindricalSurfaceDewarper.cpp

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,24 @@
1616
along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19-
#include "CylindricalSurfaceDewarper.h"
20-
#include "ToLineProjector.h"
21-
#include "NumericTraits.h"
22-
#include "ToVec.h"
23-
#include "ToPoint.h"
24-
#include "../foundation/MultipleTargetsSupport.h"
19+
#include <cmath>
20+
#include <cassert>
21+
#include <stdexcept>
22+
#include <string>
23+
#include <boost/foreach.hpp>
2524
#include <Eigen/Core>
2625
#include <Eigen/QR>
2726
#include <QLineF>
2827
#include <QtGlobal>
2928
#include <QDebug>
3029
#include <boost/foreach.hpp>
3130
#include <algorithm>
32-
#include <cmath>
33-
#include <cassert>
34-
#include <stdexcept>
35-
#include <string>
31+
#include "CylindricalSurfaceDewarper.h"
32+
#include "ToLineProjector.h"
33+
#include "NumericTraits.h"
34+
#include "ToVec.h"
35+
#include "ToPoint.h"
36+
#include "../foundation/MultipleTargetsSupport.h"
3637

3738
/*
3839
Naming conventions:
@@ -95,11 +96,13 @@ CylindricalSurfaceDewarper::CylindricalSurfaceDewarper(
9596
std::vector<QPointF> const& img_directrix1,
9697
std::vector<QPointF> const& img_directrix2,
9798
double depth_perception,
98-
double curve_correct)
99+
double curve_correct,
100+
double curve_angle)
99101
: m_pln2img(calcPlnToImgHomography(img_directrix1, img_directrix2)),
100102
m_img2pln(m_pln2img.inv()),
101103
m_depthPerception(depth_perception),
102104
m_curveCorrect(curve_correct),
105+
m_curveAngle(curve_angle),
103106
m_plnStraightLineY(
104107
calcPlnStraightLineY(img_directrix1, img_directrix2, m_pln2img, m_img2pln)
105108
),
@@ -115,6 +118,10 @@ CylindricalSurfaceDewarper::mapGeneratrix(double crv_x, State& state) const
115118
{
116119
double const pln_x = m_arcLengthMapper.arcLenToX(crv_x, state.m_arcLengthHint);
117120

121+
double const lin_y1 = -0.02 * (m_curveAngle - 2.0);
122+
double const lin_y2 = -lin_y1;
123+
double const lin_y = lin_y1 + (lin_y2 - lin_y1) * pln_x;
124+
118125
Vector2d const pln_top_pt(pln_x, 0);
119126
Vector2d const pln_bottom_pt(pln_x, 1);
120127
QPointF const img_top_pt(toPoint(m_pln2img(pln_top_pt)));
@@ -135,7 +142,7 @@ CylindricalSurfaceDewarper::mapGeneratrix(double crv_x, State& state) const
135142
double const img_directrix12fd_proj = img_directrix12f_proj - pln_straight_line_y;
136143
//double const curve_coef = 1.0 + 0.5 * (m_curveCorrect - 2.0);
137144
double const curve_coef = (m_curveCorrect < 2.0) ? (1.0 / (3.0 - m_curveCorrect)) : (m_curveCorrect - 1.0);
138-
double const img_directrix12fds_proj = img_directrix12fd_proj * curve_coef;
145+
double const img_directrix12fds_proj = img_directrix12fd_proj * curve_coef + lin_y;
139146
double const img_directrix12fs_proj = img_directrix12fds_proj + pln_straight_line_y;
140147
QPointF const img_straight_line_pt(toPoint(m_pln2img(Vector2d(pln_x, img_directrix12fs_proj))));
141148
double const img_straight_line_proj(projector.projectionScalar(img_straight_line_pt));
@@ -163,6 +170,10 @@ CylindricalSurfaceDewarper::mapToDewarpedSpace(QPointF const& img_pt, State& sta
163170
double const pln_x = m_img2pln(toVec(img_pt))[0];
164171
double const crv_x = m_arcLengthMapper.xToArcLen(pln_x, state.m_arcLengthHint);
165172

173+
double const lin_y1 = -0.02 * (m_curveAngle - 2.0);
174+
double const lin_y2 = -lin_y1;
175+
double const lin_y = lin_y1 + (lin_y2 - lin_y1) * pln_x;
176+
166177
Vector2d const pln_top_pt(pln_x, 0);
167178
Vector2d const pln_bottom_pt(pln_x, 1);
168179
QPointF const img_top_pt(toPoint(m_pln2img(pln_top_pt)));
@@ -183,7 +194,7 @@ CylindricalSurfaceDewarper::mapToDewarpedSpace(QPointF const& img_pt, State& sta
183194
double const img_directrix12fd_proj = img_directrix12f_proj - pln_straight_line_y;
184195
//double const curve_coef = 1.0 + 0.5 * (m_curveCorrect - 2.0);
185196
double const curve_coef = (m_curveCorrect < 2.0) ? (1.0 / (3.0 - m_curveCorrect)) : (m_curveCorrect - 1.0);
186-
double const img_directrix12fds_proj = img_directrix12fd_proj * curve_coef;
197+
double const img_directrix12fds_proj = img_directrix12fd_proj * curve_coef + lin_y;
187198
double const img_directrix12fs_proj = img_directrix12fds_proj + pln_straight_line_y;
188199
QPointF const img_straight_line_pt(toPoint(m_pln2img(Vector2d(pln_x, img_directrix12fs_proj))));
189200
double const img_straight_line_proj(projector.projectionScalar(img_straight_line_pt));
@@ -384,7 +395,7 @@ CylindricalSurfaceDewarper::CoupledPolylinesIterator::CoupledPolylinesIterator(
384395
std::vector<QPointF> const& img_directrix2,
385396
HomographicTransform<2, double> const& pln2img,
386397
HomographicTransform<2, double> const& img2pln)
387-
: m_seq1It(img_directrix1.begin()),
398+
: m_seq1It(img_directrix1.begin()),
388399
m_seq2It(img_directrix2.begin()),
389400
m_seq1End(img_directrix1.end()),
390401
m_seq2End(img_directrix2.end()),

src/dewarping/CylindricalSurfaceDewarper.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ class DEWARPING_EXPORT CylindricalSurfaceDewarper
7070
std::vector<QPointF> const& img_directrix1,
7171
std::vector<QPointF> const& img_directrix2,
7272
double depth_perception,
73-
double curve_correct);
73+
double curve_correct,
74+
double curve_angle);
7475

7576
/**
7677
* \brief Returns the arc length of a directrix, assuming its
@@ -131,6 +132,7 @@ class DEWARPING_EXPORT CylindricalSurfaceDewarper
131132
HomographicTransform<2, double> m_img2pln;
132133
double m_depthPerception;
133134
double m_curveCorrect;
135+
double m_curveAngle;
134136
double m_plnStraightLineY;
135137
double m_directrixArcLength;
136138
ArcLengthMapper m_arcLengthMapper;

src/dewarping/DewarpingImageTransform.cpp

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@
1616
along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19-
#include "DewarpingImageTransform.h"
20-
#include "RoundingHasher.h"
21-
#include "ToVec.h"
22-
#include "ToLineProjector.h"
23-
#include "LineBoundedByPolygon.h"
24-
#include "imageproc/AffineImageTransform.h"
25-
#include "imageproc/AffineTransformedImage.h"
19+
#include <algorithm>
20+
#include <iterator>
21+
#include <utility>
22+
#include <memory>
23+
#include <limits>
24+
#include <cmath>
25+
#include <cassert>
26+
#include <map>
27+
#include <boost/optional.hpp>
28+
#include <boost/range/adaptor/reversed.hpp>
2629
#include <Eigen/Core>
2730
#include <Eigen/LU>
2831
#include <QImage>
@@ -36,16 +39,13 @@
3639
#include <QTransform>
3740
#include <QColor>
3841
#include <QString>
39-
#include <boost/optional.hpp>
40-
#include <boost/range/adaptor/reversed.hpp>
41-
#include <algorithm>
42-
#include <iterator>
43-
#include <utility>
44-
#include <memory>
45-
#include <limits>
46-
#include <cmath>
47-
#include <cassert>
48-
#include <map>
42+
#include "DewarpingImageTransform.h"
43+
#include "RoundingHasher.h"
44+
#include "ToVec.h"
45+
#include "ToLineProjector.h"
46+
#include "LineBoundedByPolygon.h"
47+
#include "imageproc/AffineImageTransform.h"
48+
#include "imageproc/AffineTransformedImage.h"
4949

5050
using namespace Eigen;
5151
using namespace imageproc;
@@ -116,17 +116,19 @@ DewarpingImageTransform::DewarpingImageTransform(
116116
std::vector<QPointF> const& top_curve,
117117
std::vector<QPointF> const& bottom_curve,
118118
DepthPerception const& depth_perception,
119-
DepthPerception const& curve_correct)
120-
: m_origSize(orig_size)
121-
, m_topPolyline(top_curve)
122-
, m_bottomPolyline(bottom_curve)
123-
, m_depthPerception(depth_perception)
124-
, m_curveCorrect(curve_correct)
125-
, m_dewarper(top_curve, bottom_curve, depth_perception.value(), curve_correct.value())
126-
, m_intrinsicScaleX(1.0)
127-
, m_intrinsicScaleY(1.0)
128-
, m_userScaleX(1.0)
129-
, m_userScaleY(1.0)
119+
DepthPerception const& curve_correct,
120+
DepthPerception const& curve_angle)
121+
: m_origSize(orig_size)
122+
, m_topPolyline(top_curve)
123+
, m_bottomPolyline(bottom_curve)
124+
, m_depthPerception(depth_perception)
125+
, m_curveCorrect(curve_correct)
126+
, m_curveAngle(curve_angle)
127+
, m_dewarper(top_curve, bottom_curve, depth_perception.value(), curve_correct.value(), curve_angle.value())
128+
, m_intrinsicScaleX(1.0)
129+
, m_intrinsicScaleY(1.0)
130+
, m_userScaleX(1.0)
131+
, m_userScaleY(1.0)
130132
{
131133
// These two lines don't depend on each other and therefore can go in any order.
132134
m_origCropArea = constrainCropArea(orig_crop_area);

src/dewarping/DewarpingImageTransform.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ class DEWARPING_EXPORT DewarpingImageTransform : public imageproc::AbstractImage
5959
std::vector<QPointF> const& top_curve,
6060
std::vector<QPointF> const& bottom_curve,
6161
DepthPerception const& depth_perception,
62-
DepthPerception const& curve_correct);
62+
DepthPerception const& curve_correct,
63+
DepthPerception const& curve_angle);
6364

6465
virtual ~DewarpingImageTransform();
6566

@@ -118,6 +119,7 @@ class DEWARPING_EXPORT DewarpingImageTransform : public imageproc::AbstractImage
118119
std::vector<QPointF> m_bottomPolyline;
119120
DepthPerception m_depthPerception;
120121
DepthPerception m_curveCorrect;
122+
DepthPerception m_curveAngle;
121123
CylindricalSurfaceDewarper m_dewarper;
122124

123125
/**

src/dewarping/DistortionModel.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ DistortionModel::DistortionModel()
3535
}
3636

3737
DistortionModel::DistortionModel(QDomElement const& el)
38-
: m_topCurve(el.namedItem("top-curve").toElement()),
39-
m_bottomCurve(el.namedItem("bottom-curve").toElement())
38+
: m_topCurve(el.namedItem("top-curve").toElement())
39+
, m_bottomCurve(el.namedItem("bottom-curve").toElement())
4040
{
4141
}
4242

src/dewarping/DistortionModelBuilder.cpp

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@
1616
along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19+
#include <algorithm>
20+
#include <exception>
21+
#include <iterator>
22+
#include <limits>
23+
#include <cmath>
24+
#include <cassert>
25+
#include <boost/foreach.hpp>
26+
#include <boost/random/mersenne_twister.hpp>
27+
#include <boost/random/uniform_int_distribution.hpp>
28+
#include <QTransform>
29+
#include <QImage>
30+
#include <QPainter>
31+
#include <QPen>
32+
#include <QColor>
33+
#include <QDebug>
1934
#include "DistortionModelBuilder.h"
2035
#include "DistortionModel.h"
2136
#include "CylindricalSurfaceDewarper.h"
@@ -27,21 +42,6 @@
2742
#include "VecNT.h"
2843
#include "ToVec.h"
2944
#include "../foundation/MultipleTargetsSupport.h"
30-
#include <QTransform>
31-
#include <QImage>
32-
#include <QPainter>
33-
#include <QPen>
34-
#include <QColor>
35-
#include <QDebug>
36-
#include <boost/foreach.hpp>
37-
#include <boost/random/mersenne_twister.hpp>
38-
#include <boost/random/uniform_int_distribution.hpp>
39-
#include <algorithm>
40-
#include <exception>
41-
#include <iterator>
42-
#include <limits>
43-
#include <cmath>
44-
#include <cassert>
4545

4646
using namespace imageproc;
4747

@@ -55,8 +55,10 @@ struct DistortionModelBuilder::TracedCurve
5555
XSpline extendedSpline;
5656
double order; // Lesser values correspond to upper curves.
5757

58-
TracedCurve(std::vector<QPointF> const& trimmed_polyline,
59-
XSpline const& extended_spline, double ord)
58+
TracedCurve(
59+
std::vector<QPointF> const& trimmed_polyline,
60+
XSpline const& extended_spline,
61+
double ord)
6062
: trimmedPolyline(trimmed_polyline),
6163
extendedPolyline(extended_spline.toPolyline()),
6264
extendedSpline(extended_spline), order(ord) {}
@@ -116,7 +118,7 @@ class DistortionModelBuilder::BadCurve : public std::exception
116118

117119

118120
DistortionModelBuilder::DistortionModelBuilder(Vec2d const& down_direction)
119-
: m_downDirection(down_direction),
121+
: m_downDirection(down_direction),
120122
m_rightDirection(down_direction[1], -down_direction[0])
121123
{
122124
assert(down_direction.squaredNorm() > 0);
@@ -212,11 +214,10 @@ DistortionModelBuilder::tryBuildModel(DebugImages* dbg, QImage const* dbg_backgr
212214

213215
ransac.buildAndAssessModel(&ordered_curves.front(), &ordered_curves.back());
214216

215-
/*
216217
// (Tulon)
217218
// First let's try to combine each of the 5 top-most lines
218219
// with each of the 3 bottom-most ones.
219-
int const exhaustive_search_threshold = 5;
220+
int const exhaustive_search_threshold = 16; // 5 <- 16 -- zvezdochiot
220221
for (int i = 0; i < std::min<int>(exhaustive_search_threshold, num_curves); ++i)
221222
{
222223
for (int j = std::max<int>(0, num_curves - exhaustive_search_threshold); j < num_curves; ++j)
@@ -247,8 +248,8 @@ DistortionModelBuilder::tryBuildModel(DebugImages* dbg, QImage const* dbg_backgr
247248
ransac.buildAndAssessModel(&ordered_curves[i], &ordered_curves[j]);
248249
}
249250
}
250-
*/
251251

252+
/*
252253
// Full RANCAS (zvezdochiot)
253254
for (int i = 0; i < (num_curves - 1); i++)
254255
{
@@ -257,7 +258,7 @@ DistortionModelBuilder::tryBuildModel(DebugImages* dbg, QImage const* dbg_backgr
257258
ransac.buildAndAssessModel(&ordered_curves[i], &ordered_curves[j]);
258259
}
259260
}
260-
261+
*/
261262
if (dbg && dbg_background)
262263
{
263264
dbg->add(visualizeTrimmedPolylines(*dbg_background, ordered_curves), "trimmed_polylines");
@@ -481,7 +482,11 @@ try
481482

482483
double const depth_perception = 2.0; // Doesn't matter much here.
483484
CylindricalSurfaceDewarper const dewarper(
484-
top_curve->extendedPolyline, bottom_curve->extendedPolyline, depth_perception, depth_perception
485+
top_curve->extendedPolyline,
486+
bottom_curve->extendedPolyline,
487+
depth_perception,
488+
depth_perception,
489+
depth_perception
485490
);
486491

487492
// CylindricalSurfaceDewarper maps the curved quadrilateral to a unit
@@ -611,7 +616,8 @@ DistortionModelBuilder::visualizeTrimmedPolylines(
611616

612617
QImage
613618
DistortionModelBuilder::visualizeModel(
614-
QImage const& background, std::vector<TracedCurve> const& curves,
619+
QImage const& background,
620+
std::vector<TracedCurve> const& curves,
615621
RansacModel const& model) const
616622
{
617623
QImage canvas(background.convertToFormat(QImage::Format_RGB32));

src/dewarping/DistortionModelBuilder.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,10 @@ class DEWARPING_EXPORT DistortionModelBuilder
116116
QImage visualizeTrimmedPolylines(
117117
QImage const& background, std::vector<TracedCurve> const& curves) const;
118118

119-
QImage visualizeModel(QImage const& background,
120-
std::vector<TracedCurve> const& curves, RansacModel const& model) const;
119+
QImage visualizeModel(
120+
QImage const& background,
121+
std::vector<TracedCurve> const& curves,
122+
RansacModel const& model) const;
121123

122124
Vec2d m_downDirection;
123125
Vec2d m_rightDirection;

src/stages/deskew/CacheDrivenTask.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ namespace deskew
4343
CacheDrivenTask::CacheDrivenTask(
4444
IntrusivePtr<Settings> const& settings,
4545
IntrusivePtr<select_content::CacheDrivenTask> const& next_task)
46-
: m_ptrNextTask(next_task),
47-
m_ptrSettings(settings)
46+
: m_ptrNextTask(next_task),
47+
m_ptrSettings(settings)
4848
{
4949
}
5050

@@ -117,6 +117,7 @@ CacheDrivenTask::process(
117117
image_transform.origCropArea(),
118118
top_curve, bottom_curve,
119119
dewarping::DepthPerception(),
120+
dewarping::DepthPerception(),
120121
dewarping::DepthPerception()
121122
);
122123
break;
@@ -128,7 +129,8 @@ CacheDrivenTask::process(
128129
params->dewarpingParams().distortionModel().topCurve().polyline(),
129130
params->dewarpingParams().distortionModel().bottomCurve().polyline(),
130131
params->dewarpingParams().depthPerception(),
131-
params->dewarpingParams().depthPerception()
132+
params->dewarpingParams().correctCurves(),
133+
params->dewarpingParams().correctAngle()
132134
);
133135
break;
134136
}

0 commit comments

Comments
 (0)