Move distance transform to RiaImageTools

This commit is contained in:
Gaute Lindkvist 2019-01-22 15:47:25 +01:00
parent 9a3a61811f
commit 19bf92c336
4 changed files with 143 additions and 109 deletions

View File

@ -7,6 +7,7 @@ ${CMAKE_CURRENT_LIST_DIR}/RiaColorTools.h
${CMAKE_CURRENT_LIST_DIR}/RiaEclipseUnitTools.h
${CMAKE_CURRENT_LIST_DIR}/RiaImageCompareReporter.h
${CMAKE_CURRENT_LIST_DIR}/RiaImageFileCompare.h
${CMAKE_CURRENT_LIST_DIR}/RiaImageTools.h
${CMAKE_CURRENT_LIST_DIR}/RiaLogging.h
${CMAKE_CURRENT_LIST_DIR}/RiaProjectModifier.h
${CMAKE_CURRENT_LIST_DIR}/RiaRegressionTest.h
@ -49,6 +50,7 @@ ${CMAKE_CURRENT_LIST_DIR}/RiaColorTools.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaEclipseUnitTools.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaImageCompareReporter.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaImageFileCompare.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaImageTools.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaLogging.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaProjectModifier.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaRegressionTest.cpp

View File

@ -0,0 +1,110 @@
#include "RiaImageTools.h"
#include <algorithm>
//--------------------------------------------------------------------------------------------------
/// Meijster, Roerdink, Hesselink
/// A GENERAL ALGORITHM FOR COMPUTING DISTANCE TRANSFORMS IN LINEAR TIME
/// http://fab.cba.mit.edu/classes/S62.12/docs/Meijster_distance.pdf
/// Currently Euclidean only, but can be easily extended by replacing the lambda functions.
//--------------------------------------------------------------------------------------------------
void RiaImageTools::distanceTransform2d(std::vector<std::vector<unsigned int>>& image)
{
if (image.empty())
{
return;
}
if (image.front().empty())
{
return;
}
const int64_t M = (int64_t)image.size();
const int64_t N = (int64_t)image.front().size();
unsigned int uinf = M + N;
// First phase
std::vector<std::vector<unsigned int>> g(M);
#pragma omp parallel for
for (int64_t x = 0; x < M; ++x)
{
g[x].resize(N, uinf);
if (image[x][0])
{
g[x][0] = 0;
}
for (int64_t y = 1; y < N - 1; ++y)
{
if (image[x][y])
{
g[x][y] = 0;
}
else
{
g[x][y] = 1 + g[x][y - 1];
}
}
for (int64_t y = N - 2; y > 0; --y)
{
if (g[x][y + 1] < g[x][y])
{
g[x][y] = 1 + g[x][y + 1];
}
}
}
auto f = [](int64_t x, int64_t i, const std::vector<std::vector<unsigned int>>& g, int64_t y) {
return (x - i) * (x - i) + g[i][y] * g[i][y];
};
auto sep = [](int64_t i, int64_t u, const std::vector<std::vector<unsigned int>>& g, int64_t y) {
if (i == u) return (int64_t)0;
int64_t numerator = u * u - i * i + g[u][y] * g[u][y] - g[i][y] * g[i][y];
int64_t divisor = 2 * (u - i);
return numerator / divisor;
};
// Second phase
#pragma omp parallel for
for (int64_t y = 0; y < N; ++y)
{
int64_t q = 0;
std::vector<unsigned int> s(std::max(N, M), 0u);
std::vector<unsigned int> t(std::max(N, M), 0u);
for (int64_t u = 1; u < M - 1; ++u)
{
while (q >= 0 && f(t[q], s[q], g, y) > f(t[q], u, g, y))
{
q--;
}
if (q < 0)
{
q = 0;
s[0] = u;
}
else
{
int64_t w = 1 + sep((int64_t)s[q], u, g, y);
if (w < M)
{
q++;
s[q] = u;
t[q] = w;
}
}
}
for (int64_t u = M - 1; u > 0; --u)
{
image[u][y] = f(u, s[q], g, y);
if (u == t[q])
{
q = q - 1;
}
}
}
}

View File

@ -0,0 +1,29 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2019- Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#pragma once
#include <vector>
class RiaImageTools
{
public:
static void distanceTransform2d(std::vector<std::vector<unsigned int>>& image);
};

View File

@ -17,6 +17,7 @@
/////////////////////////////////////////////////////////////////////////////////
#include "RimGeoMechContourMapProjection.h"
#include "RiaImageTools.h"
#include "RiaWeightedGeometricMeanCalculator.h"
#include "RiaWeightedHarmonicMeanCalculator.h"
#include "RiaWeightedMeanCalculator.h"
@ -128,114 +129,6 @@ cvf::ref<cvf::UByteArray> RimGeoMechContourMapProjection::getCellVisibility() co
return cellGridIdxVisibility;
}
//--------------------------------------------------------------------------------------------------
/// Meijster, Roerdink, Hesselink
/// A GENERAL ALGORITHM FOR COMPUTING DISTANCE TRANSFORMS IN LINEAR TIME
/// http://fab.cba.mit.edu/classes/S62.12/docs/Meijster_distance.pdf
//--------------------------------------------------------------------------------------------------
void distanceTransform(std::vector<std::vector<uint>>& image)
{
if (image.empty())
{
return;
}
if (image.front().empty())
{
return;
}
const int64_t M = (int64_t) image.size();
const int64_t N = (int64_t) image.front().size();
unsigned int uinf = M + N;
// First phase
std::vector<std::vector<uint>> g(M);
#pragma omp parallel for
for (int64_t x = 0; x < M; ++x)
{
g[x].resize(N, uinf);
if (image[x][0])
{
g[x][0] = 0;
}
for (int64_t y = 1; y < N - 1; ++y)
{
if (image[x][y])
{
g[x][y] = 0;
}
else
{
g[x][y] = 1 + g[x][y - 1];
}
}
for (int64_t y = N - 2; y > 0; --y)
{
if (g[x][y + 1] < g[x][y])
{
g[x][y] = 1 + g[x][y + 1];
}
}
}
auto f = [](int64_t x, int64_t i, const std::vector<std::vector<uint>>& g, int64_t y)
{
return (x - i) * (x - i) + g[i][y] * g[i][y];
};
auto sep = [](int64_t i, int64_t u, const std::vector<std::vector<uint>>& g, int64_t y)
{
if (i == u) return (int64_t) 0;
int64_t numerator = u * u - i * i + g[u][y] * g[u][y] - g[i][y] * g[i][y];
int64_t divisor = 2 * (u - i);
return numerator / divisor;
};
// Second phase
#pragma omp parallel for
for (int64_t y = 0; y < N; ++y)
{
int64_t q = 0;
std::vector<uint> s(std::max(N, M), 0u);
std::vector<uint> t(std::max(N, M), 0u);
for (int64_t u = 1; u < M - 1; ++u)
{
while (q >= 0 && f(t[q], s[q], g, y) > f(t[q], u, g, y))
{
q--;
}
if (q < 0)
{
q = 0;
s[0] = u;
}
else
{
int64_t w = 1 + sep((int64_t) s[q], u, g, y);
if (w < M)
{
q++;
s[q] = u;
t[q] = w;
}
}
}
for (int64_t u = M - 1; u > 0; --u)
{
image[u][y] = f(u, s[q], g, y);
if (u == t[q])
{
q = q - 1;
}
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@ -345,7 +238,7 @@ std::vector<bool> RimGeoMechContourMapProjection::getMapCellVisibility()
if (m_paddingAroundPorePressureRegion > 0.0)
{
distanceTransform(distanceImage);
RiaImageTools::distanceTransform2d(distanceImage);
cvf::Vec3d porExtent = validPorBoundingBox.extent();
double radius = std::max(porExtent.x(), porExtent.y()) * 0.25;