/usr/include/vigra/multi_blockwise.hxx is in libvigraimpex-dev 1.10.0+git20160211.167be93+dfsg-2+b5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 | /************************************************************************/
/* */
/* Copyright 2015 by Thorsten Beier */
/* */
/* This file is part of the VIGRA computer vision library. */
/* The VIGRA Website is */
/* http://hci.iwr.uni-heidelberg.de/vigra/ */
/* Please direct questions, bug reports, and contributions to */
/* ullrich.koethe@iwr.uni-heidelberg.de or */
/* vigra@informatik.uni-hamburg.de */
/* */
/* Permission is hereby granted, free of charge, to any person */
/* obtaining a copy of this software and associated documentation */
/* files (the "Software"), to deal in the Software without */
/* restriction, including without limitation the rights to use, */
/* copy, modify, merge, publish, distribute, sublicense, and/or */
/* sell copies of the Software, and to permit persons to whom the */
/* Software is furnished to do so, subject to the following */
/* conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the */
/* Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
/* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
/* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
/* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
/* OTHER DEALINGS IN THE SOFTWARE. */
/* */
/************************************************************************/
#ifndef VIGRA_MULTI_BLOCKWISE_HXX
#define VIGRA_MULTI_BLOCKWISE_HXX
#include <cmath>
#include <vector>
#include "multi_blocking.hxx"
#include "multi_convolution.hxx"
#include "multi_tensorutilities.hxx"
#include "threadpool.hxx"
#include "array_vector.hxx"
namespace vigra{
/** Option base class for blockwise algorithms.
Attaches blockshape to ParallelOptions.
*/
class BlockwiseOptions
: public ParallelOptions
{
public:
typedef ArrayVector<MultiArrayIndex> Shape;
BlockwiseOptions()
: ParallelOptions()
, blockShape_()
{}
/** Retrieve block shape as a std::vector.
If the returned vector is empty, a default block shape should be used.
If the returned vector has length 1, square blocks of size
<tt>getBlockShape()[0]</tt> should be used.
*/
Shape const & getBlockShape() const
{
return blockShape_;
}
// for Python bindings
Shape readBlockShape() const
{
return blockShape_;
}
/** Retrieve block shape as a fixed-size vector.
Default shape specifications are appropriately expanded.
An exception is raised if the stored shape's length is
incompatible with dimension <tt>N</tt>.
*/
template <int N>
TinyVector<MultiArrayIndex, N> getBlockShapeN() const
{
if(blockShape_.size() > 1)
{
vigra_precondition(blockShape_.size() == (size_t)N,
"BlockwiseOptions::getBlockShapeN(): dimension mismatch between N and stored block shape.");
return TinyVector<MultiArrayIndex, N>(blockShape_.data());
}
else if(blockShape_.size() == 1)
{
return TinyVector<MultiArrayIndex, N>(blockShape_[0]);
}
else
{
return detail::ChunkShape<N>::defaultShape();
}
}
/** Specify block shape as a std::vector of appropriate length.
If <tt>blockShape.size() == 0</tt>, the default shape is used.
If <tt>blockShape.size() == 1</tt>, square blocks of size
<tt>blockShape[0]</tt> are used.
Default: Use square blocks with side length <tt>VIGRA_DEFAULT_BLOCK_SHAPE</tt>.
*/
BlockwiseOptions & blockShape(const Shape & blockShape){
blockShape_ = blockShape;
return *this;
}
// for Python bindings
void setBlockShape(const Shape & blockShape){
blockShape_ = blockShape;
}
/** Specify block shape by a fixed-size shape object.
*/
template <class T, int N>
BlockwiseOptions & blockShape(const TinyVector<T, N> & blockShape){
Shape(blockShape.begin(), blockShape.end()).swap(blockShape_);
return *this;
}
/** Specify square block shape by its side length.
*/
BlockwiseOptions & blockShape(MultiArrayIndex blockShape){
Shape(1, blockShape).swap(blockShape_);
return *this;
}
BlockwiseOptions & numThreads(const int n)
{
ParallelOptions::numThreads(n);
return *this;
}
void setNumThreads(const int n)
{
ParallelOptions::numThreads(n);
}
private:
Shape blockShape_;
};
/** Option class for blockwise convolution algorithms.
Simply derives from \ref vigra::BlockwiseOptions and
\ref vigra::ConvolutionOptions to join their capabilities.
*/
template<unsigned int N>
class BlockwiseConvolutionOptions
: public BlockwiseOptions
, public ConvolutionOptions<N>{
public:
BlockwiseConvolutionOptions()
: BlockwiseOptions(),
ConvolutionOptions<N>()
{}
};
namespace blockwise{
/**
helper function to create blockwise parallel filters.
This implementation should be used if the filter functor
does not support the ROI/sub array options.
*/
template<
unsigned int DIM,
class T_IN, class ST_IN,
class T_OUT, class ST_OUT,
class FILTER_FUNCTOR,
class C
>
void blockwiseCallerNoRoiApi(
const vigra::MultiArrayView<DIM, T_IN, ST_IN > & source,
const vigra::MultiArrayView<DIM, T_OUT, ST_OUT> & dest,
FILTER_FUNCTOR & functor,
const vigra::MultiBlocking<DIM, C> & blocking,
const typename vigra::MultiBlocking<DIM, C>::Shape & borderWidth,
const BlockwiseConvolutionOptions<DIM> & options
){
typedef typename MultiBlocking<DIM, C>::BlockWithBorder BlockWithBorder;
auto beginIter = blocking.blockWithBorderBegin(borderWidth);
auto endIter = blocking.blockWithBorderEnd(borderWidth);
parallel_foreach(options.getNumThreads(),
beginIter, endIter,
[&](const int threadId, const BlockWithBorder bwb)
{
// get the input of the block as a view
vigra::MultiArrayView<DIM, T_IN, ST_IN> sourceSub = source.subarray(bwb.border().begin(),
bwb.border().end());
// get the output as NEW allocated array
vigra::MultiArray<DIM, T_OUT> destSub(sourceSub.shape());
// call the functor
functor(sourceSub, destSub);
// write the core global out
vigra::MultiArrayView<DIM, T_OUT, ST_OUT> destSubCore = destSub.subarray(bwb.localCore().begin(),
bwb.localCore().end());
// write the core global out
dest.subarray(bwb.core().begin()-blocking.roiBegin(),
bwb.core().end() -blocking.roiBegin() ) = destSubCore;
},
blocking.numBlocks()
);
}
/**
helper function to create blockwise parallel filters.
This implementation should be used if the filter functor
does support the ROI/sub array options.
*/
template<
unsigned int DIM,
class T_IN, class ST_IN,
class T_OUT, class ST_OUT,
class FILTER_FUNCTOR,
class C
>
void blockwiseCaller(
const vigra::MultiArrayView<DIM, T_IN, ST_IN > & source,
const vigra::MultiArrayView<DIM, T_OUT, ST_OUT> & dest,
FILTER_FUNCTOR & functor,
const vigra::MultiBlocking<DIM, C> & blocking,
const typename vigra::MultiBlocking<DIM, C>::Shape & borderWidth,
const BlockwiseConvolutionOptions<DIM> & options
){
typedef typename MultiBlocking<DIM, C>::BlockWithBorder BlockWithBorder;
//typedef typename MultiBlocking<DIM, C>::BlockWithBorderIter BlockWithBorderIter;
typedef typename MultiBlocking<DIM, C>::Block Block;
auto beginIter = blocking.blockWithBorderBegin(borderWidth);
auto endIter = blocking.blockWithBorderEnd(borderWidth);
parallel_foreach(options.getNumThreads(),
beginIter, endIter,
[&](const int threadId, const BlockWithBorder bwb)
{
// get the input of the block as a view
vigra::MultiArrayView<DIM, T_IN, ST_IN> sourceSub = source.subarray(bwb.border().begin(),
bwb.border().end());
// get the output of the blocks core as a view
vigra::MultiArrayView<DIM, T_OUT, ST_OUT> destCore = dest.subarray(bwb.core().begin(),
bwb.core().end());
const Block localCore = bwb.localCore();
// call the functor
functor(sourceSub, destCore, localCore.begin(), localCore.end());
},
blocking.numBlocks()
);
}
#define CONVOLUTION_FUNCTOR(FUNCTOR_NAME, FUNCTION_NAME) \
template<unsigned int DIM> \
class FUNCTOR_NAME{ \
public: \
typedef ConvolutionOptions<DIM> ConvOpt; \
FUNCTOR_NAME(const ConvOpt & convOpt) \
: convOpt_(convOpt){} \
template<class S, class D> \
void operator()(const S & s, D & d)const{ \
FUNCTION_NAME(s, d, convOpt_); \
} \
template<class S, class D,class SHAPE> \
void operator()(const S & s, D & d, const SHAPE & roiBegin, const SHAPE & roiEnd){ \
ConvOpt convOpt(convOpt_); \
convOpt.subarray(roiBegin, roiEnd); \
FUNCTION_NAME(s, d, convOpt); \
} \
private: \
ConvOpt convOpt_; \
};
CONVOLUTION_FUNCTOR(GaussianSmoothFunctor, vigra::gaussianSmoothMultiArray);
CONVOLUTION_FUNCTOR(GaussianGradientFunctor, vigra::gaussianGradientMultiArray);
CONVOLUTION_FUNCTOR(SymmetricGradientFunctor, vigra::symmetricGradientMultiArray);
CONVOLUTION_FUNCTOR(GaussianDivergenceFunctor, vigra::gaussianDivergenceMultiArray);
CONVOLUTION_FUNCTOR(HessianOfGaussianFunctor, vigra::hessianOfGaussianMultiArray);
CONVOLUTION_FUNCTOR(LaplacianOfGaussianFunctor, vigra::laplacianOfGaussianMultiArray);
CONVOLUTION_FUNCTOR(GaussianGradientMagnitudeFunctor, vigra::gaussianGradientMagnitude);
CONVOLUTION_FUNCTOR(StructureTensorFunctor, vigra::structureTensorMultiArray);
#undef CONVOLUTION_FUNCTOR
template<unsigned int DIM>
class HessianOfGaussianEigenvaluesFunctor{
public:
typedef ConvolutionOptions<DIM> ConvOpt;
HessianOfGaussianEigenvaluesFunctor(const ConvOpt & convOpt)
: convOpt_(convOpt){}
template<class S, class D>
void operator()(const S & s, D & d)const{
typedef typename vigra::NumericTraits<typename S::value_type>::RealPromote RealType;
vigra::MultiArray<DIM, TinyVector<RealType, int(DIM*(DIM+1)/2)> > hessianOfGaussianRes(d.shape());
vigra::hessianOfGaussianMultiArray(s, hessianOfGaussianRes, convOpt_);
vigra::tensorEigenvaluesMultiArray(hessianOfGaussianRes, d);
}
template<class S, class D,class SHAPE>
void operator()(const S & s, D & d, const SHAPE & roiBegin, const SHAPE & roiEnd){
typedef typename vigra::NumericTraits<typename S::value_type>::RealPromote RealType;
vigra::MultiArray<DIM, TinyVector<RealType, int(DIM*(DIM+1)/2)> > hessianOfGaussianRes(roiEnd-roiBegin);
convOpt_.subarray(roiBegin, roiEnd);
vigra::hessianOfGaussianMultiArray(s, hessianOfGaussianRes, convOpt_);
vigra::tensorEigenvaluesMultiArray(hessianOfGaussianRes, d);
}
private:
ConvOpt convOpt_;
};
template<unsigned int DIM, unsigned int EV>
class HessianOfGaussianSelectedEigenvalueFunctor{
public:
typedef ConvolutionOptions<DIM> ConvOpt;
HessianOfGaussianSelectedEigenvalueFunctor(const ConvOpt & convOpt)
: convOpt_(convOpt){}
template<class S, class D>
void operator()(const S & s, D & d)const{
typedef typename vigra::NumericTraits<typename S::value_type>::RealPromote RealType;
// compute the hessian of gaussian and extract eigenvalue
vigra::MultiArray<DIM, TinyVector<RealType, int(DIM*(DIM+1)/2)> > hessianOfGaussianRes(s.shape());
vigra::hessianOfGaussianMultiArray(s, hessianOfGaussianRes, convOpt_);
vigra::MultiArray<DIM, TinyVector<RealType, DIM > > allEigenvalues(s.shape());
vigra::tensorEigenvaluesMultiArray(hessianOfGaussianRes, allEigenvalues);
d = allEigenvalues.bindElementChannel(EV);
}
template<class S, class D,class SHAPE>
void operator()(const S & s, D & d, const SHAPE & roiBegin, const SHAPE & roiEnd){
typedef typename vigra::NumericTraits<typename S::value_type>::RealPromote RealType;
// compute the hessian of gaussian and extract eigenvalue
vigra::MultiArray<DIM, TinyVector<RealType, int(DIM*(DIM+1)/2)> > hessianOfGaussianRes(roiEnd-roiBegin);
convOpt_.subarray(roiBegin, roiEnd);
vigra::hessianOfGaussianMultiArray(s, hessianOfGaussianRes, convOpt_);
vigra::MultiArray<DIM, TinyVector<RealType, DIM > > allEigenvalues(roiEnd-roiBegin);
vigra::tensorEigenvaluesMultiArray(hessianOfGaussianRes, allEigenvalues);
d = allEigenvalues.bindElementChannel(EV);
}
private:
ConvOpt convOpt_;
};
template<unsigned int DIM>
class HessianOfGaussianFirstEigenvalueFunctor
: public HessianOfGaussianSelectedEigenvalueFunctor<DIM, 0>{
public:
typedef ConvolutionOptions<DIM> ConvOpt;
HessianOfGaussianFirstEigenvalueFunctor(const ConvOpt & convOpt)
: HessianOfGaussianSelectedEigenvalueFunctor<DIM, 0>(convOpt){}
};
template<unsigned int DIM>
class HessianOfGaussianLastEigenvalueFunctor
: public HessianOfGaussianSelectedEigenvalueFunctor<DIM, DIM-1>{
public:
typedef ConvolutionOptions<DIM> ConvOpt;
HessianOfGaussianLastEigenvalueFunctor(const ConvOpt & convOpt)
: HessianOfGaussianSelectedEigenvalueFunctor<DIM, DIM-1>(convOpt){}
};
/// \warning this functions is deprecated
/// and should not be used from end users
template<unsigned int N>
vigra::TinyVector< vigra::MultiArrayIndex, N > getBorder(
const BlockwiseConvolutionOptions<N> & opt,
const size_t order,
const bool usesOuterScale = false
){
vigra::TinyVector< vigra::MultiArrayIndex, N > res(vigra::SkipInitialization);
if(opt.getFilterWindowSize()<=0.00001){
for(size_t d=0; d<N; ++d){
double stdDev = opt.getStdDev()[d];
if(usesOuterScale)
stdDev += opt.getOuterScale()[d];
res[d] = static_cast<MultiArrayIndex>(3.0 * stdDev + 0.5*static_cast<double>(order)+0.5);
}
}
else{
throw std::runtime_error("blockwise filters do not allow a user defined FilterWindowSize");
}
return res;
}
} // end namespace blockwise
#define VIGRA_BLOCKWISE(FUNCTOR, FUNCTION, ORDER, USES_OUTER_SCALE) \
template <unsigned int N, class T1, class S1, class T2, class S2> \
void FUNCTION( \
MultiArrayView<N, T1, S1> const & source, \
MultiArrayView<N, T2, S2> dest, \
BlockwiseConvolutionOptions<N> const & options \
) \
{ \
typedef MultiBlocking<N, vigra::MultiArrayIndex> Blocking; \
typedef typename Blocking::Shape Shape; \
const Shape border = blockwise::getBorder(options, ORDER, USES_OUTER_SCALE); \
BlockwiseConvolutionOptions<N> subOptions(options); \
subOptions.subarray(Shape(0), Shape(0)); \
const Blocking blocking(source.shape(), options.template getBlockShapeN<N>()); \
blockwise::FUNCTOR<N> f(subOptions); \
blockwise::blockwiseCaller(source, dest, f, blocking, border, options); \
}
VIGRA_BLOCKWISE(GaussianSmoothFunctor, gaussianSmoothMultiArray, 0, false );
VIGRA_BLOCKWISE(GaussianGradientFunctor, gaussianGradientMultiArray, 1, false );
VIGRA_BLOCKWISE(SymmetricGradientFunctor, symmetricGradientMultiArray, 1, false );
VIGRA_BLOCKWISE(GaussianDivergenceFunctor, gaussianDivergenceMultiArray, 1, false );
VIGRA_BLOCKWISE(HessianOfGaussianFunctor, hessianOfGaussianMultiArray, 2, false );
VIGRA_BLOCKWISE(HessianOfGaussianEigenvaluesFunctor, hessianOfGaussianEigenvaluesMultiArray, 2, false );
VIGRA_BLOCKWISE(HessianOfGaussianFirstEigenvalueFunctor, hessianOfGaussianFirstEigenvalueMultiArray, 2, false );
VIGRA_BLOCKWISE(HessianOfGaussianLastEigenvalueFunctor, hessianOfGaussianLastEigenvalueMultiArray, 2, false );
VIGRA_BLOCKWISE(LaplacianOfGaussianFunctor, laplacianOfGaussianMultiArray, 2, false );
VIGRA_BLOCKWISE(GaussianGradientMagnitudeFunctor, gaussianGradientMagnitudeMultiArray, 1, false );
VIGRA_BLOCKWISE(StructureTensorFunctor, structureTensorMultiArray, 1, true );
#undef VIGRA_BLOCKWISE
// alternative name for backward compatibility
template <unsigned int N, class T1, class S1, class T2, class S2>
inline void
gaussianGradientMagnitude(
MultiArrayView<N, T1, S1> const & source,
MultiArrayView<N, T2, S2> dest,
BlockwiseConvolutionOptions<N> const & options)
{
gaussianGradientMagnitudeMultiArray(source, dest, options);
}
} // end namespace vigra
#endif // VIGRA_MULTI_BLOCKWISE_HXX
|