Skip to content

Commit

Permalink
1.2014.09.14: research: output: process refactoring, dewarp: forward …
Browse files Browse the repository at this point in the history
…ordering of the RANCAS list.
  • Loading branch information
zvezdochiot committed Sep 14, 2024
1 parent bd4de2f commit 1d65dd4
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 95 deletions.
7 changes: 2 additions & 5 deletions src/dewarping/DistortionModelBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,9 @@ DistortionModelBuilder::tryBuildModel(DebugImages* dbg, QImage const* dbg_backgr
// Full RANCAS (zvezdochiot)
for (int i = 0; i < (num_curves - 1); i++)
{
for (int j = (num_curves - 1); j > i; j--)
for (int j = i + 1; j < num_curves; j++)
{
if (i < j)
{
ransac.buildAndAssessModel(&ordered_curves[i], &ordered_curves[j]);
}
ransac.buildAndAssessModel(&ordered_curves[i], &ordered_curves[j]);
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/stages/output/BlackWhiteOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ BlackWhiteOptions::toXml(QDomDocument& doc, QString const& name) const
QDomElement el(doc.createElement(name));
el.setAttribute("thresholdMethod", formatThresholdMethod(m_thresholdMethod));
el.setAttribute("morphology", m_morphology ? "1" : "0");
el.setAttribute("negate", m_negate ? "1" : "0");
if (m_negate)
{
el.setAttribute("negate", "1");
}
if (m_dimmingColoredCoef != 0.0)
{
el.setAttribute("dimmingColoredCoef", m_dimmingColoredCoef);
Expand Down
10 changes: 8 additions & 2 deletions src/stages/output/ColorGrayscaleOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,14 @@ ColorGrayscaleOptions::toXml(QDomDocument& doc, QString const& name) const
el.setAttribute("unPaperIters", m_unPaperIters);
}
el.setAttribute("normalizeCoef", m_normalizeCoef);
el.setAttribute("whiteMargins", m_whiteMargins ? "1" : "0");
el.setAttribute("grayScale", m_grayScale ? "1" : "0");
if (m_whiteMargins)
{
el.setAttribute("whiteMargins", "1");
}
if (m_grayScale)
{
el.setAttribute("grayScale", "1");
}
return el;
}

Expand Down
169 changes: 82 additions & 87 deletions src/stages/output/OutputGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,15 +358,25 @@ OutputGenerator::process(
#endif
);

ColorGrayscaleOptions const& color_options = m_colorParams.colorGrayscaleOptions();
BlackWhiteOptions const& black_white_options = m_colorParams.blackWhiteOptions();
BlackKmeansOptions const& black_kmeans_options = m_colorParams.blackKmeansOptions();
double norm_coef = color_options.normalizeCoef();

// Color filters begin
GrayImage coloredSignificance(transformed_image);
if (!m_outRect.isEmpty())
BinaryImage bw_content(m_outRect.size().expandedTo(QSize(1, 1)), WHITE);
QImage dst;
if (m_outRect.isEmpty())
{
dst = bw_content.toQImage();
}
else
{
ColorGrayscaleOptions const& color_options = m_colorParams.colorGrayscaleOptions();
BlackWhiteOptions const& black_white_options = m_colorParams.blackWhiteOptions();
BlackKmeansOptions const& black_kmeans_options = m_colorParams.blackKmeansOptions();
double norm_coef = color_options.normalizeCoef();

GrayImage coloredSignificance;
BinaryImage colored_mask;
QImage maybe_smoothed;
BinaryImage bw_mask;

// Color filters begin
colored(transformed_image, color_options);

if (norm_coef > 0.0)
Expand Down Expand Up @@ -413,95 +423,88 @@ OutputGenerator::process(

unPaperFilterInPlace(transformed_image, color_options.unPaperIters(), color_options.unPaperCoef());

coloredSignificance = GrayImage(transformed_image);
if (render_params.needBinarization())
{
coloredSignificanceFilterInPlace(transformed_image, coloredSignificance, black_white_options.dimmingColoredCoef());
}
}

status.throwIfCancelled();
// Color filters end
status.throwIfCancelled();
// Color filters end

if (color_options.getflgGrayScale())
{
GrayImage gray(transformed_image);
transformed_image = gray.toQImage();
}

QImage maybe_smoothed;
// We only do smoothing if we are going to do binarization later.
if (render_params.needBinarization())
{
if (black_white_options.morphology())
// We only do smoothing if we are going to do binarization later.
if (render_params.needBinarization())
{
maybe_smoothed = smoothToGrayscale(transformed_image, accel_ops);
if (dbg)
if (black_white_options.morphology())
{
maybe_smoothed = smoothToGrayscale(transformed_image, accel_ops);
if (dbg)
{
dbg->add(maybe_smoothed, "smoothed");
}
}
else
{
maybe_smoothed = QImage(transformed_image);
}
coloredDimmingFilterInPlace(maybe_smoothed, coloredSignificance);
if ((black_white_options.dimmingColoredCoef() > 0.0) && (black_kmeans_options.coloredMaskCoef() > 0.0))
{
dbg->add(maybe_smoothed, "smoothed");
colored_mask = binarizeBiModal(coloredSignificance, (0.5 - black_kmeans_options.coloredMaskCoef()) * 256);
}
else
{
colored_mask = BinaryImage(transformed_image.size(), BLACK);
}
coloredSignificance = GrayImage(); // save memory
}
else

if (color_options.getflgGrayScale())
{
maybe_smoothed = QImage(transformed_image);
GrayImage gray(transformed_image);
transformed_image = gray.toQImage();
}
coloredDimmingFilterInPlace(maybe_smoothed, coloredSignificance);
}

BinaryImage colored_mask;
if ((black_white_options.dimmingColoredCoef() > 0.0) && (black_kmeans_options.coloredMaskCoef() > 0.0))
{
colored_mask = binarizeBiModal(coloredSignificance, (0.5 - black_kmeans_options.coloredMaskCoef()) * 256);
}
else
{
colored_mask = BinaryImage(transformed_image.size(), BLACK);
}
coloredSignificance = GrayImage(); // save memory

status.throwIfCancelled();
status.throwIfCancelled();

BinaryImage bw_mask;
BinaryImage bw_content(m_outRect.size().expandedTo(QSize(1, 1)), WHITE);
if (render_params.binaryOutput() || render_params.mixedOutput() || m_outRect.isEmpty())
{
if (!m_contentRect.isEmpty())
if (render_params.binaryOutput() || render_params.mixedOutput())
{
BinaryImage binarization_mask(bw_content.size(), BLACK);
binarization_mask.fillExcept(m_contentRect, WHITE);

bw_content = binarize(maybe_smoothed, binarization_mask);
maybe_smoothed = QImage(); // Save memory.
binarization_mask.release(); // Save memory.
if (dbg)
if (!m_contentRect.isEmpty())
{
dbg->add(bw_content, "binarized_and_cropped");
}
BinaryImage binarization_mask(bw_content.size(), BLACK);
binarization_mask.fillExcept(m_contentRect, WHITE);

status.throwIfCancelled();
bw_content = binarize(maybe_smoothed, binarization_mask);
maybe_smoothed = QImage(); // Save memory.
binarization_mask.release(); // Save memory.
if (dbg)
{
dbg->add(bw_content, "binarized_and_cropped");
}

morphologicalSmoothInPlace(bw_content, accel_ops);
if (dbg)
{
dbg->add(bw_content, "edges_smoothed");
}
status.throwIfCancelled();

status.throwIfCancelled();
morphologicalSmoothInPlace(bw_content, accel_ops);
if (dbg)
{
dbg->add(bw_content, "edges_smoothed");
}

// We want to keep despeckling the very last operation
// affecting the binary part of the output. That's because
// for "Deskpeckling" tab we will be reconstructing the input
// to this despeckling operation from the final output file.
// That's done to be able to "replay" the despeckling with
// different parameters. Unfortunately we do have that
// applyFillZonesInPlace() call below us, so the reconstruction
// is not accurate. Fortunately, that reconstruction is for
// visualization purposes only and that's the best we can do
// without caching the full-size input-to-despeckling images.
maybeDespeckleInPlace(bw_content, m_despeckleFactor, out_speckles_image, status, dbg);
}
status.throwIfCancelled();

// We want to keep despeckling the very last operation
// affecting the binary part of the output. That's because
// for "Deskpeckling" tab we will be reconstructing the input
// to this despeckling operation from the final output file.
// That's done to be able to "replay" the despeckling with
// different parameters. Unfortunately we do have that
// applyFillZonesInPlace() call below us, so the reconstruction
// is not accurate. Fortunately, that reconstruction is for
// visualization purposes only and that's the best we can do
// without caching the full-size input-to-despeckling images.
maybeDespeckleInPlace(bw_content, m_despeckleFactor, out_speckles_image, status, dbg);
}

if (!m_outRect.isEmpty())
{
bw_mask = BinaryImage(transformed_image.size(), BLACK);
if (render_params.mixedOutput())
{
Expand Down Expand Up @@ -561,17 +564,9 @@ OutputGenerator::process(
{
dbg->add(bw_mask, "colored_mask with zones");
}
applyFillZonesInPlace(bw_content, fill_zones);
}
applyFillZonesInPlace(bw_content, fill_zones);
}

QImage dst;
if (m_outRect.isEmpty())
{
dst = bw_content.toQImage();
}
else
{
if (render_params.whiteMargins())
{
QImage margin = QImage(m_outRect.size(), transformed_image.format());
Expand Down Expand Up @@ -691,10 +686,10 @@ OutputGenerator::process(
/* deprecated: maskMorphological(dst, bw_content, black_kmeans_options.kmeansMorphology()); */
}
}
bw_mask.release(); // Save memory.
colored_mask.release(); // Save memory.
}
bw_content.release(); // Save memory.
bw_mask.release(); // Save memory.
colored_mask.release(); // Save memory.
transformed_image = QImage(); // Save memory.

return dst;
Expand Down

0 comments on commit 1d65dd4

Please sign in to comment.