align.cpp 8.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*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.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
13
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
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
// 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 Intel Corporation or 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 "opencv2/photo.hpp"
#include "opencv2/imgproc.hpp"
#include "hdr_common.hpp"

namespace cv
{

class AlignMTBImpl : public AlignMTB
{
public:
53 54 55 56 57
    AlignMTBImpl(int _max_bits, int _exclude_range, bool _cut) :
        name("AlignMTB"),
        max_bits(_max_bits),
        exclude_range(_exclude_range),
        cut(_cut)
Fedor Morozov's avatar
Fedor Morozov committed
58 59
    {
    }
60

Fedor Morozov's avatar
Fedor Morozov committed
61
    void process(InputArrayOfArrays src, std::vector<Mat>& dst,
62
                 InputArray, InputArray)
Fedor Morozov's avatar
Fedor Morozov committed
63 64 65 66
    {
        process(src, dst);
    }

Fedor Morozov's avatar
Fedor Morozov committed
67
    void process(InputArrayOfArrays _src, std::vector<Mat>& dst)
Fedor Morozov's avatar
Fedor Morozov committed
68
    {
Fedor Morozov's avatar
Fedor Morozov committed
69
        std::vector<Mat> src;
Fedor Morozov's avatar
Fedor Morozov committed
70
        _src.getMatVector(src);
71

Fedor Morozov's avatar
Fedor Morozov committed
72 73 74 75 76 77 78
        checkImageDimensions(src);
        dst.resize(src.size());

        size_t pivot = src.size() / 2;
        dst[pivot] = src[pivot];
        Mat gray_base;
        cvtColor(src[pivot], gray_base, COLOR_RGB2GRAY);
Fedor Morozov's avatar
Fedor Morozov committed
79
        std::vector<Point> shifts;
Fedor Morozov's avatar
Fedor Morozov committed
80 81 82

        for(size_t i = 0; i < src.size(); i++) {
            if(i == pivot) {
Fedor Morozov's avatar
Fedor Morozov committed
83
                shifts.push_back(Point(0, 0));
Fedor Morozov's avatar
Fedor Morozov committed
84 85 86 87
                continue;
            }
            Mat gray;
            cvtColor(src[i], gray, COLOR_RGB2GRAY);
88
            Point shift = calculateShift(gray_base, gray);
Fedor Morozov's avatar
Fedor Morozov committed
89
            shifts.push_back(shift);
Fedor Morozov's avatar
Fedor Morozov committed
90 91
            shiftMat(src[i], dst[i], shift);
        }
Fedor Morozov's avatar
Fedor Morozov committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
        if(cut) {
            Point max(0, 0), min(0, 0);
            for(size_t i = 0; i < shifts.size(); i++) {
                if(shifts[i].x > max.x) {
                    max.x = shifts[i].x;
                }
                if(shifts[i].y > max.y) {
                    max.y = shifts[i].y;
                }
                if(shifts[i].x < min.x) {
                    min.x = shifts[i].x;
                }
                if(shifts[i].y < min.y) {
                    min.y = shifts[i].y;
                }
            }
            Point size = dst[0].size();
            for(size_t i = 0; i < dst.size(); i++) {
                dst[i] = dst[i](Rect(max, min + size));
            }
        }
Fedor Morozov's avatar
Fedor Morozov committed
113 114
    }

115
    Point calculateShift(InputArray _img0, InputArray _img1)
Fedor Morozov's avatar
Fedor Morozov committed
116 117 118 119 120 121 122 123 124 125 126 127
    {
        Mat img0 = _img0.getMat();
        Mat img1 = _img1.getMat();
        CV_Assert(img0.channels() == 1 && img0.type() == img1.type());
        CV_Assert(img0.size() == img0.size());

        int maxlevel = static_cast<int>(log((double)max(img0.rows, img0.cols)) / log(2.0)) - 1;
        maxlevel = min(maxlevel, max_bits - 1);

        std::vector<Mat> pyr0;
        std::vector<Mat> pyr1;
        buildPyr(img0, pyr0, maxlevel);
128 129 130
        buildPyr(img1, pyr1, maxlevel);

        Point shift(0, 0);
Fedor Morozov's avatar
Fedor Morozov committed
131
        for(int level = maxlevel; level >= 0; level--) {
132

Fedor Morozov's avatar
Fedor Morozov committed
133 134
            shift *= 2;
            Mat tb1, tb2, eb1, eb2;
Fedor Morozov's avatar
Fedor Morozov committed
135 136
            computeBitmaps(pyr0[level], tb1, eb1);
            computeBitmaps(pyr1[level], tb2, eb2);
Fedor Morozov's avatar
Fedor Morozov committed
137

Ilya Lavrenov's avatar
Ilya Lavrenov committed
138
            int min_err = (int)pyr0[level].total();
Fedor Morozov's avatar
Fedor Morozov committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152
            Point new_shift(shift);
            for(int i = -1; i <= 1; i++) {
                for(int j = -1; j <= 1; j++) {
                    Point test_shift = shift + Point(i, j);
                    Mat shifted_tb2, shifted_eb2, diff;
                    shiftMat(tb2, shifted_tb2, test_shift);
                    shiftMat(eb2, shifted_eb2, test_shift);
                    bitwise_xor(tb1, shifted_tb2, diff);
                    bitwise_and(diff, eb1, diff);
                    bitwise_and(diff, shifted_eb2, diff);
                    int err = countNonZero(diff);
                    if(err < min_err) {
                        new_shift = test_shift;
                        min_err = err;
153
                    }
Fedor Morozov's avatar
Fedor Morozov committed
154 155 156 157
                }
            }
            shift = new_shift;
        }
158
        return shift;
Fedor Morozov's avatar
Fedor Morozov committed
159 160
    }

161
    void shiftMat(InputArray _src, OutputArray _dst, const Point shift)
Fedor Morozov's avatar
Fedor Morozov committed
162 163 164 165 166
    {
        Mat src = _src.getMat();
        _dst.create(src.size(), src.type());
        Mat dst = _dst.getMat();

Fedor Morozov's avatar
Fedor Morozov committed
167
        Mat res = Mat::zeros(src.size(), src.type());
Fedor Morozov's avatar
Fedor Morozov committed
168 169 170 171
        int width = src.cols - abs(shift.x);
        int height = src.rows - abs(shift.y);
        Rect dst_rect(max(shift.x, 0), max(shift.y, 0), width, height);
        Rect src_rect(max(-shift.x, 0), max(-shift.y, 0), width, height);
Fedor Morozov's avatar
Fedor Morozov committed
172 173
        src(src_rect).copyTo(res(dst_rect));
        res.copyTo(dst);
Fedor Morozov's avatar
Fedor Morozov committed
174 175 176 177 178 179 180 181
    }

