1
/*M///////////////////////////////////////////////////////////////////////////////////////
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
11
// For Open Source Computer Vision Library
13
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
14
// Third party copyrights are property of their respective owners.
16
// Redistribution and use in source and binary forms, with or without modification,
17
// are permitted provided that the following conditions are met:
19
// * Redistribution's of source code must retain the above copyright notice,
20
// this list of conditions and the following disclaimer.
22
// * Redistribution's in binary form must reproduce the above copyright notice,
23
// this list of conditions and the following disclaimer in the documentation
24
// and/or other materials provided with the distribution.
26
// * The name of the copyright holders may not be used to endorse or promote products
27
// derived from this software without specific prior written permission.
29
// This software is provided by the copyright holders and contributors "as is" and
30
// any express or implied warranties, including, but not limited to, the implied
31
// warranties of merchantability and fitness for a particular purpose are disclaimed.
32
// In no event shall the Intel Corporation or contributors be liable for any direct,
33
// indirect, incidental, special, exemplary, or consequential damages
34
// (including, but not limited to, procurement of substitute goods or services;
35
// loss of use, data, or profits; or business interruption) however caused
36
// and on any theory of liability, whether in contract, strict liability,
37
// or tort (including negligence or otherwise) arising in any way out of
38
// the use of this software, even if advised of the possibility of such damage.
42
#include "precomp.hpp"
43
#include "opencv2/photo.hpp"
44
#include "opencv2/imgproc.hpp"
45
#include "hdr_common.hpp"
50
class AlignMTBImpl : public AlignMTB
53
AlignMTBImpl(int _max_bits, int _exclude_range, bool _cut) :
56
exclude_range(_exclude_range),
61
void process(InputArrayOfArrays src, std::vector<Mat>& dst,
62
InputArray, InputArray)
67
void process(InputArrayOfArrays _src, std::vector<Mat>& dst)
70
_src.getMatVector(src);
72
checkImageDimensions(src);
73
dst.resize(src.size());
75
size_t pivot = src.size() / 2;
76
dst[pivot] = src[pivot];
78
cvtColor(src[pivot], gray_base, COLOR_RGB2GRAY);
79
std::vector<Point> shifts;
81
for(size_t i = 0; i < src.size(); i++) {
83
shifts.push_back(Point(0, 0));
87
cvtColor(src[i], gray, COLOR_RGB2GRAY);
88
Point shift = calculateShift(gray_base, gray);
89
shifts.push_back(shift);
90
shiftMat(src[i], dst[i], shift);
93
Point max(0, 0), min(0, 0);
94
for(size_t i = 0; i < shifts.size(); i++) {
95
if(shifts[i].x > max.x) {
98
if(shifts[i].y > max.y) {
101
if(shifts[i].x < min.x) {
104
if(shifts[i].y < min.y) {
108
Point size = dst[0].size();
109
for(size_t i = 0; i < dst.size(); i++) {
110
dst[i] = dst[i](Rect(max, min + size));
115
Point calculateShift(InputArray _img0, InputArray _img1)
117
Mat img0 = _img0.getMat();
118
Mat img1 = _img1.getMat();
119
CV_Assert(img0.channels() == 1 && img0.type() == img1.type());
120
CV_Assert(img0.size() == img0.size());
122
int maxlevel = static_cast<int>(log((double)max(img0.rows, img0.cols)) / log(2.0)) - 1;
123
maxlevel = min(maxlevel, max_bits - 1);
125
std::vector<Mat> pyr0;
126
std::vector<Mat> pyr1;
127
buildPyr(img0, pyr0, maxlevel);
128
buildPyr(img1, pyr1, maxlevel);
131
for(int level = maxlevel; level >= 0; level--) {
134
Mat tb1, tb2, eb1, eb2;
135
computeBitmaps(pyr0[level], tb1, eb1);
136
computeBitmaps(pyr1[level], tb2, eb2);
138
int min_err = (int)pyr0[level].total();
139
Point new_shift(shift);
140
for(int i = -1; i <= 1; i++) {
141
for(int j = -1; j <= 1; j++) {
142
Point test_shift = shift + Point(i, j);
143
Mat shifted_tb2, shifted_eb2, diff;
144
shiftMat(tb2, shifted_tb2, test_shift);
145
shiftMat(eb2, shifted_eb2, test_shift);
146
bitwise_xor(tb1, shifted_tb2, diff);
147
bitwise_and(diff, eb1, diff);
148
bitwise_and(diff, shifted_eb2, diff);
149
int err = countNonZero(diff);
151
new_shift = test_shift;
161
void shiftMat(InputArray _src, OutputArray _dst, const Point shift)
163
Mat src = _src.getMat();
164
_dst.create(src.size(), src.type());
165
Mat dst = _dst.getMat();
167
Mat res = Mat::zeros(src.size(), src.type());
168
int width = src.cols - abs(shift.x);
169
int height = src.rows - abs(shift.y);
170
Rect dst_rect(max(shift.x, 0), max(shift.y, 0), width, height);
171
Rect src_rect(max(-shift.x, 0), max(-shift.y, 0), width, height);
172
src(src_rect).copyTo(res(dst_rect));
176
int getMaxBits() const { return max_bits; }
177
void setMaxBits(int val) { max_bits = val; }
179
int getExcludeRange() const { return exclude_range; }
180
void setExcludeRange(int val) { exclude_range = val; }
182
bool getCut() const { return cut; }
183
void setCut(bool val) { cut = val; }
185
void write(FileStorage& fs) const
188
<< "max_bits" << max_bits
189
<< "exclude_range" << exclude_range
190
<< "cut" << static_cast<int>(cut);
193
void read(const FileNode& fn)
195
FileNode n = fn["name"];
196
CV_Assert(n.isString() && String(n) == name);
197
max_bits = fn["max_bits"];
198
exclude_range = fn["exclude_range"];
199
int cut_val = fn["cut"];
200
cut = (cut_val != 0);
203
void computeBitmaps(InputArray _img, OutputArray _tb, OutputArray _eb)
205
Mat img = _img.getMat();
206
_tb.create(img.size(), CV_8U);
207
_eb.create(img.size(), CV_8U);
208
Mat tb = _tb.getMat(), eb = _eb.getMat();
209
int median = getMedian(img);
210
compare(img, median, tb, CMP_GT);
211
compare(abs(img - median), exclude_range, eb, CMP_GT);
216
int max_bits, exclude_range;
219
void downsample(Mat& src, Mat& dst)
221
dst = Mat(src.rows / 2, src.cols / 2, CV_8UC1);
223
int offset = src.cols * 2;
224
uchar *src_ptr = src.ptr();
225
uchar *dst_ptr = dst.ptr();
226
for(int y = 0; y < dst.rows; y ++) {
227
uchar *ptr = src_ptr;
228
for(int x = 0; x < dst.cols; x++) {
237
void buildPyr(Mat& img, std::vector<Mat>& pyr, int maxlevel)
239
pyr.resize(maxlevel + 1);
240
pyr[0] = img.clone();
241
for(int level = 0; level < maxlevel; level++) {
242
downsample(pyr[level], pyr[level + 1]);
246
int getMedian(Mat& img)
250
int hist_size = LDR_SIZE;
251
float range[] = {0, LDR_SIZE} ;
252
const float* ranges[] = {range};
253
calcHist(&img, 1, &channels, Mat(), hist, 1, &hist_size, ranges);
254
float *ptr = hist.ptr<float>();
255
int median = 0, sum = 0;
256
int thresh = (int)img.total() / 2;
257
while(sum < thresh && median < LDR_SIZE) {
258
sum += static_cast<int>(ptr[median]);
265
Ptr<AlignMTB> createAlignMTB(int max_bits, int exclude_range, bool cut)
267
return makePtr<AlignMTBImpl>(max_bits, exclude_range, cut);