forked from alicevision/AliceVision
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageAlgo.cpp
More file actions
350 lines (289 loc) · 12.8 KB
/
imageAlgo.cpp
File metadata and controls
350 lines (289 loc) · 12.8 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include "imageAlgo.hpp"
#include <aliceVision/mvsData/Color.hpp>
#include <aliceVision/mvsData/Rgb.hpp>
#include <aliceVision/mvsData/Image.hpp>
#include <OpenImageIO/imageio.h>
#include <OpenImageIO/imagebuf.h>
#include <OpenImageIO/imagebufalgo.h>
#include <Eigen/Eigen>
namespace aliceVision
{
namespace imageAlgo
{
float func_XYZtoLAB(float t)
{
if(t > 0.008856f)
return std::pow(t, 1.0f/3.0f);
else
return t / 0.1284f + 0.1379f;
}
float func_LABtoXYZ(float t)
{
if(t > 0.2069f)
return std::pow(t, 3.0f);
else
return 0.1284f * (t - 0.1379f);
}
void RGBtoXYZ(oiio::ImageBuf::Iterator<float>& pixel)
{
const Eigen::Vector3f rgb(pixel[0], pixel[1], pixel[2]);
Eigen::Matrix3f M;
M << 0.4124f, 0.3576f, 0.1805f,
0.2126f, 0.7152f, 0.0722f,
0.0193f, 0.1192f, 0.9504f;
const Eigen::Vector3f xyz_vec = M * rgb;
pixel[0] = xyz_vec[0] * 0.9505f;
pixel[1] = xyz_vec[1];
pixel[2] = xyz_vec[2] * 1.0890f;
}
void XYZtoRGB(oiio::ImageBuf::Iterator<float>& pixel)
{
const Eigen::Vector3f xyz(pixel[0] / 0.9505f, pixel[1], pixel[2] / 1.0890f);
Eigen::Matrix3f M;
M << 3.2406f, -1.5372f, -0.4986f,
-0.9689f, 1.8758f, 0.0415f,
0.0557f, -0.2040f, 1.0570f;
const Eigen::Vector3f rgb_vec = M * xyz;
pixel[0] = rgb_vec[0];
pixel[1] = rgb_vec[1];
pixel[2] = rgb_vec[2];
}
void XYZtoLAB(oiio::ImageBuf::Iterator<float>& pixel)
{
float L = 116.0f * func_XYZtoLAB(pixel[1]) - 16.0f;
float A = 500.0f * (func_XYZtoLAB(pixel[0]) - func_XYZtoLAB(pixel[1]));
float B = 200.0f * (func_XYZtoLAB(pixel[1]) - func_XYZtoLAB(pixel[2]));
pixel[0] = L / 100.0f;
pixel[1] = A / 100.0f;
pixel[2] = B / 100.0f;
}
void LABtoXYZ(oiio::ImageBuf::Iterator<float>& pixel)
{
float L_offset = (pixel[0] * 100.0f + 16.0f) / 116.0f;
pixel[0] = func_LABtoXYZ(L_offset + pixel[1] * 100.0f / 500.0f);
pixel[1] = func_LABtoXYZ(L_offset);
pixel[2] = func_LABtoXYZ(L_offset - pixel[2] * 100.0f / 200.0f);
}
void RGBtoLAB(oiio::ImageBuf::Iterator<float>& pixel)
{
RGBtoXYZ(pixel);
XYZtoLAB(pixel);
}
void LABtoRGB(oiio::ImageBuf::Iterator<float>& pixel)
{
LABtoXYZ(pixel);
XYZtoRGB(pixel);
}
void processImage(oiio::ImageBuf& image, std::function<void(oiio::ImageBuf::Iterator<float>&)> pixelFunc)
{
oiio::ImageBufAlgo::parallel_image(image.roi(), [&image, &pixelFunc](oiio::ROI roi) {
for(oiio::ImageBuf::Iterator<float> pixel(image, roi); !pixel.done(); ++pixel)
{
pixelFunc(pixel);
}
});
}
void processImage(oiio::ImageBuf& dst, const oiio::ImageBuf& src, std::function<void(oiio::ImageBuf::Iterator<float>&)> pixelFunc)
{
dst.copy(src);
processImage(dst, pixelFunc);
}
void colorconvert(oiio::ImageBuf& imgBuf, imageIO::EImageColorSpace fromColorSpace, imageIO::EImageColorSpace toColorSpace)
{
using namespace imageIO;
if(fromColorSpace == toColorSpace)
return;
else if(toColorSpace == EImageColorSpace::LINEAR)
{
if(fromColorSpace == EImageColorSpace::SRGB)
oiio::ImageBufAlgo::colorconvert(imgBuf, imgBuf,
EImageColorSpace_enumToString(EImageColorSpace::SRGB), EImageColorSpace_enumToString(EImageColorSpace::LINEAR));
else if(fromColorSpace == EImageColorSpace::XYZ)
processImage(imgBuf, &XYZtoRGB);
else if(fromColorSpace == EImageColorSpace::LAB)
processImage(imgBuf, &LABtoRGB);
}
else if(toColorSpace == EImageColorSpace::SRGB)
{
if(fromColorSpace == EImageColorSpace::XYZ)
processImage(imgBuf, &XYZtoRGB);
else if(fromColorSpace == EImageColorSpace::LAB)
processImage(imgBuf, &LABtoRGB);
oiio::ImageBufAlgo::colorconvert(imgBuf, imgBuf,
EImageColorSpace_enumToString(EImageColorSpace::LINEAR), EImageColorSpace_enumToString(EImageColorSpace::SRGB));
}
else if(toColorSpace == EImageColorSpace::XYZ)
{
if(fromColorSpace == EImageColorSpace::LINEAR)
processImage(imgBuf, &RGBtoXYZ);
else if(fromColorSpace == EImageColorSpace::SRGB)
{
oiio::ImageBufAlgo::colorconvert(imgBuf, imgBuf,
EImageColorSpace_enumToString(EImageColorSpace::SRGB), EImageColorSpace_enumToString(EImageColorSpace::LINEAR));
processImage(imgBuf, &RGBtoXYZ);
}
else if(fromColorSpace == EImageColorSpace::LAB)
processImage(imgBuf, &LABtoXYZ);
}
else if(toColorSpace == EImageColorSpace::LAB)
{
if(fromColorSpace == EImageColorSpace::LINEAR)
processImage(imgBuf, &RGBtoLAB);
else if(fromColorSpace == EImageColorSpace::SRGB)
{
oiio::ImageBufAlgo::colorconvert(imgBuf, imgBuf,
EImageColorSpace_enumToString(EImageColorSpace::SRGB), EImageColorSpace_enumToString(EImageColorSpace::LINEAR));
processImage(imgBuf, &RGBtoLAB);
}
else if(fromColorSpace == EImageColorSpace::XYZ)
processImage(imgBuf, &XYZtoLAB);
}
ALICEVISION_LOG_TRACE("Convert image from " << EImageColorSpace_enumToString(fromColorSpace) << " to " << EImageColorSpace_enumToString(toColorSpace));
}
void colorconvert(Image& image, imageIO::EImageColorSpace fromColorSpace, imageIO::EImageColorSpace toColorSpace)
{
oiio::ImageSpec imageSpec(image.width(), image.height(), 3, oiio::TypeDesc::FLOAT);
std::vector<Color>& buffer = image.data();
oiio::ImageBuf imageBuf(imageSpec, buffer.data());
colorconvert(imageBuf, fromColorSpace, toColorSpace);
}
void colorconvert(oiio::ImageBuf& dst, const oiio::ImageBuf& src, imageIO::EImageColorSpace fromColorSpace, imageIO::EImageColorSpace toColorSpace)
{
dst.copy(src);
colorconvert(dst, fromColorSpace, toColorSpace);
}
template<typename T>
void transposeImage(oiio::TypeDesc typeDesc,
int width,
int height,
int nchannels,
std::vector<T>& buffer)
{
oiio::ImageSpec imageSpec(width, height, nchannels, typeDesc);
oiio::ImageBuf inBuf(imageSpec, buffer.data());
oiio::ImageBuf transposeBuf;
oiio::ImageBufAlgo::transpose(transposeBuf, inBuf, oiio::ROI::All());
transposeBuf.get_pixels(oiio::ROI::All(), typeDesc, buffer.data());
}
void transposeImage(int width, int height, std::vector<unsigned char>& buffer)
{
transposeImage(oiio::TypeDesc::UCHAR, width, height, 1, buffer);
}
void transposeImage(int width, int height, std::vector<rgb>& buffer)
{
transposeImage(oiio::TypeDesc::UCHAR, width, height, 3, buffer);
}
void transposeImage(int width, int height, std::vector<float>& buffer)
{
transposeImage(oiio::TypeDesc::FLOAT, width, height, 1, buffer);
}
void transposeImage(int width, int height, std::vector<Color>& buffer)
{
transposeImage(oiio::TypeDesc::FLOAT, width, height, 3, buffer);
}
void transposeImage(Image &image)
{
transposeImage(oiio::TypeDesc::FLOAT, image.width(), image.height(), 3, image.data());
}
template<typename T>
void resizeImage(oiio::TypeDesc typeDesc,
int inWidth,
int inHeight,
int nchannels,
int downscale,
const std::vector<T>& inBuffer,
std::vector<T>& outBuffer,
const std::string& filter = "",
float filterSize = 0)
{
const int outWidth = inWidth / downscale;
const int outHeight = inHeight / downscale;
outBuffer.resize(outWidth * outHeight);
const oiio::ImageBuf inBuf(oiio::ImageSpec(inWidth, inHeight, nchannels, typeDesc), const_cast<T*>(inBuffer.data()));
oiio::ImageBuf outBuf(oiio::ImageSpec(outWidth, outHeight, nchannels, typeDesc), outBuffer.data());
oiio::ImageBufAlgo::resize(outBuf, inBuf, filter, filterSize, oiio::ROI::All());
}
void resizeImage(int inWidth, int inHeight, int downscale, const std::vector<unsigned char>& inBuffer, std::vector<unsigned char>& outBuffer, const std::string& filter, float filterSize)
{
resizeImage(oiio::TypeDesc::UCHAR, inWidth, inHeight, 1, downscale, inBuffer, outBuffer, filter, filterSize);
}
void resizeImage(int inWidth, int inHeight, int downscale, const std::vector<rgb>& inBuffer, std::vector<rgb>& outBuffer, const std::string& filter, float filterSize)
{
resizeImage(oiio::TypeDesc::UCHAR, inWidth, inHeight, 3, downscale, inBuffer, outBuffer, filter, filterSize);
}
void resizeImage(int inWidth, int inHeight, int downscale, const std::vector<float>& inBuffer, std::vector<float>& outBuffer, const std::string& filter, float filterSize)
{
resizeImage(oiio::TypeDesc::FLOAT, inWidth, inHeight, 1, downscale, inBuffer, outBuffer, filter, filterSize);
}
void resizeImage(int inWidth, int inHeight, int downscale, const std::vector<Color>& inBuffer, std::vector<Color>& outBuffer, const std::string& filter, float filterSize)
{
resizeImage(oiio::TypeDesc::FLOAT, inWidth, inHeight, 3, downscale, inBuffer, outBuffer, filter, filterSize);
}
void resizeImage(int downscale, const Image &inImage, Image &outImage, const std::string &filter, float filterSize)
{
resizeImage(oiio::TypeDesc::FLOAT, inImage.width(), inImage.height(), 3, downscale, inImage.data(), outImage.data(), filter, filterSize);
outImage.setHeight(inImage.height() / downscale);
outImage.setWidth(inImage.width() / downscale);
}
template<typename T>
void convolveImage(oiio::TypeDesc typeDesc,
int inWidth,
int inHeight,
int nchannels,
const std::vector<T>& inBuffer,
std::vector<T>& outBuffer,
const std::string& kernel,
float kernelWidth,
float kernelHeight)
{
outBuffer.resize(inBuffer.size());
const oiio::ImageBuf inBuf(oiio::ImageSpec(inWidth, inHeight, nchannels, typeDesc), const_cast<T*>(inBuffer.data()));
oiio::ImageBuf outBuf(oiio::ImageSpec(inWidth, inHeight, nchannels, typeDesc), outBuffer.data());
oiio::ImageBuf K;
oiio::ImageBufAlgo::make_kernel(K, kernel, kernelWidth, kernelHeight);
oiio::ImageBufAlgo::convolve(outBuf, inBuf, K);
}
void convolveImage(int inWidth, int inHeight, const std::vector<unsigned char>& inBuffer, std::vector<unsigned char>& outBuffer, const std::string& kernel, float kernelWidth, float kernelHeight)
{
convolveImage(oiio::TypeDesc::UCHAR, inWidth, inHeight, 1, inBuffer, outBuffer, kernel, kernelWidth, kernelHeight);
}
void convolveImage(int inWidth, int inHeight, const std::vector<rgb>& inBuffer, std::vector<rgb>& outBuffer, const std::string& kernel, float kernelWidth, float kernelHeight)
{
convolveImage(oiio::TypeDesc::UCHAR, inWidth, inHeight, 3, inBuffer, outBuffer, kernel, kernelWidth, kernelHeight);
}
void convolveImage(int inWidth, int inHeight, const std::vector<float>& inBuffer, std::vector<float>& outBuffer, const std::string& kernel, float kernelWidth, float kernelHeight)
{
convolveImage(oiio::TypeDesc::FLOAT, inWidth, inHeight, 1, inBuffer, outBuffer, kernel, kernelWidth, kernelHeight);
}
void convolveImage(int inWidth, int inHeight, const std::vector<Color>& inBuffer, std::vector<Color>& outBuffer, const std::string& kernel, float kernelWidth, float kernelHeight)
{
convolveImage(oiio::TypeDesc::FLOAT, inWidth, inHeight, 3, inBuffer, outBuffer, kernel, kernelWidth, kernelHeight);
}
void convolveImage(const Image &inImage, Image &outImage, const std::string &kernel, float kernelWidth, float kernelHeight)
{
convolveImage(oiio::TypeDesc::FLOAT, inImage.width(), inImage.height(), 3, inImage.data(), outImage.data(), kernel, kernelWidth, kernelHeight);
outImage.setHeight(inImage.height());
outImage.setWidth(inImage.width());
}
void fillHoles(int inWidth, int inHeight, std::vector<Color>& colorBuffer, const std::vector<float>& alphaBuffer)
{
oiio::ImageBuf rgbBuf(oiio::ImageSpec(inWidth, inHeight, 3, oiio::TypeDesc::FLOAT), colorBuffer.data());
const oiio::ImageBuf alphaBuf(oiio::ImageSpec(inWidth, inHeight, 1, oiio::TypeDesc::FLOAT), const_cast<float*>(alphaBuffer.data()));
// Create RGBA ImageBuf from source buffers with correct channel names
// (identified alpha channel is needed for fillholes_pushpull)
oiio::ImageBuf rgbaBuf;
oiio::ImageBufAlgo::channel_append(rgbaBuf, rgbBuf, alphaBuf);
rgbaBuf.specmod().default_channel_names();
// Temp RGBA buffer to store fillholes result
oiio::ImageBuf filledBuf;
oiio::ImageBufAlgo::fillholes_pushpull(filledBuf, rgbaBuf);
rgbaBuf.clear();
// Copy result to original RGB buffer
oiio::ImageBufAlgo::copy(rgbBuf, filledBuf);
}
void fillHoles(Image& image, const std::vector<float>& alphaBuffer)
{
fillHoles(image.width(), image.height(), image.data(), alphaBuffer);
}
} // namespace imageAlgo
} // namspace aliceVision