exposureStabilize.hpp 3.65 KB
Newer Older
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
// Copyright (c) 2012-2017 VideoStitch SAS
// Copyright (c) 2018 stitchEm

#pragma once

#include "libvideostitch/config.hpp"
#include "libvideostitch/panoDef.hpp"
#include "util/lmfit/lmmin.hpp"
#include "gpu/vectorTypes.hpp"

#include <memory>

namespace VideoStitch {
namespace Util {
/**
 * A base problem for exposure compensation.
 */
class ExposureStabilizationProblemBase : public SolverProblem {
 public:
  class ParameterSet;

  /**
   * The parameter set to optimize: Only Ev or WB.
   */
  enum ParameterSetType {
    EvParameterSet,
    WBParameterSet,
  };

  /**
   * The type of anchoring:
   */
  enum AnchoringType {
    SingleInputAnchoring,
    CenterParamsAnchoring,
  };

  /**
   * ctor.
   * @param pano Pano definition
   * @param anchor anchor input, or -1
   * @param parameterSetType What parameters to stabilize
   */
  ExposureStabilizationProblemBase(const Core::PanoDefinition& pano, int anchor, ParameterSetType parameterSetType);

  virtual ~ExposureStabilizationProblemBase();

  /**
   * Returns the anchoring type.
   */
  AnchoringType getAnchoringType() const;

  /**
   * Returns the video input id of the anchor. Only meaningful if getAnchoringType() == SingleInputAnchoring.
   */
  videoreaderid_t getVideoAnchor() const;

  int numParams() const;

  virtual int getNumInputSamples() const = 0;

  int getNumValuesPerSample() const {
    return 3;  // (R, G, B) for each sample
  }

  /**
   * k is a video input index
   */
  float3 getVideoColorMult(const double* params, videoreaderid_t k) const;

  int getNumAdditionalValues() const { return 0; }

  /**
   * Returns the minimum acceptable parameter value.
   */
  double getMinValidParamValue() const;

  /**
   * Returns the maximum acceptable parameter value.
   */
  double getMaxValidParamValue() const;

  /**
   * Returns true if the parameter set consists of only valid values.
   */
  bool isValid(const double* params) const;

  /**
   * Sets the time to be used when querying time-dependant panorama or input properties.
   * @param newTime The time to set.
   */
  void setTime(int newTime) { time = newTime; }

  /**
   * Computes the initial guess for parameters.
   * @param params Vector of parameters.
   */
  void computeInitialGuess(std::vector<double>& params) const;

  /**
   * Returns the pano definition.
   */
  const Core::PanoDefinition& getPano() const;

  /**
   * Computes the index of video input @a k in the renumbered video inputs (without the anchor).
   * @param k Pano video input id.
   * @returns params video input id.
   */
  videoreaderid_t getVideoParamIndex(int k) const;

  /**
   * Computes the index of video input @a k from the renumbered video parameter id..
   * @param paramK params input id.
   * @returns pano video input id.
   */
  videoreaderid_t getVideoInputIndex(int paramK) const;

  /**
   * Save a constant curve point.
   * @param params Vector of parameters.
   */
  void constantControlPoint(std::vector<double>& params);

  /**
   * Save a curve point at the current time.
   * @param params Vector of parameters.
   */
  void saveControlPoint(std::vector<double>& params);

  /**
   * Inject the new splines in place of the old ones.
   * @param pano Output pano def.
   * @param preserveOutside If true, add one keyframe on each side to preserve exterior values.
   * @param firstFrame First stabilizatiuon frame.
   * @param lastFrame Last stabilizatiuon frame.
   * @return false if there are no saved control points.
   */
  bool injectSavedControlPoints(Core::PanoDefinition* pano, bool preserveOutside, int firstFrame, int lastFrame);

 private:
  const std::unique_ptr<ParameterSet> parameterSet;
  int time;
};

}  // namespace Util
}  // namespace VideoStitch