📄 subpixeledge.cpp
字号:
// SubpixelEdge.cpp - by Robin Hewitt, 2004
// http://www.robinhewitt.com/mavis
// This is free software. See license at the bottom
// of this file for details.
//
#include "SubpixelEdge.h"
#include <memory.h>
#include <math.h>
//////////////////////////////////////////////////////////////
// Implementation of the SubpixelEdge classes
//
const int SubpixelEdge::MAX = 1;
const int SubpixelEdge::MIN = 2;
const int SubpixelEdge::MAX_ABSVAL = 3;
const int SubpixelEdge::MIN_ABSVAL = 4;
SubpixelEdge::SubpixelEdge(
int * depVal, // dependent variable for each region pixel
int indepMin, // min value of independent variable
int indepMax, // max value of independent variable
int * indepVal, // independent variable for each region pixel
int nPx,
//IntImg * pImg,
MVImg<int> * pImg,
//int (*imgVal)(IntImg *,int,int),
int (*imgVal)(MVImg<int> *,int,int),
int cmpType
) {
indepVarLo = indepMin;
nPts = 1 + indepMax - indepVarLo;
int * pixelLoc = new int[nPts];
bool * isSet = new bool[nPts];
subpixelLoc = new double[nPts];
memset(isSet, 0, nPts*sizeof(bool));
bool (*cmpFunc)(int, int);
switch(cmpType)
{
case MAX:
cmpFunc = isGT;
break;
case MIN:
cmpFunc = isLT;
break;
case MIN_ABSVAL:
cmpFunc = absValIsLT;
break;
case MAX_ABSVAL:
default:
cmpFunc = absValIsGT;
break;
}
// locate edge pixels at the extreme values in the image along
// the dependent coord for each position of the independent variable
for(int px=0; px<nPx; px++)
{
int iPt = indepVal[px] - indepVarLo;
if( isSet[iPt] )
{
int i1 = imgVal( pImg, indepVal[iPt], depVal[iPt] );
int i2 = imgVal( pImg, indepVal[iPt], pixelLoc[iPt] );
if( cmpFunc(i1, i2) ) pixelLoc[iPt] = depVal[iPt];
}
else
{
pixelLoc[iPt] = depVal[iPt];
isSet[iPt] = true;
}
}
// use a parabola fit to find subpixel location
for(int iPt=0; iPt<nPts; iPt++)
{
double offset;
bool applyOffset;
// get index of pixels to each side of current one
int prevPx = pixelLoc[iPt] - 1;
int nextPx = pixelLoc[iPt] + 1;
// find image values for all three pixels - use abs values
int ctrVal = abs(imgVal( pImg, indepVarLo + iPt, pixelLoc[iPt] ));
int prevVal = abs(imgVal( pImg, indepVarLo + iPt, prevPx ));
int nextVal = abs(imgVal( pImg, indepVarLo + iPt, nextPx ));
// compute offset
if( prevVal == nextVal )
applyOffset = false;
else
{
offset = (double)(prevVal-nextVal) /
(double)( 2*(prevVal+nextVal-ctrVal-ctrVal) );
applyOffset = true;
}
// set location
subpixelLoc[iPt] = applyOffset?
offset + (double)pixelLoc[iPt] : (double)pixelLoc[iPt];
// update min and max
if(iPt)
{
if(subpixelLoc[iPt] < edgeMin) edgeMin = subpixelLoc[iPt];
else if(subpixelLoc[iPt] > edgeMax) edgeMax = subpixelLoc[iPt];
}
else
edgeMin = edgeMax = subpixelLoc[0];
}
// free the work buffers
delete[] pixelLoc;
delete[] isSet;
}
SubpixelEdge::~SubpixelEdge()
{
delete[] subpixelLoc;
}
void SubpixelEdge::offsetDepVar(double offset)
{
for(int i=0; i<nPts; i++) subpixelLoc[i] += offset;
edgeMin += offset;
edgeMax += offset;
}
bool SubpixelEdge::isGT(int i1, int i2)
{
return (i1 > i2);
}
bool SubpixelEdge::isLT(int i1, int i2)
{
return (i1 > i2);
}
bool SubpixelEdge::absValIsGT(int i1, int i2)
{
return ( abs(i1) > abs(i2) );
}
bool SubpixelEdge::absValIsLT(int i1, int i2)
{
return ( abs(i1) < abs(i2) );
}
int SubpixelEdge::getNPoints() {return nPts;}
//////////////////////////////////////////////////////////////
// Implementation of the YofXSubpixelEdge class
//
//YofXSubpixelEdge::YofXSubpixelEdge(Region *pRegion, IntImg * pImg, int cmpType) :
YofXSubpixelEdge::YofXSubpixelEdge(Region *pRegion, MVImg<int> * pImg, int cmpType) :
SubpixelEdge(
pRegion->getPxY(), // values of dependent variable
pRegion->getMinX(), // min value of independent variable
pRegion->getMaxX(), // max value of independent variable
pRegion->getPxX(), // values of independent variable
pRegion->getNPixels(),
pImg,
imgVal,
cmpType
) {}
//int YofXSubpixelEdge::imgVal(IntImg * pImg, int x, int y)
int YofXSubpixelEdge::imgVal(MVImg<int> * pImg, int x, int y)
{
return pImg->getYXData()[y][x];
}
//////////////////////////////////////////////////////////////
// Implementation of the XofYSubpixelEdge class
//
//XofYSubpixelEdge::XofYSubpixelEdge(Region *pRegion, IntImg * pImg, int cmpType) :
XofYSubpixelEdge::XofYSubpixelEdge(Region *pRegion, MVImg<int> * pImg, int cmpType) :
SubpixelEdge(
pRegion->getPxX(), // values of dependent variable
pRegion->getMinY(), // min value of independent variable
pRegion->getMaxY(), // max value of independent variable
pRegion->getPxY(), // values of independent variable
pRegion->getNPixels(),
pImg,
imgVal,
cmpType
) {}
//int XofYSubpixelEdge::imgVal(IntImg * pImg, int y, int x)
int XofYSubpixelEdge::imgVal(MVImg<int> * pImg, int y, int x)
{
return pImg->getYXData()[y][x];
}
///////////////////////////////////////////////////////////////////////////////////////
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this
// license. If you do not agree to this license, do not download, install, copy or
// use the software.
//
//
// Mavis License Agreement
//
// Copyright (c) 2004, Robin Hewitt (http://www.robin-hewitt.com).
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// This software is provided "as is" and any express or implied warranties, including,
// but not limited to, the implied warranties of merchantability and fitness for a
// particular purpose are disclaimed. In no event shall the authors or contributors be
// liable for any direct, indirect, incidental, special, exemplary, or consequential
// damages (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused and on any
// theory of liability, whether in contract, strict liability, or tort (including
// negligence or otherwise) arising in any way out of the use of this software, even
// if advised of the possibility of such damage.
///////////////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -