29 #include "boost/format.hpp"
51 _kernelImagePtrList(),
54 _isDeltaFunctionBasis(false) {}
58 :
Kernel(kernelList[0]->getWidth(), kernelList[0]->getHeight(), kernelList.size()),
60 _kernelImagePtrList(),
62 _kernelParams(kernelParameters),
63 _isDeltaFunctionBasis(false) {
64 if (kernelList.
size() != kernelParameters.
size()) {
66 os <<
"kernelList.size() = " << kernelList.
size() <<
" != " << kernelParameters.
size() <<
" = "
67 <<
"kernelParameters.size()";
71 _setKernelList(kernelList);
76 :
Kernel(kernelList[0]->getWidth(), kernelList[0]->getHeight(), kernelList.size(), spatialFunction),
78 _kernelImagePtrList(),
80 _kernelParams(
std::vector<double>(kernelList.size())),
81 _isDeltaFunctionBasis(false) {
83 _setKernelList(kernelList);
88 :
Kernel(kernelList[0]->getWidth(), kernelList[0]->getHeight(), spatialFunctionList),
90 _kernelImagePtrList(),
92 _kernelParams(
std::vector<double>(kernelList.size())),
93 _isDeltaFunctionBasis(false) {
94 if (kernelList.
size() != spatialFunctionList.
size()) {
96 os <<
"kernelList.size() = " << kernelList.
size() <<
" != " << spatialFunctionList.
size() <<
" = "
97 <<
"spatialFunctionList.size()";
101 _setKernelList(kernelList);
111 retPtr->setCtr(this->
getCtr());
119 kernelList.
push_back(kIter->resized(width, height));
126 retPtr = std::make_shared<LinearCombinationKernel>(kernelList, _kernelParams);
133 if (kernelList.
size() < 1) {
140 for (
unsigned int ii = 0; ii < kernelList.
size(); ++ii) {
143 (
boost::format(
"kernel %d has different size than kernel 0") % ii).str());
145 if (kernelList[ii]->
getCtr() != ctr0) {
147 (
boost::format(
"kernel %d has different center than kernel 0") % ii).str());
151 (
boost::format(
"kernel %d is spatially varying") % ii).str());
177 KernelImageList newKernelImagePtrList;
178 newKernelImagePtrList.reserve(nSpatialParameters);
179 for (
int i = 0; i < nSpatialParameters; ++i) {
180 KernelImagePtr kernelImagePtr(
new KernelImage(this->
getDimensions()));
181 newKernelImagePtrList.push_back(kernelImagePtr);
186 KernelList::const_iterator kIter = _kernelList.
begin();
187 KernelList::const_iterator
const kEnd = _kernelList.
end();
188 auto &firstSpFunc = *firstSpFuncPtr;
189 auto &firstType =
typeid(firstSpFunc);
190 for (; kIter != kEnd; ++kIter, ++spFuncPtrIter) {
191 auto &spFunc = **spFuncPtrIter;
192 if (
typeid(spFunc) != firstType) {
196 (**kIter).computeImage(kernelImage,
false);
197 for (
int i = 0; i < nSpatialParameters; ++i) {
198 double spParam = (*spFuncPtrIter)->getParameter(i);
199 newKernelImagePtrList[i]->scaledPlus(spParam, kernelImage);
207 newKernelList.
reserve(nSpatialParameters);
208 KernelImageList::iterator newKImPtrIter = newKernelImagePtrList.begin();
209 KernelImageList::iterator
const newKImPtrEnd = newKernelImagePtrList.end();
210 for (; newKImPtrIter != newKImPtrEnd; ++newKImPtrIter) {
214 for (
int i = 0; i < nSpatialParameters; ++i) {
216 newSpParameters[i] = 1.0;
219 newSpFunctionPtrList.
push_back(newSpFunctionPtr);
223 refactoredKernel->setCtr(this->
getCtr());
224 return refactoredKernel;
231 for (KernelList::const_iterator i = _kernelList.
begin(); i != _kernelList.
end(); ++i) {
232 os << (*i)->toString(
prefix +
"\t");
234 os <<
"..parameters: [ ";
236 if (i != _kernelParams.
begin())
os <<
", ";
251 _kernelImagePtrList.
begin();
254 for (; kImPtrIter != _kernelImagePtrList.
end(); ++kImPtrIter, ++kSumIter, ++kParIter) {
255 image.scaledPlus(*kParIter, **kImPtrIter);
256 imSum += (*kSumIter) * (*kParIter);
271 this->_kernelParams[ind] = value;
277 void LinearCombinationKernel::_setKernelList(
KernelList const &kernelList) {
278 _kernelSumList.
clear();
279 _kernelImagePtrList.
clear();
281 _isDeltaFunctionBasis =
true;
282 for (KernelList::const_iterator kIter = kernelList.
begin(), kEnd = kernelList.
end(); kIter != kEnd;
286 _isDeltaFunctionBasis =
false;
290 _kernelSumList.
push_back(basisKernelPtr->computeImage(*kernelImagePtr,
false));
291 _kernelImagePtrList.
push_back(kernelImagePtr);
299 struct LinearCombinationKernelPersistenceHelper :
public Kernel::PersistenceHelper {
303 LinearCombinationKernelPersistenceHelper(
int nComponents,
bool isSpatiallyVarying)
304 : Kernel::PersistenceHelper(isSpatiallyVarying ? nComponents : 0),
305 components(
schema.addField<table::Array<int>>(
"components",
"archive IDs of component kernel",
307 if (!isSpatiallyVarying) {
308 amplitudes =
schema.addField<table::Array<double>>(
"amplitudes",
"amplitudes component kernel",
313 LinearCombinationKernelPersistenceHelper(table::Schema
const &schema_)
315 if (!spatialFunctions.isValid()) {
332 LinearCombinationKernelPersistenceHelper
const keys(catalogs.
front().getSchema());
337 componentList[i] = archive.
get<
Kernel>(record[
keys.components[i]]);
340 if (
keys.spatialFunctions.isValid()) {
346 kernelParameters[i] = record[
keys.amplitudes[i]];
360 std::string getLinearCombinationKernelPersistenceName() {
return "LinearCombinationKernel"; }
367 return getLinearCombinationKernelPersistenceName();
375 for (
int n = 0; n <
keys.components.getSize(); ++n) {
376 record->set(
keys.components[n], handle.
put(_kernelList[n]));
380 for (
int n = 0; n <
keys.components.getSize(); ++n) {
381 record->set(
keys.components[n], handle.
put(_kernelList[n]));
382 record->set(
keys.amplitudes[n], _kernelParams[n]);