Skip to content

Commit 853453c

Browse files
committed
1.2025.01.11: dewarp: fix density thresholds
1 parent 17ea9fe commit 853453c

6 files changed

+203
-111
lines changed

src/dewarping/CylindricalSurfaceDewarper.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ CylindricalSurfaceDewarper::mapGeneratrix(double crv_x, State& state) const
118118
{
119119
double const pln_x = m_arcLengthMapper.arcLenToX(crv_x, state.m_arcLengthHint);
120120

121-
double const lin_y1 = -0.02 * (m_curveAngle - 2.0);
121+
double const lin_y1 = -0.05 * (m_curveAngle - 2.0);
122122
double const lin_y2 = -lin_y1;
123123
double const lin_y = lin_y1 + (lin_y2 - lin_y1) * pln_x;
124124

@@ -152,13 +152,11 @@ CylindricalSurfaceDewarper::mapGeneratrix(double crv_x, State& state) const
152152

153153
QPointF const img_straight_line_pt(toPoint(m_pln2img(Vector2d(pln_x, img_directrix12fs_proj))));
154154
double const img_straight_line_proj(projector.projectionScalar(img_straight_line_pt));
155-
double const img_straight_line_proj_st = pln_straight_line_frac * img_directrix12fs_proj
156-
+ (1.0 - pln_straight_line_frac) * img_straight_line_proj;
157155

158156
boost::array<std::pair<double, double>, 3> pairs;
159157
pairs[0] = std::make_pair(0.0, img_directrix1_proj);
160158
pairs[1] = std::make_pair(1.0, img_directrix2_proj);
161-
pairs[2] = std::make_pair(pln_straight_line_y, img_straight_line_proj_st);
159+
pairs[2] = std::make_pair(pln_straight_line_y, img_straight_line_proj);
162160

163161
HomographicTransform<1, double> H(threePoint1DHomography(pairs));
164162

@@ -178,7 +176,7 @@ CylindricalSurfaceDewarper::mapToDewarpedSpace(QPointF const& img_pt, State& sta
178176
double const pln_x = m_img2pln(toVec(img_pt))[0];
179177
double const crv_x = m_arcLengthMapper.xToArcLen(pln_x, state.m_arcLengthHint);
180178

181-
double const lin_y1 = -0.02 * (m_curveAngle - 2.0);
179+
double const lin_y1 = -0.05 * (m_curveAngle - 2.0);
182180
double const lin_y2 = -lin_y1;
183181
double const lin_y = lin_y1 + (lin_y2 - lin_y1) * pln_x;
184182

@@ -212,13 +210,11 @@ CylindricalSurfaceDewarper::mapToDewarpedSpace(QPointF const& img_pt, State& sta
212210

213211
QPointF const img_straight_line_pt(toPoint(m_pln2img(Vector2d(pln_x, img_directrix12fs_proj))));
214212
double const img_straight_line_proj(projector.projectionScalar(img_straight_line_pt));
215-
double const img_straight_line_proj_st = pln_straight_line_frac * img_directrix12fs_proj
216-
+ (1.0 - pln_straight_line_frac) * img_straight_line_proj;
217213

218214
boost::array<std::pair<double, double>, 3> pairs;
219215
pairs[0] = std::make_pair(img_directrix1_proj, 0.0);
220216
pairs[1] = std::make_pair(img_directrix2_proj, 1.0);
221-
pairs[2] = std::make_pair(img_straight_line_proj_st, pln_straight_line_y);
217+
pairs[2] = std::make_pair(img_straight_line_proj, pln_straight_line_y);
222218

223219
HomographicTransform<1, double> const H(threePoint1DHomography(pairs));
224220

@@ -323,7 +319,7 @@ CylindricalSurfaceDewarper::fourPoint2DHomography(
323319
Matrix<double, 8, 1> const h(qr.solve(b));
324320
Matrix3d H;
325321
H << h[0], h[1], h[2], h[3], h[4], h[5], h[6], h[7], 1.0;
326-
322+
327323
return HomographicTransform<2, double>(H);
328324
}
329325

@@ -349,7 +345,7 @@ CylindricalSurfaceDewarper::threePoint1DHomography(
349345
auto qr = A.colPivHouseholderQr();
350346
if (!qr.isInvertible())
351347
{
352-
throw std::runtime_error("Failed to build 2D homography");
348+
throw std::runtime_error("Failed to build curves homography");
353349
}
354350

355351
Matrix<double, 3, 1> const h(qr.solve(b));

src/dewarping/CylindricalSurfaceDewarper.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@
1919
#ifndef DEWARPING_CYLINDRICAL_SURFACE_DEWARPER_H_
2020
#define DEWARPING_CYLINDRICAL_SURFACE_DEWARPER_H_
2121

22-
#include "dewarping_config.h"
23-
#include "HomographicTransform.h"
24-
#include "PolylineIntersector.h"
25-
#include "ArcLengthMapper.h"
26-
#include <boost/array.hpp>
2722
#include <vector>
2823
#include <utility>
2924
#include <QPointF>
3025
#include <QLineF>
26+
#include <boost/array.hpp>
27+
#include "dewarping_config.h"
28+
#include "HomographicTransform.h"
29+
#include "PolylineIntersector.h"
30+
#include "ArcLengthMapper.h"
3131

3232
namespace dewarping
3333
{

src/dewarping/DetectVerticalBounds.cpp

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

19+
#include <cmath>
20+
#include <utility>
21+
#include <cassert>
22+
#include <boost/random/mersenne_twister.hpp>
23+
#include <boost/random/uniform_int_distribution.hpp>
1924
#include "DetectVerticalBounds.h"
2025
#include "NumericTraits.h"
2126
#include "ToLineProjector.h"
2227
#include "LineIntersectionScalar.h"
2328
#include "imageproc/Constants.h"
24-
#include <boost/random/mersenne_twister.hpp>
25-
#include <boost/random/uniform_int_distribution.hpp>
26-
#include <cmath>
27-
#include <utility>
28-
#include <cassert>
2929

3030
using namespace imageproc;
3131

@@ -42,7 +42,8 @@ enum LeftRightSide
4242
};
4343

4444
QLineF findVerticalBound(std::vector<QLineF> const& chords,
45-
double const dist_threshold, LeftRightSide const side)
45+
double const dist_threshold,
46+
LeftRightSide const side)
4647
{
4748
if (chords.size() < 2)
4849
{
@@ -125,7 +126,8 @@ QLineF findVerticalBound(std::vector<QLineF> const& chords,
125126
} // anonymous namespace
126127

127128
std::pair<QLineF, QLineF> detectVerticalBounds(
128-
std::list<std::vector<QPointF>> const& curves, double const dist_threshold)
129+
std::list<std::vector<QPointF>> const& curves,
130+
double const dist_threshold)
129131
{
130132
std::vector<QLineF> chords;
131133
chords.reserve(curves.size());

src/dewarping/DewarpingImageTransform.cpp

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,11 @@ DewarpingImageTransform::DewarpingImageTransform(
126126
, m_depthPerception(depth_perception)
127127
, m_curveCorrect(curve_correct)
128128
, m_curveAngle(curve_angle)
129-
, m_dewarper(top_curve, bottom_curve, depth_perception.value(), curve_correct.value(), curve_angle.value())
129+
, m_dewarper(top_curve,
130+
bottom_curve,
131+
depth_perception.value(),
132+
curve_correct.value(),
133+
curve_angle.value())
130134
, m_intrinsicScaleX(1.0)
131135
, m_intrinsicScaleY(1.0)
132136
, m_userScaleX(1.0)
@@ -147,7 +151,11 @@ DewarpingImageTransform::fingerprint() const
147151
RoundingHasher hash(QCryptographicHash::Sha1);
148152

149153
hash << "DewarpingImageTransform";
150-
hash << m_origSize << m_origCropArea << m_depthPerception.value() << m_curveCorrect.value() << m_curveAngle.value();
154+
hash << m_origSize
155+
<< m_origCropArea
156+
<< m_depthPerception.value()
157+
<< m_curveCorrect.value()
158+
<< m_curveAngle.value();
151159

152160
for (QPointF const& pt : m_topPolyline)
153161
{
@@ -200,7 +208,8 @@ DewarpingImageTransform::scale(double xscale, double yscale)
200208

201209
AffineTransformedImage
202210
DewarpingImageTransform::toAffine(
203-
QImage const& image, QColor const& outside_color,
211+
QImage const& image,
212+
QColor const& outside_color,
204213
std::shared_ptr<AcceleratableOperations> const& accel_ops) const
205214
{
206215
assert(!image.isNull());
@@ -261,15 +270,18 @@ DewarpingImageTransform::toAffine() const
261270
}
262271

263272
QImage
264-
DewarpingImageTransform::materialize(QImage const& image,
265-
QRect const& target_rect,
266-
QColor const& outside_color,
267-
std::shared_ptr<AcceleratableOperations> const& accel_ops) const
273+
DewarpingImageTransform::materialize(
274+
QImage const& image,
275+
QRect const& target_rect,
276+
QColor const& outside_color,
277+
std::shared_ptr<AcceleratableOperations> const& accel_ops) const
268278
{
269279
assert(!image.isNull());
270280
assert(!target_rect.isEmpty());
271281

272-
QRectF model_domain(0, 0, m_intrinsicScaleX * m_userScaleX, m_intrinsicScaleY * m_userScaleY);
282+
QRectF model_domain(0, 0,
283+
m_intrinsicScaleX * m_userScaleX,
284+
m_intrinsicScaleY * m_userScaleY);
273285
model_domain.translate(-target_rect.topLeft());
274286
auto const minmax_densities = calcMinMaxDensities();
275287

@@ -289,7 +301,8 @@ DewarpingImageTransform::forwardMapper() const
289301
{
290302
auto dewarper(std::make_shared<CylindricalSurfaceDewarper>(m_dewarper));
291303
QTransform post_transform;
292-
post_transform.scale(m_intrinsicScaleX * m_userScaleX, m_intrinsicScaleY * m_userScaleY);
304+
post_transform.scale(m_intrinsicScaleX * m_userScaleX,
305+
m_intrinsicScaleY * m_userScaleY);
293306

294307
return [=](QPointF const& pt)
295308
{
@@ -435,7 +448,8 @@ DewarpingImageTransform::constrainCropArea(QPolygonF const& orig_crop_area) cons
435448
double const min_density = minmax_densities.first;
436449
double const max_density = minmax_densities.second;
437450

438-
ConstrainedCropAreaBuilder builder(orig_crop_area, min_density, max_density, m_dewarper);
451+
ConstrainedCropAreaBuilder builder(orig_crop_area,
452+
min_density, max_density, m_dewarper);
439453

440454
builder.sampleCrvXRange(0.0 + 0.3, 0.0 - 0.6, -1.0);
441455
builder.sampleCrvXRange(1.0 - 0.3, 1.0 + 0.6, 1.0);
@@ -469,7 +483,8 @@ DewarpingImageTransform::calcMinMaxDensities() const
469483
auto const minmax_densities = std::minmax_element(
470484
std::begin(corner_densities), std::end(corner_densities)
471485
);
472-
return std::make_pair(0.6 * *minmax_densities.first, 1.4 * *minmax_densities.second);
486+
/* return std::make_pair(0.6 * *minmax_densities.first, 1.4 * *minmax_densities.second); // Tulon */
487+
return std::make_pair(0.5 * *minmax_densities.first, 2.0 * *minmax_densities.second); // zvezdochiot (2025-01-11)
473488
}
474489

475490

@@ -509,7 +524,8 @@ DewarpingImageTransform::ConstrainedCropAreaBuilder::sampleCrvXRange(
509524
{
510525

511526
auto segment_it = processGeneratrix(
512-
crv_x, m_dewarper.mapGeneratrix(crv_x, m_dewarpingState)
527+
crv_x,
528+
m_dewarper.mapGeneratrix(crv_x, m_dewarpingState)
513529
);
514530
if (segment_it == m_vertSegments.end())
515531
{
@@ -522,7 +538,9 @@ DewarpingImageTransform::ConstrainedCropAreaBuilder::sampleCrvXRange(
522538
if (last_segment)
523539
{
524540
maybeAddExtraVerticalSegments(
525-
last_segment->crv_x, last_segment->length, segment_it->first, segment_len
541+
last_segment->crv_x,
542+
last_segment->length,
543+
segment_it->first, segment_len
526544
);
527545
}
528546

@@ -653,7 +671,8 @@ DewarpingImageTransform::ConstrainedCropAreaBuilder::maybeAddExtraVerticalSegmen
653671

654672
double const mid_crv_x = 0.5 * (segment1_crv_x + segment2_crv_x);
655673
auto const segment_it = processGeneratrix(
656-
mid_crv_x, m_dewarper.mapGeneratrix(mid_crv_x, m_dewarpingState)
674+
mid_crv_x,
675+
m_dewarper.mapGeneratrix(mid_crv_x, m_dewarpingState)
657676
);
658677
if (segment_it == m_vertSegments.end())
659678
{

src/dewarping/DistortionModelBuilder.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -302,15 +302,16 @@ DistortionModelBuilder::centroid(std::vector<QPointF> const& polyline)
302302
return Vec2d(polyline.front());
303303
}
304304

305-
Vec2d accum(0, 0);
306-
double total_weight = 0;
305+
Vec2d accum(0.0, 0.0);
306+
double total_weight = 0.0;
307307

308308
for (int i = 1; i < num_points; ++i)
309309
{
310310
QLineF const segment(polyline[i - 1], polyline[i]);
311311
Vec2d const center(0.5 * (segment.p1() + segment.p2()));
312312
double const weight = segment.length();
313-
accum += center * weight;
313+
Vec2d const delta(center * weight);
314+
accum += delta;
314315
total_weight += weight;
315316
}
316317

@@ -320,7 +321,7 @@ DistortionModelBuilder::centroid(std::vector<QPointF> const& polyline)
320321
}
321322
else
322323
{
323-
return accum / total_weight;
324+
return Vec2d(accum / total_weight);
324325
}
325326
}
326327

@@ -574,7 +575,7 @@ DistortionModelBuilder::visualizeTrimmedPolylines(
574575

575576
int const width = background.width();
576577
int const height = background.height();
577-
double const stroke_width = sqrt(double(width * width + height * height)) / 500;
578+
double const stroke_width = sqrt(double(width * width + height * height)) / 500.0;
578579

579580
// Extend / trim bounds.
580581
QLineF bound1(m_bound1);
@@ -603,7 +604,7 @@ DistortionModelBuilder::visualizeTrimmedPolylines(
603604
painter.setPen(Qt::NoPen);
604605
BOOST_FOREACH(TracedCurve const& curve, curves)
605606
{
606-
QRectF rect(0, 0, stroke_width, stroke_width);
607+
QRectF rect(0.0, 0.0, stroke_width, stroke_width);
607608
BOOST_FOREACH(QPointF const& knot, curve.trimmedPolyline)
608609
{
609610
rect.moveCenter(knot);
@@ -626,7 +627,7 @@ DistortionModelBuilder::visualizeModel(
626627

627628
int const width = background.width();
628629
int const height = background.height();
629-
double const stroke_width = sqrt(double(width * width + height * height)) / 500;
630+
double const stroke_width = sqrt(double(width * width + height * height)) / 500.0;
630631

631632
// Extend / trim bounds.
632633
QLineF bound1(m_bound1);

0 commit comments

Comments
 (0)