-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageoperations.cpp
More file actions
747 lines (579 loc) · 23.7 KB
/
imageoperations.cpp
File metadata and controls
747 lines (579 loc) · 23.7 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
// Copyright 2020 José María Castelo Ares
// This file is part of MorphogenCV.
// MorphogenCV is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// MorphogenCV is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with MorphogenCV. If not, see <https://www.gnu.org/licenses/>.
#include "imageoperations.h"
// Bilateral filter
std::string BilateralFilter::name = "Blur: bilateral";
BilateralFilter::BilateralFilter(bool on, int d, double sc, double ss): ImageOperation(on)
{
diameter = new IntParameter("Diameter", d, 0, 50, false);
double minSigmaColor, maxSigmaColor;
adjustMinMax(sc, 0.0, 300.0, minSigmaColor, maxSigmaColor);
sigmaColor = new DoubleParameter("Sigma color", sc, minSigmaColor, maxSigmaColor, 0.0, 1.0e6);
double minSigmaSpace, maxSigmaSpace;
adjustMinMax(ss, 0.0, 300.0, minSigmaSpace, maxSigmaSpace);
sigmaSpace = new DoubleParameter("Sigma space", ss, minSigmaSpace, maxSigmaSpace, 0.0, 1.0e6);
}
void BilateralFilter::applyOperation(cv::Mat &src)
{
cv::Mat dst(src.rows, src.cols, src.type());
cv::bilateralFilter(src, dst, diameter->value, sigmaColor->value, sigmaSpace->value, cv::BORDER_ISOLATED);
src = dst.clone();
}
// Blend previous images
std::string BlendPreviousImages::name = "Blend previous images";
BlendPreviousImages::BlendPreviousImages(bool on, int s, double bf): ImageOperation(on)
{
size = new IntParameter("Number of images", s, 0, 100, false);
oldSize = s;
double minBlendFactor, maxBlendFactor;
adjustMinMax(bf, 0.0, 0.5, minBlendFactor, maxBlendFactor);
blendFactor = new DoubleParameter("Blend factor", bf, minBlendFactor, maxBlendFactor, 0.0, 1.0);
}
void BlendPreviousImages::applyOperation(cv::Mat &src)
{
if (oldSize != size->value)
{
if (size->value < oldSize)
for (int i = 0; i < oldSize - size->value; i++)
previousImages.erase(previousImages.begin() + i);
oldSize = size->value;
}
int imax = previousImages.size();
for (int i = 0; i < imax; i++)
cv::addWeighted(src, 1.0, previousImages[i], blendFactor->value * (imax - i) / imax, 0.0, src);
if (size->value > 0)
{
if (!previousImages.empty() && static_cast<int>(previousImages.size()) == size->value)
previousImages.erase(previousImages.begin());
previousImages.push_back(src.clone());
}
}
// Blur
std::string Blur::name = "Blur: homogeneous";
Blur::Blur(bool on, int size): ImageOperation(on)
{
ksize = new IntParameter("Kernel size", size, 1, 51, true);
}
void Blur::applyOperation(cv::Mat &src)
{
cv::blur(src, src, cv::Size(ksize->value, ksize->value), cv::Point(-1, -1), cv::BORDER_ISOLATED);
}
// Canny
std::string Canny::name = "Canny";
Canny::Canny(bool on, double th1, double th2, int size, bool g): ImageOperation(on)
{
double minThreshold1, maxThreshold1;
adjustMinMax(th1, 0.0, 300.0, minThreshold1, maxThreshold1);
threshold1 = new DoubleParameter("Threshold 1", th1, minThreshold1, maxThreshold1, 0.0, 1.0e6);
double minThreshold2, maxThreshold2;
adjustMinMax(th2, 0.0, 300.0, minThreshold2, maxThreshold2);
threshold2 = new DoubleParameter("Threshold 2", th2, minThreshold2, maxThreshold2, 0.0, 1.0e6);
apertureSize = new IntParameter("Aperture size", size, 3, 7, true);
L2gradient = new BoolParameter("L2 gradient", g);
}
void Canny::applyOperation(cv::Mat &src)
{
cv::Mat detectedEdges;
cv::Canny(src, detectedEdges, threshold1->value, threshold2->value, apertureSize->value, L2gradient->value);
cv::Mat dst;
src.copyTo(dst, detectedEdges);
src = dst.clone();
}
// Color quantization
std::string ColorQuantization::name = "Color quantization";
ColorQuantization::ColorQuantization(bool on, int nBGRLevels, int nHueLevels, int nLightLevels, int nSatLevels, int type): ImageOperation(on)
{
bgrLevels = new IntParameter("BGR Levels", nBGRLevels, 1, 255, false);
hueLevels = new IntParameter("Hue Levels", nHueLevels, 1, 179, false);
lightLevels = new IntParameter("Lightness Levels", nLightLevels, 1, 255, false);
satLevels = new IntParameter("Saturation Levels", nSatLevels, 1, 255, false);
std::vector<std::string> names = {"BGR", "HLS"};
std::vector<int> values = {0, 1};
colorSpace = new OptionsParameter<int>("Color-space", names, values, type);
}
void ColorQuantization::applyOperation(cv::Mat &src)
{
if (colorSpace->value == 0)
{
src.forEach<cv::Vec3b>([&](cv::Vec3b &pixel, const int*)
{
pixel[0] = static_cast<uchar>(roundf(pixel[0] * bgrLevels->value / 255.0) * 255.0 / bgrLevels->value);
pixel[1] = static_cast<uchar>(roundf(pixel[1] * bgrLevels->value / 255.0) * 255.0 / bgrLevels->value);
pixel[2] = static_cast<uchar>(roundf(pixel[2] * bgrLevels->value / 255.0) * 255.0 / bgrLevels->value);
});
}
else if (colorSpace->value == 1)
{
cv::Mat hls;
cv::cvtColor(src, hls, cv::COLOR_BGR2HLS);
hls.forEach<cv::Vec3b>([&](cv::Vec3b &pixel, const int*)
{
pixel[0] = static_cast<uchar>(roundf(pixel[0] * hueLevels->value / 179.0) * 179.0 / hueLevels->value);
pixel[1] = static_cast<uchar>(roundf(pixel[1] * lightLevels->value / 255.0) * 255.0 / lightLevels->value);
pixel[2] = static_cast<uchar>(roundf(pixel[2] * satLevels->value / 255.0) * 255.0 / satLevels->value);
});
cv::cvtColor(hls, src, cv::COLOR_HLS2BGR);
}
}
// Convert to
std::string ConvertTo::name = "Contrast/brightness";
ConvertTo::ConvertTo(bool on, double a, double b): ImageOperation(on)
{
double minAlpha, maxAlpha;
adjustMinMax(a, 0.0, 5.0, minAlpha, maxAlpha);
alpha = new DoubleParameter("Gain", a, minAlpha, maxAlpha, -1.0e6, 1.0e6);
double minBeta, maxBeta;
adjustMinMax(b, -100.0, 100.0, minBeta, maxBeta);
beta = new DoubleParameter("Bias", b, minBeta, maxBeta, -1.0e6, 1.0e6);
}
void ConvertTo::applyOperation(cv::Mat &src)
{
src.convertTo(src, -1, alpha->value, beta->value);
}
// Deblur filter
std::string DeblurFilter::name = "Deblur filter";
DeblurFilter::DeblurFilter(bool on, double r, double snr): ImageOperation(on)
{
double minRadius, maxRadius;
adjustMinMax(r, 0.0, 20.0, minRadius, maxRadius);
radius = new DoubleParameter("Radius", r, minRadius, maxRadius, 0.0, 1.0e6);
double minSNR, maxSNR;
adjustMinMax(snr, 0.001, 200.0, minSNR, maxSNR);
signalToNoiseRatio = new DoubleParameter("Signal to noise ratio", snr, minSNR, maxSNR, 1.0e-6, 1.0e6);
}
void DeblurFilter::computePSF(cv::Mat &outputImg, cv::Size filterSize)
{
cv::Mat h(filterSize, CV_32F, cv::Scalar(0));
cv::Point point(filterSize.width / 2, filterSize.height / 2);
cv::circle(h, point, radius->value, 255, -1, 8);
cv::Scalar summa = sum(h);
outputImg = h / summa[0];
}
void DeblurFilter::computeWnrFilter(const cv::Mat &input_h_PSF, cv::Mat &output_G, double nsr)
{
cv::Mat h_PSF_shifted;
fftShift(input_h_PSF, h_PSF_shifted);
cv::Mat planes[2] = { cv::Mat_<float>(h_PSF_shifted.clone()), cv::Mat::zeros(h_PSF_shifted.size(), CV_32F) };
cv::Mat complexI;
cv::merge(planes, 2, complexI);
cv::dft(complexI, complexI);
cv::split(complexI, planes);
cv::Mat denom;
cv::pow(abs(planes[0]), 2, denom);
denom += nsr;
cv::divide(planes[0], denom, output_G);
}
void DeblurFilter::fftShift(const cv::Mat &inputImg, cv::Mat &outputImg)
{
outputImg = inputImg.clone();
int cx = outputImg.cols / 2;
int cy = outputImg.rows / 2;
cv::Mat q0(outputImg, cv::Rect(0, 0, cx, cy));
cv::Mat q1(outputImg, cv::Rect(cx, 0, cx, cy));
cv::Mat q2(outputImg, cv::Rect(0, cy, cx, cy));
cv::Mat q3(outputImg, cv::Rect(cx, cy, cx, cy));
cv::Mat tmp;
q0.copyTo(tmp);
q3.copyTo(q0);
tmp.copyTo(q3);
q1.copyTo(tmp);
q2.copyTo(q1);
tmp.copyTo(q2);
}
void DeblurFilter::filter2DFreq(const cv::Mat &inputImg, cv::Mat &outputImg, const cv::Mat &H)
{
cv::Mat planes[2] = { cv::Mat_<float>(inputImg.clone()), cv::Mat::zeros(inputImg.size(), CV_32F) };
cv::Mat complexI;
cv::merge(planes, 2, complexI);
cv::dft(complexI, complexI, cv::DFT_SCALE);
cv::Mat planesH[2] = { cv::Mat_<float>(H.clone()), cv::Mat::zeros(H.size(), CV_32F) };
cv::Mat complexH;
cv::merge(planesH, 2, complexH);
cv::Mat complexIH;
cv::mulSpectrums(complexI, complexH, complexIH, 0);
cv::idft(complexIH, complexIH);
cv::split(complexIH, planes);
outputImg = planes[0];
}
void DeblurFilter::applyOperation(cv::Mat &src)
{
cv::Rect roi = cv::Rect(0, 0, src.cols & -2, src.rows & -2);
cv::Mat Hw, h;
computePSF(h, roi.size());
computeWnrFilter(h, Hw, 1.0 / signalToNoiseRatio->value);
cv::Mat srcChannels[3];
cv::split(src, srcChannels);
cv::Mat dstChannels[3];
filter2DFreq(srcChannels[0](roi), dstChannels[0], Hw);
filter2DFreq(srcChannels[1](roi), dstChannels[1], Hw);
filter2DFreq(srcChannels[2](roi), dstChannels[2], Hw);
cv::Mat dst;
cv::merge(dstChannels, 3, dst);
dst.convertTo(dst, CV_8UC3);
cv::normalize(dst, src, 0, 255, cv::NORM_MINMAX);
}
// Equalize histogram
std::string EqualizeHist::name = "Equalize histogram";
EqualizeHist::EqualizeHist(bool on): ImageOperation(on){}
void EqualizeHist::applyOperation(cv::Mat &src)
{
cv::cvtColor(src, src, cv::COLOR_BGR2YCrCb);
std::vector<cv::Mat> channels;
cv::split(src, channels);
cv::equalizeHist(channels[0], channels[0]);
cv::merge(channels, src);
cv::cvtColor(src, src, cv::COLOR_YCrCb2BGR);
}
// Filter 2D
std::string Filter2D::name = "Filter 2D";
Filter2D::Filter2D(bool on, std::vector<float> v): ImageOperation(on)
{
kernel = new KernelParameter("Kernel", v, -1000.0, 1000.0, true);
updateKernelMat();
}
void Filter2D::updateKernelMat()
{
kernelMat = cv::Mat(kernel->values).reshape(0, 3);
kernelMat.convertTo(kernelMat, CV_32F);
}
void Filter2D::applyOperation(cv::Mat &src)
{
cv::filter2D(src, src, -1, kernelMat, cv::Point(-1, -1), 0.0, cv::BORDER_ISOLATED);
}
// Gamma correction
std::string GammaCorrection::name = "Gamma correction";
GammaCorrection::GammaCorrection(bool on, double g): ImageOperation(on)
{
double minGamma, maxGamma;
adjustMinMax(g, 0.0, 10.0, minGamma, maxGamma);
gamma = new DoubleParameter("Gamma", g, minGamma, maxGamma, 0.0, 1.0e6);
}
void GammaCorrection::applyOperation(cv::Mat &src)
{
cv::Mat lookUpTable = cv::Mat(1, 256, CV_8U);
uchar *p = lookUpTable.ptr();
for (int i = 0; i < 256; i++)
p[i] = cv::saturate_cast<uchar>(cv::pow(i / 255.0, gamma->value) * 255.0);
cv::LUT(src, lookUpTable, src);
}
// Gaussian blur
std::string GaussianBlur::name = "Blur: Gaussian";
GaussianBlur::GaussianBlur(bool on, int k, double s): ImageOperation(on)
{
ksize = new IntParameter("Kernel size", k, 0, 51, true);
double minSigma, maxSigma;
adjustMinMax(s, 0.001, 5.0, minSigma, maxSigma);
sigma = new DoubleParameter("Sigma", s, minSigma, maxSigma, 1.0e-6, 1.0e6);
}
void GaussianBlur::applyOperation(cv::Mat &src)
{
cv::GaussianBlur(src, src, cv::Size(ksize->value, ksize->value), sigma->value, sigma->value, cv::BORDER_ISOLATED);
}
// Invert colors
std::string InvertColors::name = "Invert colors";
InvertColors::InvertColors(bool on): ImageOperation(on){}
void InvertColors::applyOperation(cv::Mat &src)
{
cv::bitwise_not(src, src);
}
// Laplacian
std::string Laplacian::name = "Laplacian";
Laplacian::Laplacian(bool on, int k): ImageOperation(on)
{
ksize = new IntParameter("Kernel size", k, 1, 51, true);
}
void Laplacian::applyOperation(cv::Mat &src)
{
cv::Mat channels[3];
cv::split(src, channels);
cv::Mat lap[3];
cv::Laplacian(channels[0], lap[0], CV_16S, ksize->value, 1.0, 0.0, cv::BORDER_ISOLATED);
cv::Laplacian(channels[1], lap[1], CV_16S, ksize->value, 1.0, 0.0, cv::BORDER_ISOLATED);
cv::Laplacian(channels[2], lap[2], CV_16S, ksize->value, 1.0, 0.0, cv::BORDER_ISOLATED);
cv::merge(lap, 3, src);
cv::convertScaleAbs(src, src);
}
// Median blur
std::string MedianBlur::name = "Blur: median";
MedianBlur::MedianBlur(bool on, int size): ImageOperation(on)
{
ksize = new IntParameter("Kernel size", size, 3, 51, true);
}
void MedianBlur::applyOperation(cv::Mat &src)
{
cv::medianBlur(src, src, ksize->value);
}
// Mix BGR channels
std::string MixBGRChannels::name = "Mix BGR channels";
MixBGRChannels::MixBGRChannels(bool on, std::vector<float> v): ImageOperation(on)
{
kernel = new KernelParameter("Mix matrix", v, -10.0, 10.0, false);
updateKernelMat();
}
void MixBGRChannels::updateKernelMat()
{
kernelMat = cv::Mat(kernel->values).reshape(0, 3);
kernelMat.convertTo(kernelMat, CV_32F);
}
void MixBGRChannels::applyOperation(cv::Mat &src)
{
src.forEach<cv::Vec3b>([&](cv::Vec3b &pixel, const int*)
{
cv::Vec3b tmp;
tmp[0] = cv::saturate_cast<uchar>(kernelMat.at<float>(0, 0) * pixel[0] + kernelMat.at<float>(0, 1) * pixel[1] + kernelMat.at<float>(0, 2) * pixel[2]);
tmp[1] = cv::saturate_cast<uchar>(kernelMat.at<float>(1, 0) * pixel[0] + kernelMat.at<float>(1, 1) * pixel[1] + kernelMat.at<float>(1, 2) * pixel[2]);
tmp[2] = cv::saturate_cast<uchar>(kernelMat.at<float>(2, 0) * pixel[0] + kernelMat.at<float>(2, 1) * pixel[1] + kernelMat.at<float>(2, 2) * pixel[2]);
pixel = tmp;
});
}
// Morphological operations
std::string MorphologyEx::name = "Morphology operation";
MorphologyEx::MorphologyEx(bool on, int k, int its, cv::MorphTypes type, cv::MorphShapes shape): ImageOperation(on)
{
ksize = new IntParameter("Kernel size", k, 3, 51, true);
iterations = new IntParameter("Iterations", its, 1, 100, false);
std::vector<std::string> typeValueNames = {"Erode", "Dilate", "Opening", "Closing", "Gradient", "Top hat", "Black hat"};
std::vector<cv::MorphTypes> typeValues = {cv::MORPH_ERODE, cv::MORPH_DILATE, cv::MORPH_OPEN, cv::MORPH_CLOSE, cv::MORPH_GRADIENT, cv::MORPH_TOPHAT, cv::MORPH_BLACKHAT};
morphType = new OptionsParameter<cv::MorphTypes>("Type", typeValueNames, typeValues, type);
std::vector<std::string> shapeValueNames = {"Rectangle", "Cross", "Ellipse"};
std::vector<cv::MorphShapes> shapeValues = {cv::MORPH_RECT, cv::MORPH_CROSS, cv::MORPH_ELLIPSE};
morphShape = new OptionsParameter<cv::MorphShapes>("Shape", shapeValueNames, shapeValues, shape);
}
void MorphologyEx::applyOperation(cv::Mat &src)
{
cv::Mat element = cv::getStructuringElement(morphShape->value, cv::Size(ksize->value, ksize->value));
cv::morphologyEx(src, src, morphType->value, element, cv::Point(-1, -1), iterations->value, cv::BORDER_ISOLATED);
}
// Pixelate
std::string Pixelate::name = "Pixelate";
Pixelate::Pixelate(bool on, int size): ImageOperation(on)
{
pixelSize = new IntParameter("Pixel size", size, 1, 50, false);
}
void Pixelate::applyOperation(cv::Mat &src)
{
cv::Mat dst(src.rows, src.cols, src.type());
cv::Rect square;
for (int row = 0; row < src.rows; row += pixelSize->value)
{
square.y = row;
square.height = row + pixelSize->value < src.rows ? pixelSize->value : src.rows - row;
for (int col = 0; col < src.cols; col += pixelSize->value)
{
square.x = col;
square.width = col + pixelSize->value < src.cols ? pixelSize->value : src.cols - col;
cv::Scalar meanColor = cv::mean(cv::Mat(src, square));
cv::rectangle(dst, square, meanColor, cv::FILLED);
}
}
src = dst.clone();
}
// Radial remap
std::string RadialRemap::name = "Radial remap";
RadialRemap::RadialRemap(bool on, double a, int rf, cv::InterpolationFlags f): ImageOperation(on)
{
double minAmplitude, maxAmplitude;
adjustMinMax(a, -5.0, 5.0, minAmplitude, maxAmplitude);
amplitude = new DoubleParameter("Amplitude", a, minAmplitude, maxAmplitude, -1.0e6, 1.0e6);
std::vector<std::string> radialFunctionValueNames = {"Linear inc.", "Linear dec.", "Cosine inc.", "Cosine dec."};
std::vector<int> radialFuncionValues = {0, 1, 2, 3};
radialFunction = new OptionsParameter<int>("Function", radialFunctionValueNames, radialFuncionValues, rf);
std::vector<std::string> flagValueNames = {"Nearest neighbor", "Bilinear", "Bicubic", "Area", "Lanczos 8x8"};
std::vector<cv::InterpolationFlags> flagValues = {cv::INTER_NEAREST, cv::INTER_LINEAR, cv::INTER_CUBIC, cv::INTER_AREA, cv::INTER_LANCZOS4};
flag = new OptionsParameter<cv::InterpolationFlags>("Interpolation", flagValueNames, flagValues, f);
oldAmplitude = amplitude->value;
oldRadialFunction = radialFunction->value;
updateMappingMatrices();
}
void RadialRemap::updateMappingMatrices()
{
mapX = cv::Mat(size, CV_32FC1);
mapY = cv::Mat(size, CV_32FC1);
float rMax = 0.5 * mapX.cols;
float centerX = 0.5 * mapX.cols;
float centerY = 0.5 * mapX.cols;
float pi = 3.14159265359;
if (radialFunction->value == 0) // Linear inc.
{
float f = amplitude->value / rMax;
for (int i = 0; i < mapX.rows; i++)
{
for (int j = 0; j < mapX.cols; j++)
{
float x = j - centerX;
float y = i - centerY;
mapX.at<float>(i, j) = centerX + x * (1.0 + f);
mapY.at<float>(i, j) = centerY + y * (1.0 + f);
}
}
}
else if (radialFunction->value == 1) // Linear dec.
{
for (int i = 0; i < mapX.rows; i++)
{
for (int j = 0; j < mapX.cols; j++)
{
float x = j - centerX;
float y = i - centerY;
float r = sqrtf(x * x + y * y);
float f = 0.0;
if (r != 0.0) f = amplitude->value * (1.0 / r - 1.0 / rMax);
mapX.at<float>(i, j) = centerX + x * (1.0 + f);
mapY.at<float>(i, j) = centerY + y * (1.0 + f);
}
}
}
else if (radialFunction->value == 2) // Cosine inc.
{
for (int i = 0; i < mapX.rows; i++)
{
for (int j = 0; j < mapX.cols; j++)
{
float x = j - centerX;
float y = i - centerY;
float r = sqrtf(x * x + y * y);
float f = 0.0;
if (r != 0.0) f = 0.5* amplitude->value * (1.0 - cosf(pi * r / rMax)) / r;
mapX.at<float>(i, j) = centerX + x * (1.0 + f);
mapY.at<float>(i, j) = centerY + y * (1.0 + f);
}
}
}
else if (radialFunction->value == 3) // Cosine dec.
{
for (int i = 0; i < mapX.rows; i++)
{
for (int j = 0; j < mapX.cols; j++)
{
float x = j - centerX;
float y = i - centerY;
float r = sqrtf(x * x + y * y);
float f = 0.0;
if (r != 0.0) f = 0.5* amplitude->value * (1.0 + cosf(pi * r / rMax)) / r;
mapX.at<float>(i, j) = centerX + x * (1.0 + f);
mapY.at<float>(i, j) = centerY + y * (1.0 + f);
}
}
}
}
void RadialRemap::applyOperation(cv::Mat &src)
{
if (src.size() != size)
{
size = src.size();
updateMappingMatrices();
}
if (amplitude->value != oldAmplitude)
{
oldAmplitude = amplitude->value;
updateMappingMatrices();
}
if (radialFunction->value != oldRadialFunction)
{
oldRadialFunction = radialFunction->value;
updateMappingMatrices();
}
cv::remap(src, src, mapX, mapY, flag->value, cv::BORDER_TRANSPARENT);
}
// Rotation
std::string Rotation::name = "Rotation/scaling";
Rotation::Rotation(bool on, double a, double s, cv::InterpolationFlags f): ImageOperation(on)
{
double minAngle, maxAngle;
adjustMinMax(a, -360, 360, minAngle, maxAngle);
angle = new DoubleParameter("Angle", a, minAngle, maxAngle, -1.0e6, 1.0e6);
double minScale, maxScale;
adjustMinMax(s, 0.0, 2.0, minScale, maxScale);
scale = new DoubleParameter("Scale", s, minScale, maxScale, 0.0, 1.0e6);
std::vector<std::string> valueNames = {"Nearest neighbor", "Bilinear", "Bicubic", "Area", "Lanczos 8x8"};
std::vector<cv::InterpolationFlags> values = {cv::INTER_NEAREST, cv::INTER_LINEAR, cv::INTER_CUBIC, cv::INTER_AREA, cv::INTER_LANCZOS4};
flag = new OptionsParameter<cv::InterpolationFlags>("Interpolation", valueNames, values, f);
}
void Rotation::applyOperation(cv::Mat &src)
{
cv::Point center = cv::Point(src.cols / 2, src.rows / 2);
cv::Mat rotationMat = cv::getRotationMatrix2D(center, angle->value, scale->value);
cv::warpAffine(src, src, rotationMat, src.size(), flag->value);
}
// Saturate
std::string Saturate::name = "Saturate";
Saturate::Saturate(bool on, double g, double b): ImageOperation(on)
{
double minGain, maxGain;
adjustMinMax(g, 0.0, 5.0, minGain, maxGain);
gain = new DoubleParameter("Gain", g, minGain, maxGain, -1.0e6, 1.0e6);
double minBias, maxBias;
adjustMinMax(b, -100.0, 100.0, minBias, maxBias);
bias = new DoubleParameter("Bias", b, minBias, maxBias, -1.0e6, 1.0e6);
}
void Saturate::applyOperation(cv::Mat &src)
{
cv::Mat hsv;
cv::cvtColor(src, hsv, cv::COLOR_BGR2HSV);
cv::Mat channels[3];
cv::split(hsv, channels);
channels[1].convertTo(channels[1], -1, gain->value, bias->value);
cv::merge(channels, 3, hsv);
cv::cvtColor(hsv, src, cv::COLOR_HSV2BGR);
}
// Sharpen
std::string Sharpen::name = "Sharpen";
Sharpen::Sharpen(bool on, double s, double t, double a): ImageOperation(on)
{
double minSigma, maxSigma;
adjustMinMax(s, 0.001, 10.0, minSigma, maxSigma);
sigma = new DoubleParameter("Sigma", s, minSigma, maxSigma, 1.0e-6, 1.0e6);
double minThreshold, maxThreshold;
adjustMinMax(t, 0.0, 100.0, minThreshold, maxThreshold);
threshold = new DoubleParameter("Threshold", t, minThreshold, maxThreshold, 0.0, 1.0e6);
double minAmount, maxAmount;
adjustMinMax(a, 0.0, 10.0, minAmount, maxAmount);
amount = new DoubleParameter("Amount", a, minAmount, maxAmount, -1.0e6, 1.0e6);
}
void Sharpen::applyOperation(cv::Mat &src)
{
cv::Mat blurred;
cv::GaussianBlur(src, blurred, cv::Size(), sigma->value, sigma->value, cv::BORDER_ISOLATED);
cv::Mat lowContrastMask = abs(src - blurred) < threshold->value;
cv::Mat sharpened = src * (1 + amount->value) + blurred * (-amount->value);
src.copyTo(sharpened, lowContrastMask);
src = sharpened.clone();
}
// Shift hue
std::string ShiftHue::name = "Shift hue";
ShiftHue::ShiftHue(bool on, int d): ImageOperation(on)
{
delta = new IntParameter("Delta", d, -180, 180, false);
}
void ShiftHue::applyOperation(cv::Mat &src)
{
cv::Mat hsv;
cv::cvtColor(src, hsv, cv::COLOR_BGR2HSV);
hsv.forEach<cv::Vec3b>([&](cv::Vec3b &pixel, const int*){ pixel[0] = (pixel[0] + delta->value) % 180; });
cv::cvtColor(hsv, src, cv::COLOR_HSV2BGR);
}
// Swap channels
std::string SwapChannels::name = "Swap channels";
SwapChannels::SwapChannels(bool on, int b, int g, int r): ImageOperation(on)
{
std::vector<std::string> valueNames = {"Blue", "Green", "Red"};
std::vector<int> values = {0, 1, 2};
blue = new OptionsParameter<int>("Blue", valueNames, values, b);
green = new OptionsParameter<int>("Green", valueNames, values, g);
red = new OptionsParameter<int>("Red", valueNames, values, r);
}
void SwapChannels::applyOperation(cv::Mat &src)
{
int fromTo[] = {0, blue->value, 1, green->value, 2, red->value};
cv::mixChannels(&src, 1, &src, 1, fromTo, 3);
}