-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagepyramid.cpp
247 lines (190 loc) · 6.44 KB
/
imagepyramid.cpp
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include "imagepyramid.h"
ImagePyramid::ImagePyramid(const Mat &src, const Size &size) :
ImagePyramid(src)
{
setSize(size);
}
ImagePyramid::ImagePyramid(const Mat &src) {
setImage(src, true);
}
ImagePyramid::ImagePyramid(
const ImagePyramid &src1,
const ImagePyramid &src2,
const Mat &src1Mask
) {
assert(src1Mask.cols == src1.getWidth() && src1Mask.rows == src1.getHeight());
Mat mask = src1Mask.clone(); // so it can be modified
//combine pyramids
for (int layer = 0; layer < src1.getLayers(); layer++) {
Mat src1Lap = src1.getLaplacian(layer);
Mat src2Lap = src2.getLaplacian(layer);
laplacianPyr.push_back(
addMaskedLaplacian(
src1Lap,
src2Lap,
mask
)
);
// downsize mask to fit next layer
pyrDown(mask, mask);
}
// reconstruct image and set both resizedImage and image
reconstructImage();
}
int ImagePyramid::setImage(const Mat &src, bool keepSize) {
if (src.empty()) {
return 1; // error
}
else {
src.copyTo(image);
// keepsize = slightly change to add more layers
// otherwise, use current size and resize
if (keepSize) {
int width = image.cols;
int height = image.rows;
if (width % 2 == 1) width--;
if (height % 2 == 1) height--;
setSize(width, height);
}
else {
resizeImage();
}
return 0;
}
}
int ImagePyramid::setSize (const Size &size) {
if (size.height <= 0 || size.width <= 0) {
return 1; // error
}
else {
this->imageSize = size;
resizeImage();
generatePyramid(); // generates Laplacian pyramid
return 0;
}
}
int ImagePyramid::setLayers(int layers) {
if (layers <= 1) {
return -1;
}
else if (layers > maxLayers()) {
return 1;
}
else {
if (layers == getLayers()); // do nothing
else if (layers < getLayers()) {
// shrink pyramid
while (layers < getLayers()) {
shrinkPyramid();
}
}
else if (layers > getLayers()) {
// expand pyramid
while(layers > getLayers()) {
expandPyramid();
}
}
return 0;
}
}
void ImagePyramid::generatePyramid() {
// Save number of layers
// unsigned int layers = getLayers();
// Use the maximum number of layers always
unsigned int layers = maxLayers();
// Start from scratch
laplacianPyr.clear();
// Add resized image as layer 0
// changed to CV_8S
Mat layer0;
laplacianPyr.push_back(resizedImage.clone());
// Expand pyramd until there are enough layers
while (laplacianPyr.size() < layers) {
expandPyramid();
}
}
void ImagePyramid::expandPyramid() {
Mat layer1 = laplacianPyr.back().clone();
laplacianPyr.pop_back();
Mat layer2;
pyrDown(layer1, layer2);
Mat layer2Upscaled;
pyrUp(layer2, layer2Upscaled);
Mat layer1Laplacian;
subtract(layer1, layer2Upscaled, layer1Laplacian, noArray(), CV_8S);
laplacianPyr.push_back(layer1Laplacian);
laplacianPyr.push_back(layer2);
}
void ImagePyramid::shrinkPyramid() {
Mat layer1 = laplacianPyr.back();
laplacianPyr.pop_back();
Mat layer2 = laplacianPyr.back();
laplacianPyr.pop_back();
pyrUp(layer2, layer2);
Mat layer1New;
subtract(layer1, layer2, layer1New);
laplacianPyr.push_back(layer1New);
}
Mat ImagePyramid::addMaskedLaplacian(
const Mat &src1, const Mat &src2,
const Mat &src1Mask) const {
assert(!src1.empty() && !src2.empty() && !src1Mask.empty());
assert(src1.rows == src2.rows && src1.cols == src2.cols);
assert(src1Mask.rows == src1.rows && src1Mask.cols == src1.cols);
Mat combined;
// assert left and right are same size
assert(src1.rows == src2.rows);
assert(src1.cols == src2.cols);
assert(src1.channels() == src2.channels());
assert(src1.type() == src2.type());
assert(src1.channels() == 3);
assert(src1Mask.type() == CV_32FC1);
// create dst of same size as left and right
combined.create(src1.rows, src1.cols, src1.type());
for (int col = 0; col < src1.cols; col++) {
for (int row = 0; row < src1.rows; row++) {
int p = (row * (col+1) + col);
// mask values
float leftMaskValue = src1Mask.at<float>(row, col); // Error here
float rightMaskValue = 1 - leftMaskValue;
// colors of source images
Vec3b leftColor = src1.at<Vec3b>(row, col);
Vec3b rightColor = src2.at<Vec3b>(row, col);
// reference for modifying dst image
Vec3b & dstColor = combined.at<Vec3b>(row, col);
// signed type
if (src1.depth() == CV_8S) {
dstColor[0] = ((signed char)leftColor[0] * leftMaskValue + (signed char)rightColor[0] * rightMaskValue);
dstColor[1] = ((signed char)leftColor[1] * leftMaskValue + (signed char)rightColor[1] * rightMaskValue);
dstColor[2] = ((signed char)leftColor[2] * leftMaskValue + (signed char)rightColor[2] * rightMaskValue);
}
// unsigned type
else if (src1.depth() == CV_8U) {
dstColor[0] = ((uint8_t)leftColor[0] * leftMaskValue + (uint8_t)rightColor[0] * rightMaskValue);
dstColor[1] = ((uint8_t)leftColor[1] * leftMaskValue + (uint8_t)rightColor[1] * rightMaskValue);
dstColor[2] = ((uint8_t)leftColor[2] * leftMaskValue + (uint8_t)rightColor[2] * rightMaskValue);
}
}
}
return combined;
}
void ImagePyramid::reconstructImage() {
Mat image;
int layers = getLayers();
// start with the last layer (should be unsigned)
image = laplacianPyr.back();
// from second last to first
for (int layer = layers-2; layer >= 0; layer--) {
// upscale and add previous layer
pyrUp(image, image);
add(image, laplacianPyr[layer], image, noArray(), image.type());
}
// set image and resizedImage without using setters
this->image = image.clone();
this->resizedImage = image.clone();
}
Mat ImagePyramid::getResizedImage(const Size &size) const{
Mat img;
resize(image, img, size, INTER_CUBIC);
return img;
}