    int getMaxBits() const { return max_bits; }
    void setMaxBits(int val) { max_bits = val; }

    int getExcludeRange() const { return exclude_range; }
    void setExcludeRange(int val) { exclude_range = val; }

Fedor Morozov's avatar
Fedor Morozov committed
182 183 184
    bool getCut() const { return cut; }
    void setCut(bool val) { cut = val; }

Fedor Morozov's avatar
Fedor Morozov committed
185
    void write(FileStorage& fs) const
186 187
    {
        fs << "name" << name
Fedor Morozov's avatar
Fedor Morozov committed
188
           << "max_bits" << max_bits
189
           << "exclude_range" << exclude_range
Fedor Morozov's avatar
Fedor Morozov committed
190
           << "cut" << static_cast<int>(cut);
191 192 193 194 195 196 197
    }

    void read(const FileNode& fn)
    {
        FileNode n = fn["name"];
        CV_Assert(n.isString() && String(n) == name);
        max_bits = fn["max_bits"];
Fedor Morozov's avatar
Fedor Morozov committed
198
        exclude_range = fn["exclude_range"];
Fedor Morozov's avatar
Fedor Morozov committed
199
        int cut_val = fn["cut"];
200
        cut = (cut_val != 0);
Fedor Morozov's avatar
Fedor Morozov committed
201 202
    }

203
    void computeBitmaps(InputArray _img, OutputArray _tb, OutputArray _eb)
Fedor Morozov's avatar
Fedor Morozov committed
204
    {
205 206 207 208
        Mat img = _img.getMat();
        _tb.create(img.size(), CV_8U);
        _eb.create(img.size(), CV_8U);
        Mat tb = _tb.getMat(), eb = _eb.getMat();
Fedor Morozov's avatar
Fedor Morozov committed
209 210 211
        int median = getMedian(img);
        compare(img, median, tb, CMP_GT);
        compare(abs(img - median), exclude_range, eb, CMP_GT);
212 213 214
    }

protected:
Fedor Morozov's avatar
Fedor Morozov committed
215 216
    String name;
    int max_bits, exclude_range;
Fedor Morozov's avatar
Fedor Morozov committed
217
    bool cut;
Fedor Morozov's avatar
Fedor Morozov committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236

    void downsample(Mat& src, Mat& dst)
    {
        dst = Mat(src.rows / 2, src.cols / 2, CV_8UC1);

        int offset = src.cols * 2;
        uchar *src_ptr = src.ptr();
        uchar *dst_ptr = dst.ptr();
        for(int y = 0; y < dst.rows; y ++) {
            uchar *ptr = src_ptr;
            for(int x = 0; x < dst.cols; x++) {
                dst_ptr[0] = ptr[0];
                dst_ptr++;
                ptr += 2;
            }
            src_ptr += offset;
        }
    }

237
    void buildPyr(Mat& img, std::vector<Mat>& pyr, int maxlevel)
Fedor Morozov's avatar
Fedor Morozov committed
238 239 240 241 242 243 244 245 246 247 248
    {
        pyr.resize(maxlevel + 1);
        pyr[0] = img.clone();
        for(int level = 0; level < maxlevel; level++) {
            downsample(pyr[level], pyr[level + 1]);
        }
    }

    int getMedian(Mat& img)
    {
        int channels = 0;
249
        Mat hist;
Fedor Morozov's avatar
Fedor Morozov committed
250 251
        int hist_size = LDR_SIZE;
        float range[] = {0, LDR_SIZE} ;
Fedor Morozov's avatar
Fedor Morozov committed
252 253 254 255
        const float* ranges[] = {range};
        calcHist(&img, 1, &channels, Mat(), hist, 1, &hist_size, ranges);
        float *ptr = hist.ptr<float>();
        int median = 0, sum = 0;
Ilya Lavrenov's avatar
Ilya Lavrenov committed
256
        int thresh = (int)img.total() / 2;
Fedor Morozov's avatar
Fedor Morozov committed
257
        while(sum < thresh && median < LDR_SIZE) {
Fedor Morozov's avatar
Fedor Morozov committed
258 259 260 261 262
            sum += static_cast<int>(ptr[median]);
            median++;
        }
        return median;
    }
Fedor Morozov's avatar
Fedor Morozov committed
263
};
Fedor Morozov's avatar
Fedor Morozov committed
264

Fedor Morozov's avatar
Fedor Morozov committed
265 266
Ptr<AlignMTB> createAlignMTB(int max_bits, int exclude_range, bool cut)
{
267
    return makePtr<AlignMTBImpl>(max_bits, exclude_range, cut);
Fedor Morozov's avatar
Fedor Morozov committed
268 269
}

270
}