1// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2012 Google Inc. All rights reserved.
3// http://code.google.com/p/ceres-solver/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7//
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.
16//
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.
28//
29// Author: strandmark@google.com (Petter Strandmark)
30//
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
33//
34//   alpha * log(1 + (1/2)*sum(F .* X)^2),
35//
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)
39// part.
40//
41// [1] S. Roth and M.J. Black. "Fields of Experts." International Journal of
42//     Computer Vision, 82(2):205--229, 2009.
43
44#ifndef CERES_EXAMPLES_FIELDS_OF_EXPERTS_H_
45#define CERES_EXAMPLES_FIELDS_OF_EXPERTS_H_
46
47#include <iostream>
48#include <vector>
49
50#include "ceres/loss_function.h"
51#include "ceres/cost_function.h"
52#include "ceres/sized_cost_function.h"
53
54#include "pgm_image.h"
55
56namespace ceres {
57namespace examples {
58
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.
62class FieldsOfExpertsCost : public ceres::CostFunction {
63 public:
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,
68                        double* residuals,
69                        double** jacobians) const;
70
71 private:
72  const std::vector<double>& filter_;
73};
74
75// The loss function used to build the correct regularization. See above.
76//
77//   f(x) = alpha_i * log(1 + (1/2)s)
78//
79class FieldsOfExpertsLoss : public ceres::LossFunction {
80 public:
81  explicit FieldsOfExpertsLoss(double alpha) : alpha_(alpha) { }
82  virtual void Evaluate(double, double*) const;
83
84 private:
85  const double alpha_;
86};
87
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
90// NewLossFunction.
91class FieldsOfExperts {
92 public:
93  // Creates an empty object with size() == 0.
94  FieldsOfExperts();
95  // Attempts to load filters from a file. If unsuccessful it returns false and
96  // sets size() == 0.
97  bool LoadFromFile(const std::string& filename);
98
99  // Side length of a square filter in this FoE. They are all of the same size.
100  int Size() const {
101    return size_;
102  }
103
104  // Total number of pixels the filter covers.
105  int NumVariables() const {
106    return size_ * size_;
107  }
108
109  // Number of filters used by the FoE.
110  int NumFilters() const {
111    return num_filters_;
112  }
113
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;
120
121  // Gets the delta pixel indices for all pixels in a patch.
122  const std::vector<int>& GetXDeltaIndices() const {
123    return x_delta_indices_;
124  }
125  const std::vector<int>& GetYDeltaIndices() const {
126    return y_delta_indices_;
127  }
128
129 private:
130  // The side length of a square filter.
131  int size_;
132  // The number of different filters used.
133  int num_filters_;
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_;
140};
141
142}  // namespace examples
143}  // namespace ceres
144
145#endif  // CERES_EXAMPLES_FIELDS_OF_EXPERTS_H_
146