-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_operations.h
2991 lines (2784 loc) · 123 KB
/
image_operations.h
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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Developed by Jimmy Hu */
#ifndef TINYDIP_IMAGE_OPERATIONS_H
#define TINYDIP_IMAGE_OPERATIONS_H
#include <concepts>
#include <execution>
#include <fstream>
#include <numbers>
#include <string>
#include "base_types.h"
#include "basic_functions.h"
#include "image.h"
#ifdef USE_OPENCV
#include <opencv2/opencv.hpp>
#endif
namespace TinyDIP
{
template<typename T>
concept image_element_standard_floating_point_type =
std::same_as<double, T>
or std::same_as<float, T>
or std::same_as<long double, T>
;
// all_of template function implementation
template<typename ElementT, class UnaryPredicate>
constexpr auto all_of(const Image<ElementT>& input, UnaryPredicate p)
{
return std::ranges::all_of(std::ranges::begin(input.getImageData()), std::ranges::end(input.getImageData()), p);
}
template<typename ElementT>
constexpr bool is_width_same(const Image<ElementT>& x, const Image<ElementT>& y)
{
return x.getWidth() == y.getWidth();
}
template<typename ElementT>
constexpr bool is_width_same(const Image<ElementT>& x, const Image<ElementT>& y, const Image<ElementT>& z)
{
return is_width_same(x, y) && is_width_same(y, z);
}
template<typename ElementT>
constexpr bool is_height_same(const Image<ElementT>& x, const Image<ElementT>& y)
{
return x.getHeight() == y.getHeight();
}
template<typename ElementT>
constexpr bool is_height_same(const Image<ElementT>& x, const Image<ElementT>& y, const Image<ElementT>& z)
{
return is_height_same(x, y) && is_height_same(y, z);
}
template<typename ElementT>
constexpr bool is_size_same(const Image<ElementT>& x, const Image<ElementT>& y)
{
return is_width_same(x, y) && is_height_same(x, y);
}
template<typename ElementT>
constexpr bool is_size_same(const Image<ElementT>& x, const Image<ElementT>& y, const Image<ElementT>& z)
{
return is_size_same(x, y) && is_size_same(y, z);
}
template<typename ElementT>
constexpr void assert_width_same(const Image<ElementT>& x, const Image<ElementT>& y)
{
assert(is_width_same(x, y));
}
template<typename ElementT>
constexpr void assert_width_same(const Image<ElementT>& x, const Image<ElementT>& y, const Image<ElementT>& z)
{
assert(is_width_same(x, y, z));
}
template<typename ElementT>
constexpr void assert_height_same(const Image<ElementT>& x, const Image<ElementT>& y)
{
assert(is_height_same(x, y));
}
template<typename ElementT>
constexpr void assert_height_same(const Image<ElementT>& x, const Image<ElementT>& y, const Image<ElementT>& z)
{
assert(is_height_same(x, y, z));
}
template<typename ElementT>
constexpr void assert_size_same(const Image<ElementT>& x, const Image<ElementT>& y)
{
assert_width_same(x, y);
assert_height_same(x, y);
}
template<typename ElementT>
constexpr void assert_size_same(const Image<ElementT>& x, const Image<ElementT>& y, const Image<ElementT>& z)
{
assert_size_same(x, y);
assert_size_same(y, z);
}
template<typename ElementT>
constexpr void check_width_same(const Image<ElementT>& x, const Image<ElementT>& y)
{
if (!is_width_same(x, y))
throw std::runtime_error("Width mismatched!");
}
template<typename ElementT>
constexpr void check_height_same(const Image<ElementT>& x, const Image<ElementT>& y)
{
if (!is_height_same(x, y))
throw std::runtime_error("Height mismatched!");
}
// check_size_same template function implementation
template<typename ElementT>
constexpr void check_size_same(const Image<ElementT>& x, const Image<ElementT>& y)
{
if(x.getSize() != y.getSize())
throw std::runtime_error("Size mismatched!");
}
// zeros template function implementation
template<typename ElementT, std::same_as<std::size_t>... Sizes>
constexpr static auto zeros(Sizes... sizes)
{
auto output = Image<ElementT>(sizes...);
return output;
}
// ones template function implementation
template<typename ElementT, std::same_as<std::size_t>... Sizes>
constexpr static auto ones(Sizes... sizes)
{
auto output = zeros<ElementT>(sizes...);
output.setAllValue(1);
return output;
}
// rand template function implementation
template<image_element_standard_floating_point_type ElementT = double, typename Urbg, std::same_as<std::size_t>... Sizes>
requires std::uniform_random_bit_generator<std::remove_reference_t<Urbg>>
constexpr static auto rand(Urbg&& mersenne_engine, Sizes... sizes)
{
if constexpr (sizeof...(Sizes) == 1)
{
return rand(mersenne_engine, sizes..., sizes...);
}
else
{
std::vector<ElementT> image_data((... * sizes));
// Reference: https://stackoverflow.com/a/23143753/6667035
// Reference: https://codereview.stackexchange.com/a/294739/231235
std::uniform_real_distribution<ElementT> dist {0, 1};
auto gen = [&](){
return dist(mersenne_engine);
};
std::generate(std::ranges::begin(image_data), std::ranges::end(image_data), gen);
auto output = Image<ElementT>(image_data, sizes...);
return output;
}
}
// rand template function implementation
template<image_element_standard_floating_point_type ElementT = double, std::same_as<std::size_t>... Size>
inline auto rand(Size... size)
{
return rand<ElementT>(std::mt19937{std::random_device{}()}, size...);
}
// conv2 template function implementation
template<typename ElementT>
requires(std::floating_point<ElementT> || std::integral<ElementT> || is_complex<ElementT>::value)
constexpr static auto conv2(const Image<ElementT>& x, const Image<ElementT>& y, bool is_size_same = false)
{
auto output = Image<ElementT>(x.getWidth() + y.getWidth() - 1, x.getHeight() + y.getHeight() - 1);
for (std::size_t y1 = 0; y1 < x.getHeight(); ++y1) {
auto* x_row = &(x.at(0, y1));
for (std::size_t y2 = 0; y2 < y.getHeight(); ++y2) {
auto* y_row = &(y.at(0, y2));
auto* out_row = &(output.at(0, y1 + y2));
for (std::size_t x1 = 0; x1 < x.getWidth(); ++x1) {
for (std::size_t x2 = 0; x2 < y.getWidth(); ++x2) {
out_row[x1 + x2] += x_row[x1] * y_row[x2];
}
}
}
}
if(is_size_same)
{
output = subimage(output, x.getWidth(), x.getHeight(), static_cast<double>(output.getWidth()) / 2.0, static_cast<double>(output.getHeight()) / 2.0);
}
return output;
}
// conv2 template function implementation
template<typename ElementT, typename ElementT2>
requires (((std::same_as<ElementT, RGB>) || (std::same_as<ElementT, RGB_DOUBLE>) || (std::same_as<ElementT, HSV>)) &&
(std::floating_point<ElementT2> || std::integral<ElementT2> || is_complex<ElementT2>::value))
constexpr static auto conv2(const Image<ElementT>& input1, const Image<ElementT2>& input2, bool is_size_same = false)
{
return apply_each(input1, [&](auto&& planes) { return conv2(planes, input2, is_size_same); });
}
namespace impl {
// convolution_detail template function implementation
template<class ExecutionPolicy, typename ImageT, typename KernelT,
typename F = std::multiplies<std::common_type_t<ImageT, KernelT>>>
requires((std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>)
&& std::regular_invocable<F, ImageT, KernelT>)
constexpr static void convolution_detail(
ExecutionPolicy&& execution_policy,
const Image<ImageT>& image,
const Image<KernelT>& kernel,
Image<ImageT>& output,
std::size_t level = 0,
std::size_t output_index = 0,
std::size_t index2 = 0,
std::size_t index3 = 0,
F f = {})
{
#pragma omp parallel for collapse(2)
for (std::size_t i = 0; i < kernel.getSize(level); ++i)
{
for (std::size_t j = 0; j < image.getSize(level); ++j)
{
output_index += (i + j) * output.getStride(level);
index2 += j * image.getStride(level);
index3 += i * kernel.getStride(level);
if(level == 0)
{
output.set(output_index) =
output.get(output_index) +
std::invoke(f, image.get(index2), kernel.get(index3));
}
else
{
convolution_detail(execution_policy, image, kernel, output, level - 1, output_index, index2, index3, f);
}
output_index -= (i + j) * output.getStride(level);
index2 -= j * image.getStride(level);
index3 -= i * kernel.getStride(level);
}
}
}
}
// convolution template function implementation
template<typename ElementT>
requires(std::floating_point<ElementT> || std::integral<ElementT> || is_complex<ElementT>::value)
constexpr static auto convolution(const Image<ElementT>& image, const Image<ElementT>& kernel)
{
return convolution(std::execution::seq, image, kernel);
}
// convolution template function implementation (with Execution Policy)
template<class ExecutionPolicy, typename ElementT>
requires((std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>) &&
(std::floating_point<ElementT> || std::integral<ElementT> || is_complex<ElementT>::value))
constexpr static auto convolution(ExecutionPolicy&& execution_policy, const Image<ElementT>& image, const Image<ElementT>& kernel)
{
/* ranges::to support list: https://stackoverflow.com/a/74662256/6667035
auto output_size =
std::views::zip_transform(
[](auto lhs, auto rhs){ return lhs + rhs - 1; },
image.getSize(),
kernel.getSize()
) | std::ranges::to<std::vector>();
*/
std::vector<std::size_t> output_size;
std::ranges::transform(
image.getSize(),
kernel.getSize(),
std::back_inserter(output_size),
[](auto lhs, auto rhs){ return lhs + rhs - 1; }
);
Image<ElementT> output(output_size);
impl::convolution_detail(execution_policy, image, kernel, output, image.getSize().size() - 1);
return output;
}
// two dimensional discrete fourier transform template function implementation
// https://codereview.stackexchange.com/q/292276/231235
template<typename ElementT, typename ComplexType = std::complex<long double>>
requires(std::floating_point<ElementT> || std::integral<ElementT>)
constexpr static auto dft2(const Image<ElementT>& input)
{
Image<ComplexType> output(input.getWidth(), input.getHeight());
auto normalization_factor = std::sqrt(1.0 / static_cast<long double>(input.getWidth() * input.getHeight()));
for (std::size_t y = 0; y < input.getHeight(); ++y)
{
for (std::size_t x = 0; x < input.getWidth(); ++x)
{
long double sum_real = 0.0;
long double sum_imag = 0.0;
for (std::size_t n = 0; n < input.getHeight(); ++n)
{
for (std::size_t m = 0; m < input.getWidth(); ++m)
{
sum_real += input.at_without_boundary_check(m, n) *
std::cos(2 * std::numbers::pi_v<long double> * (x * m / static_cast<long double>(input.getWidth()) + y * n / static_cast<long double>(input.getHeight())));
sum_imag += -input.at_without_boundary_check(m, n) *
std::sin(2 * std::numbers::pi_v<long double> * (x * m / static_cast<long double>(input.getWidth()) + y * n / static_cast<long double>(input.getHeight())));
}
}
output.at_without_boundary_check(x, y).real(normalization_factor * sum_real);
output.at_without_boundary_check(x, y).imag(normalization_factor * sum_imag);
}
}
return output;
}
// two dimensional inverse discrete fourier transform template function implementation
template<typename ElementT, typename ComplexType = std::complex<long double>>
constexpr auto idft2(const Image<ElementT>& input)
{
Image<ComplexType> output(input.getWidth(), input.getHeight());
auto normalization_factor = std::sqrt(1.0 / static_cast<long double>(input.getWidth() * input.getHeight()));
for (std::size_t y = 0; y < input.getHeight(); ++y)
{
for (std::size_t x = 0; x < input.getWidth(); ++x)
{
std::complex<long double> sum = 0.0;
std::complex<long double> i (0.0,1.0);
for (std::size_t n = 0; n < input.getHeight(); ++n)
{
for (std::size_t m = 0; m < input.getWidth(); ++m)
{
sum += input.at_without_boundary_check(m, n) *
(std::cos(2 * std::numbers::pi_v<long double> * (x * m / static_cast<long double>(input.getWidth()) + y * n / static_cast<long double>(input.getHeight()))) +
i * std::sin(2 * std::numbers::pi_v<long double> * (x * m / static_cast<long double>(input.getWidth()) + y * n / static_cast<long double>(input.getHeight()))));
}
}
output.at_without_boundary_check(x, y) = normalization_factor * sum;
}
}
return output;
}
#ifdef USE_OPENCV
// to_cv_mat function implementation
constexpr auto to_cv_mat(const Image<RGB>& input)
{
cv::Mat output = cv::Mat::zeros(cv::Size(input.getWidth(), input.getHeight()), CV_8UC3);
#pragma omp parallel for collapse(2)
for (int y = 0; y < output.rows; ++y)
{
for (int x = 0; x < output.cols; ++x)
{
output.at<cv::Vec3b>(output.rows - y - 1, x)[0] = input.at(x, y).channels[2];
output.at<cv::Vec3b>(output.rows - y - 1, x)[1] = input.at(x, y).channels[1];
output.at<cv::Vec3b>(output.rows - y - 1, x)[2] = input.at(x, y).channels[0];
}
}
return output;
}
// to_color_image function implementation
constexpr auto to_color_image(const cv::Mat input)
{
auto output = Image<RGB>(input.cols, input.rows);
#pragma omp parallel for collapse(2)
for (int y = 0; y < input.rows; ++y)
{
for (int x = 0; x < input.cols; ++x)
{
output.at(x, y).channels[0] = input.at<cv::Vec3b>(input.rows - y - 1, x)[2];
output.at(x, y).channels[1] = input.at<cv::Vec3b>(input.rows - y - 1, x)[1];
output.at(x, y).channels[2] = input.at<cv::Vec3b>(input.rows - y - 1, x)[0];
}
}
return output;
}
#endif
// rgb2hsv function implementation
static auto rgb2hsv(RGB input)
{
HSV output{};
std::uint8_t Red = input.channels[0], Green = input.channels[1], Blue = input.channels[2];
std::vector<std::uint8_t> v{ Red, Green, Blue };
std::ranges::sort(v);
std::uint8_t Max = v[2], Mid = v[1], Min = v[0];
auto H1 = std::acos(0.5 * ((Red - Green) + (Red - Blue)) /
std::sqrt(((std::pow((Red - Green), 2.0)) +
(Red - Blue) * (Green - Blue)))) * (180.0 / std::numbers::pi);
if (Max == Min)
{
output.channels[0] = 0.0;
}
else if (Blue <= Green)
{
output.channels[0] = H1;
}
else
{
output.channels[0] = 360.0 - H1;
}
if (Max == 0)
{
output.channels[1] = 0.0;
}
else
{
output.channels[1] = 1.0 - (static_cast<double>(Min) / static_cast<double>(Max));
}
output.channels[2] = Max;
return output;
}
// rgb2hsv function implementation
static auto rgb2hsv(RGB_DOUBLE input)
{
RGB rgb{static_cast<std::uint8_t>(input.channels[0]),
static_cast<std::uint8_t>(input.channels[1]),
static_cast<std::uint8_t>(input.channels[2])};
return rgb2hsv(rgb);
}
// hsv2rgb function implementation
static auto hsv2rgb(HSV input)
{
RGB output{};
long double H = input.channels[0], S = input.channels[1], Max = input.channels[2];
std::uint8_t hi = static_cast<std::uint8_t>(floor(H / 60.0));
long double f = (H / 60.0) - hi;
long double Min, q, t;
Min = Max * (1.0 - S);
q = Max * (1.0 - f * S);
t = Max * (1.0 - (1.0 - f) * S);
if (hi == 0)
{
output.channels[0] = static_cast<std::uint8_t>(Max);
output.channels[1] = static_cast<std::uint8_t>(t);
output.channels[2] = static_cast<std::uint8_t>(Min);
}
else if (hi == 1)
{
output.channels[0] = static_cast<std::uint8_t>(q);
output.channels[1] = static_cast<std::uint8_t>(Max);
output.channels[2] = static_cast<std::uint8_t>(Min);
}
else if (hi == 2)
{
output.channels[0] = static_cast<std::uint8_t>(Min);
output.channels[1] = static_cast<std::uint8_t>(Max);
output.channels[2] = static_cast<std::uint8_t>(t);
}
else if (hi == 3)
{
output.channels[0] = static_cast<std::uint8_t>(Min);
output.channels[1] = static_cast<std::uint8_t>(q);
output.channels[2] = static_cast<std::uint8_t>(Max);
}
else if (hi == 4)
{
output.channels[0] = static_cast<std::uint8_t>(t);
output.channels[1] = static_cast<std::uint8_t>(Min);
output.channels[2] = static_cast<std::uint8_t>(Max);
}
else if (hi == 5)
{
output.channels[0] = static_cast<std::uint8_t>(Max);
output.channels[1] = static_cast<std::uint8_t>(Min);
output.channels[2] = static_cast<std::uint8_t>(q);
}
return output;
}
// Grayscale2RGB function implementation
// Grayscale2RGB function returns RGB pixel which represents GrayScale input in hue color scale.
static auto Grayscale2RGB(GrayScale input)
{
HSV hsv;
hsv.channels[0] = static_cast<double>(input) / 256.0 * 360;
hsv.channels[1] = 1.0;
hsv.channels[2] = 255.0;
return hsv2rgb(hsv);
}
// Grayscale2RGB function implementation
static auto Grayscale2RGB(Image<GrayScale> input)
{
auto input_data = input.getImageData();
auto output_data = TinyDIP::recursive_transform([](auto&& input) { return Grayscale2RGB(input); }, input_data);
Image<RGB> output(output_data, input.getSize());
return output;
}
// constructRGB template function implementation
template<typename OutputT = RGB>
constexpr static auto constructRGB(Image<GrayScale> r, Image<GrayScale> g, Image<GrayScale> b)
{
check_size_same(r, g);
check_size_same(g, b);
auto image_data_r = r.getImageData();
auto image_data_g = g.getImageData();
auto image_data_b = b.getImageData();
std::vector<OutputT> new_data;
new_data.resize(r.count());
#pragma omp parallel for
for (std::size_t index = 0; index < r.count(); ++index)
{
OutputT rgb { image_data_r[index],
image_data_g[index],
image_data_b[index]};
new_data[index] = rgb;
}
Image<OutputT> output(new_data, r.getSize());
return output;
}
// constructRGBDOUBLE template function implementation
template<typename OutputT = RGB_DOUBLE>
constexpr static auto constructRGBDOUBLE(Image<double> r, Image<double> g, Image<double> b)
{
check_size_same(r, g);
check_size_same(g, b);
auto image_data_r = r.getImageData();
auto image_data_g = g.getImageData();
auto image_data_b = b.getImageData();
std::vector<OutputT> new_data;
new_data.resize(r.count());
#pragma omp parallel for
for (std::size_t index = 0; index < r.count(); ++index)
{
OutputT rgb_double { image_data_r[index],
image_data_g[index],
image_data_b[index]};
new_data[index] = rgb_double;
}
Image<OutputT> output(new_data, r.getSize());
return output;
}
// constructHSV template function implementation
template<typename OutputT = HSV>
constexpr static auto constructHSV(Image<double> h, Image<double> s, Image<double> v)
{
check_size_same(h, s);
check_size_same(s, v);
auto image_data_h = h.getImageData();
auto image_data_s = s.getImageData();
auto image_data_v = v.getImageData();
std::vector<OutputT> new_data;
new_data.resize(h.count());
#pragma omp parallel for
for (std::size_t index = 0; index < h.count(); ++index)
{
OutputT hsv { image_data_h[index],
image_data_s[index],
image_data_v[index]};
new_data[index] = hsv;
}
Image<OutputT> output(new_data, h.getSize());
return output;
}
// convert_image template function implementation
// Reference: https://codereview.stackexchange.com/a/292847/231235
template<typename DstT, typename SrcT>
requires(std::same_as<DstT, RGB_DOUBLE> or std::same_as<DstT, HSV>)
constexpr static auto convert_image(Image<SrcT> input)
{
auto image_data = input.getImageData();
std::vector<DstT> new_data;
new_data.resize(input.count());
#pragma omp parallel for
for (std::size_t index = 0; index < input.count(); ++index)
{
DstT dst { static_cast<double>(image_data[index].channels[0]),
static_cast<double>(image_data[index].channels[1]),
static_cast<double>(image_data[index].channels[2])};
new_data[index] = dst;
}
Image<DstT> output(new_data, input.getSize());
return output;
}
// convert_image template function implementation
// Reference: https://codereview.stackexchange.com/a/292847/231235
template<typename DstT, typename SrcT>
requires(std::same_as<DstT, RGB>)
constexpr static auto convert_image(Image<SrcT> input)
{
auto image_data = input.getImageData();
std::vector<DstT> new_data;
new_data.resize(input.count());
#pragma omp parallel for
for (std::size_t index = 0; index < input.count(); ++index)
{
DstT dst { static_cast<GrayScale>(image_data[index].channels[0]),
static_cast<GrayScale>(image_data[index].channels[1]),
static_cast<GrayScale>(image_data[index].channels[2])};
new_data[index] = dst;
}
Image<DstT> output(new_data, input.getSize());
return output;
}
// getPlane template function implementation
template<class OutputT = unsigned char>
constexpr static auto getPlane(Image<RGB> input, std::size_t index)
{
auto input_data = input.getImageData();
std::vector<OutputT> output_data;
output_data.resize(input.count());
#pragma omp parallel for
for (std::size_t i = 0; i < input.count(); ++i)
{
output_data[i] = input_data[i].channels[index];
}
auto output = Image<OutputT>(output_data, input.getSize());
return output;
}
// getPlane template function implementation
template<class T = HSV, class OutputT = double>
requires (std::same_as<T, HSV> || std::same_as<T, RGB_DOUBLE>)
constexpr static auto getPlane(Image<T> input, std::size_t index)
{
auto input_data = input.getImageData();
std::vector<OutputT> output_data;
output_data.resize(input.count());
#pragma omp parallel for
for (std::size_t i = 0; i < input.count(); ++i)
{
output_data[i] = input_data[i].channels[index];
}
auto output = Image<OutputT>(output_data, input.getSize());
return output;
}
// getRplane function implementation
constexpr static auto getRplane(Image<RGB> input)
{
return getPlane(input, 0);
}
// getRplane function implementation
constexpr static auto getRplane(Image<RGB_DOUBLE> input)
{
return getPlane(input, 0);
}
// getGplane function implementation
constexpr static auto getGplane(Image<RGB> input)
{
return getPlane(input, 1);
}
// getGplane function implementation
constexpr static auto getGplane(Image<RGB_DOUBLE> input)
{
return getPlane(input, 1);
}
// getBplane function implementation
constexpr static auto getBplane(Image<RGB> input)
{
return getPlane(input, 2);
}
// getBplane function implementation
constexpr static auto getBplane(Image<RGB_DOUBLE> input)
{
return getPlane(input, 2);
}
template<class T = HSV>
requires (std::same_as<T, HSV>)
constexpr static auto getHplane(Image<T> input)
{
return getPlane(input, 0);
}
template<class T = HSV>
requires (std::same_as<T, HSV>)
constexpr static auto getSplane(Image<T> input)
{
return getPlane(input, 1);
}
template<class T = HSV>
requires (std::same_as<T, HSV>)
constexpr static auto getVplane(Image<T> input)
{
return getPlane(input, 2);
}
// apply_each template function implementation
template<class F, class... Args>
constexpr static auto apply_each(Image<RGB> input, F operation, Args&&... args)
{
auto Rplane = std::async(std::launch::async, [&] { return operation(getRplane(input), args...); });
auto Gplane = std::async(std::launch::async, [&] { return operation(getGplane(input), args...); });
auto Bplane = std::async(std::launch::async, [&] { return operation(getBplane(input), args...); });
return constructRGB(Rplane.get(), Gplane.get(), Bplane.get());
}
// apply_each template function implementation
template<class F, class... Args>
constexpr static auto apply_each(Image<RGB_DOUBLE> input, F operation, Args&&... args)
{
auto Rplane = std::async(std::launch::async, [&] { return operation(getRplane(input), args...); });
auto Gplane = std::async(std::launch::async, [&] { return operation(getGplane(input), args...); });
auto Bplane = std::async(std::launch::async, [&] { return operation(getBplane(input), args...); });
return constructRGBDOUBLE(Rplane.get(), Gplane.get(), Bplane.get());
}
// apply_each template function implementation
template<class F, class... Args>
constexpr static auto apply_each(Image<HSV> input, F operation, Args&&... args)
{
auto Hplane = std::async(std::launch::async, [&] { return operation(getHplane(input), args...); });
auto Splane = std::async(std::launch::async, [&] { return operation(getSplane(input), args...); });
auto Vplane = std::async(std::launch::async, [&] { return operation(getVplane(input), args...); });
return constructHSV(Hplane.get(), Splane.get(), Vplane.get());
}
// im2double function implementation
constexpr static auto im2double(Image<RGB> input)
{
return convert_image<RGB_DOUBLE>(input);
}
// im2double function implementation
constexpr static auto im2double(Image<GrayScale> input)
{
return input.cast<double>();
}
// im2uint8 function implementation
constexpr static auto im2uint8(Image<RGB_DOUBLE> input)
{
return convert_image<RGB>(input);
}
// im2uint8 function implementation
constexpr static auto im2uint8(Image<double> input)
{
return input.cast<GrayScale>();
}
// print_with_latex function implementation
static void print_with_latex(Image<RGB> input)
{
std::cout << "\\begin{tikzpicture}[x=1cm,y=0.4cm]\n";
for (std::size_t y = 0; y < input.getHeight(); ++y)
{
for (std::size_t x = 0; x < input.getWidth(); ++x)
{
auto R = input.at(x, y).channels[0];
auto G = input.at(x, y).channels[1];
auto B = input.at(x, y).channels[2];
std::cout << "\\draw (" << x << "," << y <<
") node[anchor=south,fill={rgb:red," << +R << ";green," << +G << ";blue," << +B << "}] {};\n";
}
}
std::cout << "\\end{tikzpicture}\n";
}
// print_with_latex_to_file function implementation
static void print_with_latex_to_file(Image<RGB> input, std::string filename)
{
std::ofstream newfile;
newfile.open(filename);
newfile << "\\begin{tikzpicture}[x=1cm,y=0.4cm]\n";
for (std::size_t y = 0; y < input.getHeight(); ++y)
{
for (std::size_t x = 0; x < input.getWidth(); ++x)
{
auto R = input.at(x, y).channels[0];
auto G = input.at(x, y).channels[1];
auto B = input.at(x, y).channels[2];
newfile << "\\draw (" << x << "," << y <<
") node[anchor=south,fill={rgb:red," << +R << ";green," << +G << ";blue," << +B << "}] {};\n";
}
}
newfile << "\\end{tikzpicture}\n";
newfile.close();
return;
}
// subimage template function implementation
// Test: https://godbolt.org/z/9vv3eGYhq
template<typename ElementT>
constexpr static auto subimage(
const Image<ElementT>& input,
const std::size_t width,
std::size_t height,
std::size_t xcenter,
std::size_t ycenter,
ElementT default_element = ElementT{}
)
{
Image<ElementT> output(width, height);
auto cornerx = xcenter - static_cast<std::size_t>(std::floor(static_cast<double>(width) / 2));
auto cornery = ycenter - static_cast<std::size_t>(std::floor(static_cast<double>(height) / 2));
for (std::size_t y = 0; y < output.getHeight(); ++y)
{
for (std::size_t x = 0; x < output.getWidth(); ++x)
{
if (cornerx + x >= input.getWidth() || cornery + y >= input.getHeight())
{
output.at(x, y) = default_element;
}
else
{
output.at(x, y) = input.at(cornerx + x, cornery + y);
}
}
}
return output;
}
// subimage2 template function implementation
template<typename ElementT>
constexpr static auto subimage2(const Image<ElementT>& input, const std::size_t startx, const std::size_t endx, const std::size_t starty, const std::size_t endy)
{
assert(startx <= endx);
assert(starty <= endy);
Image<ElementT> output(endx - startx + 1, endy - starty + 1);
auto width = output.getWidth();
auto height = output.getHeight();
#pragma omp parallel for collapse(2)
for (std::size_t y = 0; y < height; ++y)
{
for (std::size_t x = 0; x < width; ++x)
{
output.at_without_boundary_check(x, y) = input.at_without_boundary_check(startx + x, starty + y);
}
}
return output;
}
template<typename ElementT>
requires (std::same_as<ElementT, RGB>)
constexpr static auto highlight_region(
const Image<ElementT>& input,
const std::size_t startx, const std::size_t endx, const std::size_t starty, const std::size_t endy,
const std::size_t width = 5, const std::uint8_t value_r = 223, const std::uint8_t value_g = 0, const std::uint8_t value_b = 34)
{
assert(startx <= endx);
assert(starty <= endy);
auto output = input;
for (std::size_t y = starty - width / 2; y < endy + width / 2; ++y)
{
for (std::size_t x = startx - width / 2; x < endx + width / 2; ++x)
{
if (std::abs(static_cast<int>(x) - static_cast<int>(startx)) < width ||
std::abs(static_cast<int>(x) - static_cast<int>(endx)) < width ||
std::abs(static_cast<int>(y) - static_cast<int>(starty)) < width ||
std::abs(static_cast<int>(y) - static_cast<int>(endy)) < width)
{
output.at(x, y).channels[0] = value_r;
output.at(x, y).channels[1] = value_g;
output.at(x, y).channels[2] = value_b;
}
}
}
return output;
}
/* split function
* xsegments is a number for the block count in x axis
* ysegments is a number for the block count in y axis
*/
template<typename ElementT>
constexpr static auto split(const Image<ElementT>& input, std::size_t xsegments, std::size_t ysegments)
{
std::vector<std::vector<Image<ElementT>>> output;
std::size_t block_size_x = input.getWidth() / xsegments;
std::size_t block_size_y = input.getHeight() / ysegments;
for (std::size_t y = 0; y < ysegments; y++)
{
std::vector<Image<ElementT>> output2;
for (std::size_t x = 0; x < xsegments; x ++)
{
output2.push_back(subimage2(input,
x * block_size_x,
(x + 1) * block_size_x - 1,
y * block_size_y,
(y + 1) * block_size_y - 1));
}
output.push_back(output2);
}
return output;
}
// pixelwiseOperation template function implementation
template<std::size_t unwrap_level = 1, class... Args>
constexpr static auto pixelwiseOperation(auto op, const Args&... inputs)
{
auto transformed_data = recursive_transform<unwrap_level>(
op,
inputs.getImageData()...);
auto output = Image<recursive_unwrap_type_t<unwrap_level, decltype(transformed_data)>>(
transformed_data,
first_of(inputs...).getSize());
return output;
}
// pixelwiseOperation template function implementation
template<std::size_t unwrap_level = 1, class ExPo, class InputT>
requires (std::is_execution_policy_v<std::remove_cvref_t<ExPo>>)
constexpr static auto pixelwiseOperation(ExPo execution_policy, auto op, const Image<InputT>& input1)
{
auto transformed_data = recursive_transform<unwrap_level>(
execution_policy,
op,
(input1.getImageData()));
auto output = Image<recursive_unwrap_type_t<unwrap_level, decltype(transformed_data)>>(
transformed_data,
input1.getSize());
return output;
}
// rgb2hsv template function implementation
template<typename ElementT, typename OutputT = HSV>
requires (std::same_as<ElementT, RGB> || std::same_as<ElementT, RGB_DOUBLE>)
constexpr static auto rgb2hsv(const Image<ElementT>& input)
{
return pixelwiseOperation([](ElementT input) { return rgb2hsv(input); }, input);
}
// rgb2hsv template function implementation
template<class ExPo, typename ElementT, typename OutputT = HSV>
requires (std::same_as<ElementT, RGB> || std::same_as<ElementT, RGB_DOUBLE>) &&
(std::is_execution_policy_v<std::remove_cvref_t<ExPo>>)
constexpr static auto rgb2hsv(ExPo execution_policy, const Image<ElementT>& input)
{
return pixelwiseOperation(execution_policy, [](ElementT input) { return rgb2hsv(input); }, input);
}
// hsv2rgb template function implementation
template<typename OutputT = RGB>
constexpr static auto hsv2rgb(const Image<HSV>& input)
{
return pixelwiseOperation([](HSV input) { return hsv2rgb(input); }, input);
}
// hsv2rgb template function implementation
template<class ExPo>
requires(std::is_execution_policy_v<std::remove_cvref_t<ExPo>>)
constexpr static auto hsv2rgb(ExPo execution_policy, const Image<HSV>& input)
{
return pixelwiseOperation(execution_policy, [](HSV input) { return hsv2rgb(input); }, input);
}
template<typename ElementT>
constexpr static auto concat_horizontal(Image<ElementT> input1, Image<ElementT> input2)
{
check_height_same(input1, input2);
Image<ElementT> output(input1.getWidth() + input2.getWidth(), input1.getHeight());
for (std::size_t y = 0; y < input1.getHeight(); ++y)
{
for (std::size_t x = 0; x < input1.getWidth(); ++x)
{
output.at(x, y) = input1.at(x, y);
}
}
for (std::size_t y = 0; y < input2.getHeight(); ++y)
{
for (std::size_t x = 0; x < input2.getWidth(); ++x)
{
output.at(input1.getWidth() + x, y) = input2.at(x, y);
}
}
return output;
}
template<typename ElementT>
constexpr static auto concat_horizontal(const std::vector<Image<ElementT>>& input)
{
//return recursive_reduce(input, Image<ElementT>(0, input[0].getHeight()), [](Image<ElementT> element1, Image<ElementT> element2) { return concat_horizontal(element1, element2); });
auto output = input[0];
for (std::size_t i = 1; i < input.size(); i++)
{
output = concat_horizontal(output, input[i]);
}
return output;
}
template<typename ElementT>
constexpr static auto concat_vertical(const Image<ElementT>& input1, const Image<ElementT>& input2)
{
check_width_same(input1, input2);