28 #include "lsst/pex/exceptions.h" 31 namespace pexExcept = lsst::pex::exceptions;
39 _kernelColFunctionPtr(),
40 _kernelRowFunctionPtr(),
53 :
Kernel(width, height, kernelColFunction.getNParameters() + kernelRowFunction.getNParameters(),
55 _kernelColFunctionPtr(kernelColFunction.
clone()),
56 _kernelRowFunctionPtr(kernelRowFunction.
clone()),
58 _localRowList(height),
68 std::vector<Kernel::SpatialFunctionPtr>
const& spatialFunctionList)
69 :
Kernel(width, height, spatialFunctionList),
70 _kernelColFunctionPtr(kernelColFunction.
clone()),
71 _kernelRowFunctionPtr(kernelRowFunction.
clone()),
73 _localRowList(height),
79 spatialFunctionList.size()) {
80 std::ostringstream os;
81 os <<
"kernelColFunction.getNParameters() + kernelRowFunction.getNParameters() = " 83 <<
" != " << spatialFunctionList.size() <<
" = " 84 <<
"spatialFunctionList.size()";
85 throw LSST_EXCEPT(pexExcept::InvalidParameterError, os.str());
92 std::shared_ptr<Kernel> retPtr;
98 *(this->_kernelRowFunctionPtr)));
100 retPtr->setCtr(this->
getCtr());
106 std::shared_ptr<Kernel> retPtr;
108 retPtr = std::make_shared<SeparableKernel>(width, height, *_kernelColFunctionPtr,
111 retPtr = std::make_shared<SeparableKernel>(width, height, *_kernelColFunctionPtr,
112 *_kernelRowFunctionPtr);
118 bool doNormalize,
double x,
double y)
const {
119 if (static_cast<int>(colList.size()) != this->
getWidth() ||
120 static_cast<int>(rowList.size()) != this->
getHeight()) {
121 std::ostringstream os;
122 os <<
"colList.size(), rowList.size() = (" << colList.size() <<
", " << rowList.size() <<
") != (" 124 <<
"kernel dimensions";
125 throw LSST_EXCEPT(pexExcept::InvalidParameterError, os.str());
131 return basicComputeVectors(colList, rowList, doNormalize);
135 return _kernelColFunctionPtr->clone();
139 return _kernelRowFunctionPtr->clone();
143 std::ostringstream os;
144 os << prefix <<
"SeparableKernel:" << std::endl;
146 <<
"..x (width) function: " << (_kernelColFunctionPtr ? _kernelColFunctionPtr->toString() :
"None")
149 <<
"..y (rows) function: " << (_kernelRowFunctionPtr ? _kernelRowFunctionPtr->toString() :
"None")
156 std::vector<double> allParams = _kernelColFunctionPtr->getParameters();
157 std::vector<double> yParams = _kernelRowFunctionPtr->getParameters();
158 std::copy(yParams.begin(), yParams.end(), std::back_inserter(allParams));
167 double imSum = basicComputeVectors(_localColList, _localRowList, doNormalize);
169 for (
int y = 0; y != image.
getHeight(); ++y) {
171 for (std::vector<Pixel>::iterator colIter = _localColList.begin(); colIter != _localColList.end();
172 ++colIter, ++imPtr) {
173 *imPtr = (*colIter) * _localRowList[y];
181 unsigned int const nColParams = _kernelColFunctionPtr->getNParameters();
182 if (ind < nColParams) {
183 _kernelColFunctionPtr->setParameter(ind, value);
185 _kernelRowFunctionPtr->setParameter(ind - nColParams, value);
193 double SeparableKernel::basicComputeVectors(std::vector<Pixel>& colList, std::vector<Pixel>& rowList,
194 bool doNormalize)
const {
196 if (_kernelColCache.empty()) {
197 for (
unsigned int i = 0; i != colList.size(); ++i) {
198 double colFuncValue = (*_kernelColFunctionPtr)(_kernelX[i]);
199 colList[i] = colFuncValue;
200 colSum += colFuncValue;
203 int const cacheSize = _kernelColCache.size();
207 std::vector<double>& cachedValues = _kernelColCache.at(indx);
208 for (
unsigned int i = 0; i != colList.size(); ++i) {
209 double colFuncValue = cachedValues[i];
210 colList[i] = colFuncValue;
211 colSum += colFuncValue;
216 if (_kernelRowCache.empty()) {
217 for (
unsigned int i = 0; i != rowList.size(); ++i) {
218 double rowFuncValue = (*_kernelRowFunctionPtr)(_kernelY[i]);
219 rowList[i] = rowFuncValue;
220 rowSum += rowFuncValue;
223 int const cacheSize = _kernelRowCache.size();
227 std::vector<double>& cachedValues = _kernelRowCache.at(indx);
228 for (
unsigned int i = 0; i != rowList.size(); ++i) {
229 double rowFuncValue = cachedValues[i];
230 rowList[i] = rowFuncValue;
231 rowSum += rowFuncValue;
234 if (indx == cacheSize/2) {
235 if (::fabs(rowFuncValue - (*_kernelRowFunctionPtr)(_kernelX[i])) > 1e-2) {
236 std::cout << indx <<
" " << i <<
" " 237 << rowFuncValue <<
" " 238 << (*_kernelRowFunctionPtr)(_kernelX[i])
246 double imSum = colSum * rowSum;
248 if ((colSum == 0) || (rowSum == 0)) {
249 throw LSST_EXCEPT(pexExcept::OverflowError,
"Cannot normalize; kernel sum is 0");
251 for (std::vector<Pixel>::iterator colIter = colList.begin(); colIter != colList.end(); ++colIter) {
255 for (std::vector<Pixel>::iterator rowIter = rowList.begin(); rowIter != rowList.end(); ++rowIter) {
267 void _computeCache(
int const cacheSize, std::vector<double>
const&
x,
269 if (cacheSize <= 0) {
270 kernelCache->erase(kernelCache->begin(), kernelCache->end());
274 if (kernelCache[0].size() != x.size()) {
275 kernelCache->erase(kernelCache->begin(), kernelCache->end());
278 int const old_cacheSize = kernelCache->size();
280 if (cacheSize == old_cacheSize) {
284 if (cacheSize < old_cacheSize) {
285 kernelCache->erase(kernelCache->begin() + cacheSize, kernelCache->end());
287 kernelCache->resize(cacheSize);
288 for (
int i = old_cacheSize; i != cacheSize; ++i) {
289 (*kernelCache)[i].resize(x.size());
295 for (
int i = 0; i != cacheSize; ++i) {
296 func->setParameter(0, (i + 0.5) / static_cast<double>(cacheSize));
297 for (
unsigned int j = 0; j != x.size(); ++j) {
298 (*kernelCache)[i][j] = (*func)(x[j]);
308 _computeCache(cacheSize, _kernelY, func, &_kernelColCache);
311 _computeCache(cacheSize, _kernelX, func, &_kernelRowCache);
int getHeight() const
Return the Kernel's height.
std::shared_ptr< KernelFunction > KernelFunctionPtr
virtual std::shared_ptr< Kernel > resized(int width, int height) const
Return a pointer to a clone with specified kernel dimensions.
int getHeight() const
Return the number of rows in the image.
_view_t::x_iterator x_iterator
An iterator for traversing the pixels in a row.
lsst::afw::geom::Point2I getCtr() const
Return index of kernel's center.
virtual double getKernelParameter(unsigned int i) const
Return a particular Kernel Parameter (no bounds checking).
virtual int getCacheSize() const
Get the current cache size (0 if none)
virtual std::vector< double > getKernelParameters() const
Return the current kernel parameters.
A Function taking two arguments.
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.
x_iterator row_begin(int y) const
Return an x_iterator to the start of the y'th row.
bool isSpatiallyVarying() const
Return true iff the kernel is spatially varying (has a spatial function)
double computeVectors(std::vector< Pixel > &colList, std::vector< Pixel > &rowList, bool doNormalize, double x=0.0, double y=0.0) const
Compute the column and row arrays in place, where kernel(col, row) = colList(col) * rowList(row) ...
A base class for image defects.
KernelFunctionPtr getKernelRowFunction() const
Get a deep copy of the row kernel function.
KernelFunctionPtr getKernelColFunction() const
Get a deep copy of the col kernel function.
A Function taking one argument.
SeparableKernel()
Construct an empty spatially invariant SeparableKernel of size 0x0.
void setKernelParametersFromSpatialModel(double x, double y) const
Set the kernel parameters from the spatial model (if any).
int getWidth() const
Return the Kernel's width.
virtual void computeCache(int const cacheSize)
Compute a cache of Kernel values, if desired.
virtual std::shared_ptr< Kernel > clone() const
Return a pointer to a deep copy of this kernel.
std::vector< SpatialFunctionPtr > _spatialFunctionList
virtual void setKernelParameter(unsigned int ind, double value) const
Set one kernel parameter.
Kernels are used for convolution with MaskedImages and (eventually) Images.
unsigned int getNParameters() const
Return the number of function parameters.
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the kernel.
virtual double doComputeImage(lsst::afw::image::Image< Pixel > &image, bool doNormalize) const
Low-level version of computeImage.