1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
/******************************************************************************
* Project: GDAL
* Purpose: Correlator
* Author: Andrew Migal, migal.drew@gmail.com
*
******************************************************************************
* Copyright (c) 2012, Andrew Migal
*
* SPDX-License-Identifier: MIT
****************************************************************************/
#include "gdal_simplesurf.h"
/************************************************************************/
/* ==================================================================== */
/* GDALIntegralImage */
/* ==================================================================== */
/************************************************************************/
GDALIntegralImage::GDALIntegralImage() = default;
int GDALIntegralImage::GetHeight()
{
return nHeight;
}
int GDALIntegralImage::GetWidth()
{
return nWidth;
}
void GDALIntegralImage::Initialize(const double **padfImg, int nHeightIn,
int nWidthIn)
{
if (pMatrix)
{
for (int i = 0; i < nHeight; i++)
delete[] pMatrix[i];
delete[] pMatrix;
}
// Memory allocation.
pMatrix = new double *[nHeightIn];
for (int i = 0; i < nHeightIn; i++)
pMatrix[i] = new double[nWidthIn];
nHeight = nHeightIn;
nWidth = nWidthIn;
// Integral image calculation.
for (int i = 0; i < nHeight; i++)
for (int j = 0; j < nWidth; j++)
{
const double val = padfImg[i][j];
double a = 0.0;
double b = 0.0;
double c = 0.0;
if (i - 1 >= 0 && j - 1 >= 0)
a = pMatrix[i - 1][j - 1];
if (j - 1 >= 0)
b = pMatrix[i][j - 1];
if (i - 1 >= 0)
c = pMatrix[i - 1][j];
// New value based on previous calculations.
pMatrix[i][j] = val - a + b + c;
}
}
/*
* Returns value of specified cell.
*/
double GDALIntegralImage::GetValue(int nRow, int nCol)
{
if (!((nRow >= 0 && nRow < nHeight) && (nCol >= 0 && nCol < nWidth)))
return 0;
return pMatrix[nRow][nCol];
}
double GDALIntegralImage::GetRectangleSum(int nRow, int nCol, int nWidthIn,
int nHeightIn)
{
// Left top point of rectangle is first.
const int w = nWidthIn - 1;
const int h = nHeightIn - 1;
const int row = nRow;
const int col = nCol;
// Left top point.
const int lt_row = (row <= nHeight) ? (row - 1) : -1;
const int lt_col = (col <= nWidth) ? (col - 1) : -1;
// Right bottom point of the rectangle.
const int rb_row = (row + h < nHeight) ? (row + h) : (nHeight - 1);
const int rb_col = (col + w < nWidth) ? (col + w) : (nWidth - 1);
double a = 0.0;
double b = 0.0;
double c = 0.0;
double d = 0.0;
if (lt_row >= 0 && lt_col >= 0)
a = GetValue(lt_row, lt_col);
if (lt_row >= 0 && rb_col >= 0)
b = GetValue(lt_row, rb_col);
if (rb_row >= 0 && rb_col >= 0)
c = GetValue(rb_row, rb_col);
if (rb_row >= 0 && lt_col >= 0)
d = GetValue(rb_row, lt_col);
const double res = a + c - b - d;
return res > 0 ? res : 0;
}
double GDALIntegralImage::HaarWavelet_X(int nRow, int nCol, int nSize)
{
return GetRectangleSum(nRow, nCol + nSize / 2, nSize / 2, nSize) -
GetRectangleSum(nRow, nCol, nSize / 2, nSize);
}
double GDALIntegralImage::HaarWavelet_Y(int nRow, int nCol, int nSize)
{
return GetRectangleSum(nRow + nSize / 2, nCol, nSize, nSize / 2) -
GetRectangleSum(nRow, nCol, nSize, nSize / 2);
}
GDALIntegralImage::~GDALIntegralImage()
{
// Clean up memory.
for (int i = 0; i < nHeight; i++)
delete[] pMatrix[i];
delete[] pMatrix;
}
/************************************************************************/
/* ==================================================================== */
/* GDALOctaveLayer */
/* ==================================================================== */
/************************************************************************/
GDALOctaveLayer::GDALOctaveLayer(int nOctave, int nInterval)
: octaveNum(nOctave),
filterSize(3 * static_cast<int>(pow(2.0, nOctave)) * nInterval + 1),
radius((filterSize - 1) / 2), scale(static_cast<int>(pow(2.0, nOctave))),
width(0), height(0), detHessians(nullptr), signs(nullptr)
{
}
void GDALOctaveLayer::ComputeLayer(GDALIntegralImage *poImg)
{
width = poImg->GetWidth();
height = poImg->GetHeight();
// Allocate memory for arrays.
detHessians = new double *[height];
signs = new int *[height];
for (int i = 0; i < height; i++)
{
detHessians[i] = new double[width];
signs[i] = new int[width];
}
// 1/3 of filter side.
const int lobe = filterSize / 3;
// Length of the longer side of the lobe in dxx and dyy filters.
const int longPart = 2 * lobe - 1;
const int normalization = filterSize * filterSize;
// Loop over image pixels.
// Filter should remain into image borders.
for (int r = radius; r <= height - radius; r++)
for (int c = radius; c <= width - radius; c++)
{
// Values of Fast Hessian filters.
double dxx =
poImg->GetRectangleSum(r - lobe + 1, c - radius, filterSize,
longPart) -
3 * poImg->GetRectangleSum(r - lobe + 1, c - (lobe - 1) / 2,
lobe, longPart);
double dyy = poImg->GetRectangleSum(r - radius, c - lobe - 1,
longPart, filterSize) -
3 * poImg->GetRectangleSum(r - lobe + 1, c - lobe + 1,
longPart, lobe);
double dxy =
poImg->GetRectangleSum(r - lobe, c - lobe, lobe, lobe) +
poImg->GetRectangleSum(r + 1, c + 1, lobe, lobe) -
poImg->GetRectangleSum(r - lobe, c + 1, lobe, lobe) -
poImg->GetRectangleSum(r + 1, c - lobe, lobe, lobe);
dxx /= normalization;
dyy /= normalization;
dxy /= normalization;
// Memorize Hessian values and their signs.
detHessians[r][c] = dxx * dyy - 0.9 * 0.9 * dxy * dxy;
signs[r][c] = (dxx + dyy >= 0) ? 1 : -1;
}
}
GDALOctaveLayer::~GDALOctaveLayer()
{
for (int i = 0; i < height; i++)
{
delete[] detHessians[i];
delete[] signs[i];
}
delete[] detHessians;
delete[] signs;
}
/************************************************************************/
/* ==================================================================== */
/* GDALOctaveMap */
/* ==================================================================== */
/************************************************************************/
GDALOctaveMap::GDALOctaveMap(int nOctaveStartIn, int nOctaveEndIn)
: pMap(new GDALOctaveLayer **[nOctaveEndIn]), octaveStart(nOctaveStartIn),
octaveEnd(nOctaveEndIn)
{
for (int i = 0; i < octaveEnd; i++)
pMap[i] = new GDALOctaveLayer *[INTERVALS];
for (int oct = octaveStart; oct <= octaveEnd; oct++)
for (int i = 1; i <= INTERVALS; i++)
pMap[oct - 1][i - 1] = new GDALOctaveLayer(oct, i);
}
void GDALOctaveMap::ComputeMap(GDALIntegralImage *poImg)
{
for (int oct = octaveStart; oct <= octaveEnd; oct++)
for (int i = 1; i <= INTERVALS; i++)
pMap[oct - 1][i - 1]->ComputeLayer(poImg);
}
bool GDALOctaveMap::PointIsExtremum(int row, int col, GDALOctaveLayer *bot,
GDALOctaveLayer *mid, GDALOctaveLayer *top,
double threshold)
{
// Check that point in middle layer has all neighbors.
if (row <= top->radius || col <= top->radius ||
row + top->radius >= top->height || col + top->radius >= top->width)
return false;
const double curPoint = mid->detHessians[row][col];
// Hessian should be higher than threshold.
if (curPoint < threshold)
return false;
// Hessian should be higher than Hessians of all neighbors.
for (int i = -1; i <= 1; i++)
for (int j = -1; j <= 1; j++)
{
const double topPoint = top->detHessians[row + i][col + j];
const double midPoint = mid->detHessians[row + i][col + j];
const double botPoint = bot->detHessians[row + i][col + j];
if (topPoint >= curPoint || botPoint >= curPoint)
return false;
if (i != 0 || j != 0)
if (midPoint >= curPoint)
return false;
}
return true;
}
GDALOctaveMap::~GDALOctaveMap()
{
// Clean up Octave layers.
for (int oct = octaveStart; oct <= octaveEnd; oct++)
for (int i = 0; i < INTERVALS; i++)
delete pMap[oct - 1][i];
// Clean up allocated memory.
for (int oct = 0; oct < octaveEnd; oct++)
delete[] pMap[oct];
delete[] pMap;
}
|