mapperpyramid.cpp 3.79 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
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
// Copyright (C) 2013, Alfonso Sanchez-Beato, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

#include "precomp.hpp"
#include <vector>

#include "opencv2/imgproc.hpp"
#include "opencv2/reg/mapperpyramid.hpp"

using namespace std;

namespace cv {
namespace reg {


////////////////////////////////////////////////////////////////////////////////////////////////////
51 52
MapperPyramid::MapperPyramid(Ptr<Mapper> baseMapper)
    : numLev_(3), numIterPerScale_(3), baseMapper_(*baseMapper)
53 54 55 56
{
}

////////////////////////////////////////////////////////////////////////////////////////////////////
57
Ptr<Map> MapperPyramid::calculate(InputArray _img1, InputArray image2, Ptr<Map> init) const
58
{
59
    Mat img1 = _img1.getMat();
60 61
    Mat img2;

62
    if(!init.empty()) {
63
        // We have initial values for the registration: we move img2 to that initial reference
64
        init->inverseWarp(image2, img2);
65
    } else {
66
        init = baseMapper_.getMap();
67
        img2 = image2.getMat();
68 69 70 71 72 73 74 75
    }

    cv::Ptr<Map> ident = baseMapper_.getMap();

    // Precalculate pyramid images
    vector<Mat> pyrIm1(numLev_), pyrIm2(numLev_);
    pyrIm1[0] = img1;
    pyrIm2[0] = img2;
76
    for(int im_i = 1; im_i < numLev_; ++im_i) {
77 78 79 80 81
        pyrDown(pyrIm1[im_i - 1], pyrIm1[im_i]);
        pyrDown(pyrIm2[im_i - 1], pyrIm2[im_i]);
    }

    Mat currRef, currImg;
82
    for(int lv_i = 0; lv_i < numLev_; ++lv_i) {
83 84 85 86 87 88
        currRef = pyrIm1[numLev_ - 1 - lv_i];
        currImg = pyrIm2[numLev_ - 1 - lv_i];
        // Scale the transformation as we are incresing the resolution in each iteration
        if(lv_i != 0) {
            ident->scale(2.);
        }
89
        for(int it_i = 0; it_i < numIterPerScale_; ++it_i) {
90
            ident = baseMapper_.calculate(currRef, currImg, ident);
91 92 93
        }
    }

94 95
    init->compose(ident);
    return init;
96 97 98
}

////////////////////////////////////////////////////////////////////////////////////////////////////
99
cv::Ptr<Map> MapperPyramid::getMap() const
100 101 102 103 104 105
{
    return cv::Ptr<Map>();
}


}}  // namespace cv::reg