Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/photo/src/align.cpp
16354 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
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.
8
//
9
//
10
// License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
14
// Third party copyrights are property of their respective owners.
15
//
16
// Redistribution and use in source and binary forms, with or without modification,
17
// are permitted provided that the following conditions are met:
18
//
19
// * Redistribution's of source code must retain the above copyright notice,
20
// this list of conditions and the following disclaimer.
21
//
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.
25
//
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.
28
//
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.
39
//
40
//M*/
41
42
#include "precomp.hpp"
43
#include "opencv2/photo.hpp"
44
#include "opencv2/imgproc.hpp"
45
#include "hdr_common.hpp"
46
47
namespace cv
48
{
49
50
class AlignMTBImpl CV_FINAL : public AlignMTB
51
{
52
public:
53
AlignMTBImpl(int _max_bits, int _exclude_range, bool _cut) :
54
name("AlignMTB"),
55
max_bits(_max_bits),
56
exclude_range(_exclude_range),
57
cut(_cut)
58
{
59
}
60
61
void process(InputArrayOfArrays src, std::vector<Mat>& dst,
62
InputArray, InputArray) CV_OVERRIDE
63
{
64
CV_INSTRUMENT_REGION();
65
66
process(src, dst);
67
}
68
69
void process(InputArrayOfArrays _src, std::vector<Mat>& dst) CV_OVERRIDE
70
{
71
CV_INSTRUMENT_REGION();
72
73
std::vector<Mat> src;
74
_src.getMatVector(src);
75
76
checkImageDimensions(src);
77
dst.resize(src.size());
78
79
size_t pivot = src.size() / 2;
80
dst[pivot] = src[pivot];
81
Mat gray_base;
82
cvtColor(src[pivot], gray_base, COLOR_RGB2GRAY);
83
std::vector<Point> shifts;
84
85
for(size_t i = 0; i < src.size(); i++) {
86
if(i == pivot) {
87
shifts.push_back(Point(0, 0));
88
continue;
89
}
90
Mat gray;
91
cvtColor(src[i], gray, COLOR_RGB2GRAY);
92
Point shift = calculateShift(gray_base, gray);
93
shifts.push_back(shift);
94
shiftMat(src[i], dst[i], shift);
95
}
96
if(cut) {
97
Point max(0, 0), min(0, 0);
98
for(size_t i = 0; i < shifts.size(); i++) {
99
if(shifts[i].x > max.x) {
100
max.x = shifts[i].x;
101
}
102
if(shifts[i].y > max.y) {
103
max.y = shifts[i].y;
104
}
105
if(shifts[i].x < min.x) {
106
min.x = shifts[i].x;
107
}
108
if(shifts[i].y < min.y) {
109
min.y = shifts[i].y;
110
}
111
}
112
Point size = dst[0].size();
113
for(size_t i = 0; i < dst.size(); i++) {
114
dst[i] = dst[i](Rect(max, min + size));
115
}
116
}
117
}
118
119
Point calculateShift(InputArray _img0, InputArray _img1) CV_OVERRIDE
120
{
121
CV_INSTRUMENT_REGION();
122
123
Mat img0 = _img0.getMat();
124
Mat img1 = _img1.getMat();
125
CV_Assert(img0.channels() == 1 && img0.type() == img1.type());
126
CV_Assert(img0.size() == img1.size());
127
128
int maxlevel = static_cast<int>(log((double)max(img0.rows, img0.cols)) / log(2.0)) - 1;
129
maxlevel = min(maxlevel, max_bits - 1);
130
131
std::vector<Mat> pyr0;
132
std::vector<Mat> pyr1;
133
buildPyr(img0, pyr0, maxlevel);
134
buildPyr(img1, pyr1, maxlevel);
135
136
Point shift(0, 0);
137
for(int level = maxlevel; level >= 0; level--) {
138
139
shift *= 2;
140
Mat tb1, tb2, eb1, eb2;
141
computeBitmaps(pyr0[level], tb1, eb1);
142
computeBitmaps(pyr1[level], tb2, eb2);
143
144
int min_err = (int)pyr0[level].total();
145
Point new_shift(shift);
146
for(int i = -1; i <= 1; i++) {
147
for(int j = -1; j <= 1; j++) {
148
Point test_shift = shift + Point(i, j);
149
Mat shifted_tb2, shifted_eb2, diff;
150
shiftMat(tb2, shifted_tb2, test_shift);
151
shiftMat(eb2, shifted_eb2, test_shift);
152
bitwise_xor(tb1, shifted_tb2, diff);
153
bitwise_and(diff, eb1, diff);
154
bitwise_and(diff, shifted_eb2, diff);
155
int err = countNonZero(diff);
156
if(err < min_err) {
157
new_shift = test_shift;
158
min_err = err;
159
}
160
}
161
}
162
shift = new_shift;
163
}
164
return shift;
165
}
166
167
void shiftMat(InputArray _src, OutputArray _dst, const Point shift) CV_OVERRIDE
168
{
169
CV_INSTRUMENT_REGION();
170
171
Mat src = _src.getMat();
172
_dst.create(src.size(), src.type());
173
Mat dst = _dst.getMat();
174
175
Mat res = Mat::zeros(src.size(), src.type());
176
int width = src.cols - abs(shift.x);
177
int height = src.rows - abs(shift.y);
178
Rect dst_rect(max(shift.x, 0), max(shift.y, 0), width, height);
179
Rect src_rect(max(-shift.x, 0), max(-shift.y, 0), width, height);
180
src(src_rect).copyTo(res(dst_rect));
181
res.copyTo(dst);
182
}
183
184
int getMaxBits() const CV_OVERRIDE { return max_bits; }
185
void setMaxBits(int val) CV_OVERRIDE { max_bits = val; }
186
187
int getExcludeRange() const CV_OVERRIDE { return exclude_range; }
188
void setExcludeRange(int val) CV_OVERRIDE { exclude_range = val; }
189
190
bool getCut() const CV_OVERRIDE { return cut; }
191
void setCut(bool val) CV_OVERRIDE { cut = val; }
192
193
void write(FileStorage& fs) const CV_OVERRIDE
194
{
195
writeFormat(fs);
196
fs << "name" << name
197
<< "max_bits" << max_bits
198
<< "exclude_range" << exclude_range
199
<< "cut" << static_cast<int>(cut);
200
}
201
202
void read(const FileNode& fn) CV_OVERRIDE
203
{
204
FileNode n = fn["name"];
205
CV_Assert(n.isString() && String(n) == name);
206
max_bits = fn["max_bits"];
207
exclude_range = fn["exclude_range"];
208
int cut_val = fn["cut"];
209
cut = (cut_val != 0);
210
}
211
212
void computeBitmaps(InputArray _img, OutputArray _tb, OutputArray _eb) CV_OVERRIDE
213
{
214
CV_INSTRUMENT_REGION();
215
216
Mat img = _img.getMat();
217
_tb.create(img.size(), CV_8U);
218
_eb.create(img.size(), CV_8U);
219
Mat tb = _tb.getMat(), eb = _eb.getMat();
220
int median = getMedian(img);
221
compare(img, median, tb, CMP_GT);
222
compare(abs(img - median), exclude_range, eb, CMP_GT);
223
}
224
225
protected:
226
String name;
227
int max_bits, exclude_range;
228
bool cut;
229
230
void downsample(Mat& src, Mat& dst)
231
{
232
dst = Mat(src.rows / 2, src.cols / 2, CV_8UC1);
233
234
int offset = src.cols * 2;
235
uchar *src_ptr = src.ptr();
236
uchar *dst_ptr = dst.ptr();
237
for(int y = 0; y < dst.rows; y ++) {
238
uchar *ptr = src_ptr;
239
for(int x = 0; x < dst.cols; x++) {
240
dst_ptr[0] = ptr[0];
241
dst_ptr++;
242
ptr += 2;
243
}
244
src_ptr += offset;
245
}
246
}
247
248
void buildPyr(Mat& img, std::vector<Mat>& pyr, int maxlevel)
249
{
250
pyr.resize(maxlevel + 1);
251
pyr[0] = img.clone();
252
for(int level = 0; level < maxlevel; level++) {
253
downsample(pyr[level], pyr[level + 1]);
254
}
255
}
256
257
int getMedian(Mat& img)
258
{
259
int channels = 0;
260
Mat hist;
261
int hist_size = LDR_SIZE;
262
float range[] = {0, LDR_SIZE} ;
263
const float* ranges[] = {range};
264
calcHist(&img, 1, &channels, Mat(), hist, 1, &hist_size, ranges);
265
float *ptr = hist.ptr<float>();
266
int median = 0, sum = 0;
267
int thresh = (int)img.total() / 2;
268
while(sum < thresh && median < LDR_SIZE) {
269
sum += static_cast<int>(ptr[median]);
270
median++;
271
}
272
return median;
273
}
274
};
275
276
Ptr<AlignMTB> createAlignMTB(int max_bits, int exclude_range, bool cut)
277
{
278
return makePtr<AlignMTBImpl>(max_bits, exclude_range, cut);
279
}
280
281
}
282
283