2
* Copyright (c) 2007 Cyrille Berger (cberger@cberger.net)
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU Lesser General Public License as published by
6
* the Free Software Foundation; version 2 of the License.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17
#ifndef _HOMOGRAPHY_IMAGE_MATCH_MODEL_P_H_
18
#define _HOMOGRAPHY_IMAGE_MATCH_MODEL_P_H_
21
class HomographyImageMatchModel
25
inline Params(int w, int h, double thres)
26
: width(w), height(h), threshold(thres) {}
31
inline HomographyImageMatchModel(std::vector<KisMatch> samples, Params* params) : m_isValid(false), m_fitComputed(false), m_matches(samples), m_width(params->width), m_height(params->height), m_threshold(params->threshold) {
34
* @return the minimal number of points needed for a fit
36
inline static uint nbFit() {
39
inline bool isValid() {
40
if (not m_fitComputed) {
45
inline void addData(std::vector<KisMatch>::iterator begin, std::vector<KisMatch>::iterator end) {
46
for (std::vector<KisMatch>::iterator it = begin; it != end; it++) {
47
m_matches.push_back(*it);
49
m_fitComputed = false;
51
inline double fittingErrorSum() {
52
if (not m_fitComputed) {
55
return m_fittingErrorSum;
57
inline double threshold() const {
60
inline double fittingError(const KisMatch& m) {
61
int indx[HomographySameDistortionFunction::SIZEINDEXES];
62
for (int i = 0; i < HomographySameDistortionFunction::SIZEINDEXES; i++) {
65
double norm(4.0 / (m_width * m_width + m_height * m_height));
66
HomographySameDistortionFunction hsdf(indx, m_width * 0.5, m_height * 0.5, norm, m.ref->x(), m.ref->y(), m.match->x(), m.match->y());
68
hsdf.f(parameters(), f1, f2);
69
return (f1 + f2) * 0.5;
71
inline const std::vector<double>& parameters() const {
74
inline const std::vector<KisMatch>& matches() const {
78
void computeFitting() {
79
KisControlPoints cps(2);
80
cps.addMatches(m_matches, 0, 1);
81
PanoptimFunction<HomographySameDistortionFunction, HomographySameDistortionFunction::SIZEINDEXES> f(cps, m_width * 0.5, m_height * 0.5, m_width, m_height);
82
m_parameters.resize(HomographySameDistortionFunction::SIZEINDEXES);
83
m_parameters[HomographySameDistortionFunction::INDX_a] = 0.0;
84
m_parameters[HomographySameDistortionFunction::INDX_b] = 0.0;
85
m_parameters[HomographySameDistortionFunction::INDX_c] = 0.0;
86
m_parameters[HomographySameDistortionFunction::INDX_h11] = 1.0;
87
m_parameters[HomographySameDistortionFunction::INDX_h21] = 0.0;
88
m_parameters[HomographySameDistortionFunction::INDX_h31] = m_matches[0].ref->x() - m_matches[0].match->x();
89
m_parameters[HomographySameDistortionFunction::INDX_h12] = 0.0;
90
m_parameters[HomographySameDistortionFunction::INDX_h22] = 1.0;
91
m_parameters[HomographySameDistortionFunction::INDX_h32] = m_matches[0].ref->y() - m_matches[0].match->y();
92
m_parameters[HomographySameDistortionFunction::INDX_h13] = 0.0;
93
m_parameters[HomographySameDistortionFunction::INDX_h23] = 0.0;
94
m_fittingErrorSum = Optimization::Algorithms::levenbergMarquardt(&f, m_parameters, 100, 1e-12, 0.01, 10.0);
97
for (uint i = 0; i < m_parameters.size(); i++) {
98
dbgPlugins << "m_parameters[" << i << "]=" << m_parameters[i];
104
double m_fittingErrorSum;
106
std::vector<double> m_parameters;
107
int m_width, m_height;