1 // Ceres Solver - A fast non-linear least squares minimizer
2 // Copyright 2015 Google Inc. All rights reserved.
3 // http://ceres-solver.org/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors may be
14 // used to endorse or promote products derived from this software without
15 // specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
29 // Author: strandmark@google.com (Petter Strandmark)
31 // Class for loading the data required for descibing a Fields of Experts (FoE)
32 // model. The Fields of Experts regularization consists of terms of the type
34 // alpha * log(1 + (1/2)*sum(F .* X)^2),
36 // where F is a d-by-d image patch and alpha is a constant. This is implemented
37 // by a FieldsOfExpertsSum object which represents the dot product between the
38 // image patches and a FieldsOfExpertsLoss which implements the log(1 + (1/2)s)
41 // [1] S. Roth and M.J. Black. "Fields of Experts." International Journal of
42 // Computer Vision, 82(2):205--229, 2009.
44 #ifndef CERES_EXAMPLES_FIELDS_OF_EXPERTS_H_
45 #define CERES_EXAMPLES_FIELDS_OF_EXPERTS_H_
50 #include "ceres/loss_function.h"
51 #include "ceres/cost_function.h"
52 #include "ceres/sized_cost_function.h"
54 #include "pgm_image.h"
59 // One sum in the FoE regularizer. This is a dot product between a filter and an
60 // image patch. It simply calculates the dot product between the filter
61 // coefficients given in the constructor and the scalar parameters passed to it.
62 class FieldsOfExpertsCost : public ceres::CostFunction {
64 explicit FieldsOfExpertsCost(const std::vector<double>& filter);
65 // The number of scalar parameters passed to Evaluate must equal the number of
66 // filter coefficients passed to the constructor.
67 virtual bool Evaluate(double const* const* parameters,
69 double** jacobians) const;
72 const std::vector<double>& filter_;
75 // The loss function used to build the correct regularization. See above.
77 // f(x) = alpha_i * log(1 + (1/2)s)
79 class FieldsOfExpertsLoss : public ceres::LossFunction {
81 explicit FieldsOfExpertsLoss(double alpha) : alpha_(alpha) { }
82 virtual void Evaluate(double, double*) const;
88 // This class loads a set of filters and coefficients from file. Then the users
89 // obtains the correct loss and cost functions through NewCostFunction and
91 class FieldsOfExperts {
93 // Creates an empty object with size() == 0.
95 // Attempts to load filters from a file. If unsuccessful it returns false and
97 bool LoadFromFile(const std::string& filename);
99 // Side length of a square filter in this FoE. They are all of the same size.
104 // Total number of pixels the filter covers.
105 int NumVariables() const {
106 return size_ * size_;
109 // Number of filters used by the FoE.
110 int NumFilters() const {
114 // Creates a new cost function. The caller is responsible for deallocating the
115 // memory. alpha_index specifies which filter is used in the cost function.
116 ceres::CostFunction* NewCostFunction(int alpha_index) const;
117 // Creates a new loss function. The caller is responsible for deallocating the
118 // memory. alpha_index specifies which filter this loss function is for.
119 ceres::LossFunction* NewLossFunction(int alpha_index) const;
121 // Gets the delta pixel indices for all pixels in a patch.
122 const std::vector<int>& GetXDeltaIndices() const {
123 return x_delta_indices_;
125 const std::vector<int>& GetYDeltaIndices() const {
126 return y_delta_indices_;
130 // The side length of a square filter.
132 // The number of different filters used.
134 // Pixel offsets for all variables.
135 std::vector<int> x_delta_indices_, y_delta_indices_;
136 // The coefficients in front of each term.
137 std::vector<double> alpha_;
138 // The filters used for the dot product with image patches.
139 std::vector<std::vector<double> > filters_;
142 } // namespace examples
145 #endif // CERES_EXAMPLES_FIELDS_OF_EXPERTS_H_