-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
365 lines (314 loc) · 14.3 KB
/
main.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
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
/* Developed by Jimmy Hu */
// compile command:
// clang++ -std=c++20 -Xpreprocessor -fopenmp -I/usr/local/include -L/usr/local/lib -lomp main.cpp -L /usr/local/Cellar/llvm/10.0.0_3/lib/ -lm -O3 -o main -v
// https://stackoverflow.com/a/61821729/6667035
// clear && rm -rf ./main && g++-11 -std=c++20 -O4 -ffast-math -funsafe-math-optimizations -std=c++20 -fpermissive -H --verbose -Wall main.cpp -o main
//#define USE_BOOST_ITERATOR
//#define USE_BOOST_SERIALIZATION
#include <filesystem>
#include <execution>
#include <valarray>
#include "basic_functions.h"
#include "image_io.h"
#include "image_operations.h"
//#define BOOST_TEST_DYN_LINK
//#define BOOST_TEST_MODULE image_elementwise_tests
#ifdef BOOST_TEST_MODULE
#include <boost/test/included/unit_test.hpp>
#ifdef BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
#else
#include <boost/test/included/unit_test.hpp>
#endif // BOOST_TEST_DYN_LINK
#include <boost/mpl/list.hpp>
#include <boost/mpl/vector.hpp>
#include <tao/tuple/tuple.hpp>
typedef boost::mpl::list<
byte, char, int, short, long, long long int,
unsigned int, unsigned short int, unsigned long int, unsigned long long int,
float, double, long double> test_types;
BOOST_AUTO_TEST_CASE_TEMPLATE(image_elementwise_add_test, T, test_types)
{
std::size_t size_x = 10;
std::size_t size_y = 10;
T initVal = 10;
T increment = 1;
auto test = TinyDIP::Image<T>(size_x, size_y, initVal);
test += TinyDIP::Image<T>(size_x, size_y, increment);
BOOST_TEST(test == TinyDIP::Image<T>(size_x, size_y, initVal + increment));
}
BOOST_AUTO_TEST_CASE_TEMPLATE(image_elementwise_add_test_zero_dimensions, T, test_types)
{
std::size_t size_x = 0; // Test images with both of the dimensions having size zero.
std::size_t size_y = 0; // Test images with both of the dimensions having size zero.
T initVal = 10;
T increment = 1;
auto test = TinyDIP::Image<T>(size_x, size_y, initVal);
test += TinyDIP::Image<T>(size_x, size_y, increment);
BOOST_TEST(test == TinyDIP::Image<T>(size_x, size_y, initVal + increment));
}
BOOST_AUTO_TEST_CASE_TEMPLATE(image_elementwise_add_test_large_dimensions, T, test_types)
{
std::size_t size_x = 18446744073709551615; // Test images with very large dimensions (std::numeric_limits<std::size_t>::max()).
std::size_t size_y = 18446744073709551615; // Test images with very large dimensions (std::numeric_limits<std::size_t>::max()).
T initVal = 10;
T increment = 1;
auto test = TinyDIP::Image<T>(size_x, size_y, initVal);
test += TinyDIP::Image<T>(size_x, size_y, increment);
BOOST_TEST(test == TinyDIP::Image<T>(size_x, size_y, initVal + increment));
}
BOOST_AUTO_TEST_CASE_TEMPLATE(image_elementwise_minus_test, T, test_types)
{
std::size_t size_x = 10;
std::size_t size_y = 10;
T initVal = 10;
T difference = 1;
auto test = TinyDIP::Image<T>(size_x, size_y, initVal);
test -= TinyDIP::Image<T>(size_x, size_y, difference);
BOOST_TEST(test == TinyDIP::Image<T>(size_x, size_y, initVal - difference));
}
BOOST_AUTO_TEST_CASE_TEMPLATE(image_elementwise_minus_test_zero_dimensions, T, test_types)
{
std::size_t size_x = 0; // Test images with both of the dimensions having size zero.
std::size_t size_y = 0; // Test images with both of the dimensions having size zero.
T initVal = 10;
T difference = 1;
auto test = TinyDIP::Image<T>(size_x, size_y, initVal);
test -= TinyDIP::Image<T>(size_x, size_y, difference);
BOOST_TEST(test == TinyDIP::Image<T>(size_x, size_y, initVal - difference));
}
BOOST_AUTO_TEST_CASE_TEMPLATE(image_elementwise_minus_test_large_dimensions, T, test_types)
{
std::size_t size_x = 18446744073709551615; // Test images with very large dimensions (std::numeric_limits<std::size_t>::max()).
std::size_t size_y = 18446744073709551615; // Test images with very large dimensions (std::numeric_limits<std::size_t>::max()).
T initVal = 10;
T difference = 1;
auto test = TinyDIP::Image<T>(size_x, size_y, initVal);
test -= TinyDIP::Image<T>(size_x, size_y, difference);
BOOST_TEST(test == TinyDIP::Image<T>(size_x, size_y, initVal - difference));
}
BOOST_AUTO_TEST_CASE_TEMPLATE(image_elementwise_multiplies_test, T, test_types)
{
std::size_t size_x = 10;
std::size_t size_y = 10;
T initVal = 10;
T multiplier = 2;
auto test = TinyDIP::Image<T>(size_x, size_y, initVal);
test *= TinyDIP::Image<T>(size_x, size_y, multiplier);
BOOST_TEST(test == TinyDIP::Image<T>(size_x, size_y, initVal * multiplier));
}
BOOST_AUTO_TEST_CASE_TEMPLATE(image_elementwise_multiplies_test_zero_dimensions, T, test_types)
{
std::size_t size_x = 0; // Test images with both of the dimensions having size zero.
std::size_t size_y = 0; // Test images with both of the dimensions having size zero.
T initVal = 10;
T multiplier = 2;
auto test = TinyDIP::Image<T>(size_x, size_y, initVal);
test *= TinyDIP::Image<T>(size_x, size_y, multiplier);
BOOST_TEST(test == TinyDIP::Image<T>(size_x, size_y, initVal * multiplier));
}
BOOST_AUTO_TEST_CASE_TEMPLATE(image_elementwise_multiplies_test_large_dimensions, T, test_types)
{
std::size_t size_x = 18446744073709551615; // Test images with very large dimensions (std::numeric_limits<std::size_t>::max()).
std::size_t size_y = 18446744073709551615; // Test images with very large dimensions (std::numeric_limits<std::size_t>::max()).
T initVal = 10;
T multiplier = 2;
auto test = TinyDIP::Image<T>(size_x, size_y, initVal);
test *= TinyDIP::Image<T>(size_x, size_y, multiplier);
BOOST_TEST(test == TinyDIP::Image<T>(size_x, size_y, initVal * multiplier));
}
BOOST_AUTO_TEST_CASE_TEMPLATE(image_elementwise_divides_test, T, test_types)
{
std::size_t size_x = 10;
std::size_t size_y = 10;
T initVal = 10;
T divider = 2;
auto test = TinyDIP::Image<T>(size_x, size_y, initVal);
test /= TinyDIP::Image<T>(size_x, size_y, divider);
BOOST_TEST(test == TinyDIP::Image<T>(size_x, size_y, initVal / divider));
}
BOOST_AUTO_TEST_CASE_TEMPLATE(image_elementwise_divides_test_zero_dimensions, T, test_types)
{
std::size_t size_x = 0; // Test images with both of the dimensions having size zero.
std::size_t size_y = 0; // Test images with both of the dimensions having size zero.
T initVal = 10;
T divider = 2;
auto test = TinyDIP::Image<T>(size_x, size_y, initVal);
test /= TinyDIP::Image<T>(size_x, size_y, divider);
BOOST_TEST(test == TinyDIP::Image<T>(size_x, size_y, initVal / divider));
}
BOOST_AUTO_TEST_CASE_TEMPLATE(image_elementwise_divides_test_large_dimensions, T, test_types)
{
std::size_t size_x = 18446744073709551615; // Test images with very large dimensions (std::numeric_limits<std::size_t>::max()).
std::size_t size_y = 18446744073709551615; // Test images with very large dimensions (std::numeric_limits<std::size_t>::max()).
T initVal = 10;
T divider = 2;
auto test = TinyDIP::Image<T>(size_x, size_y, initVal);
test /= TinyDIP::Image<T>(size_x, size_y, divider);
BOOST_TEST(test == TinyDIP::Image<T>(size_x, size_y, initVal / divider));
}
/*
BOOST_AUTO_TEST_CASE_TEMPLATE(image_elementwise_divides_zero_test, T, test_types)
{
std::size_t size_x = 10;
std::size_t size_y = 10;
T initVal = 10;
T divider = 0;
auto test = TinyDIP::Image<T>(size_x, size_y, initVal);
test /= TinyDIP::Image<T>(size_x, size_y, divider);
BOOST_TEST(test == TinyDIP::Image<T>(size_x, size_y, initVal / divider)); // dividing by zero test
}
*/
#endif
void difference_and_enhancement(std::string input_path1, std::string input_path2, double enhancement_times)
{
if (input_path1.empty())
{
std::cerr << "Input path is empty!";
}
std::filesystem::path input1 = input_path1;
std::filesystem::path input2 = input_path2;
}
#ifndef BOOST_TEST_MODULE
void addLeadingZeros(std::string input_path, std::string output_path);
void print(auto comment, auto const& seq, char term = ' ') {
for (std::cout << comment << '\n'; auto const& elem : seq)
std::cout << elem << term;
std::cout << '\n';
}
int main()
{
auto start = std::chrono::system_clock::now();
std::string file_path = "InputImages/1";
auto bmp1 = TinyDIP::bmp_read(file_path.c_str(), false);
std::size_t N1 = 8, N2 = 8;
auto block_count_x = bmp1.getWidth() / N1;
auto block_count_y = bmp1.getHeight() / N2;
bmp1 = TinyDIP::concat(TinyDIP::recursive_transform<2>(
//std::execution::par,
[](auto&& element)
{
auto hsv_block = TinyDIP::rgb2hsv(TinyDIP::im2double(element));
auto v_block = TinyDIP::getVplane(hsv_block);
auto v_block_dct = TinyDIP::dct2(v_block);
return TinyDIP::hsv2rgb(TinyDIP::constructHSV(
TinyDIP::getHplane(hsv_block),
TinyDIP::getSplane(hsv_block),
TinyDIP::idct2(v_block_dct)
));
},
TinyDIP::split(bmp1, block_count_x, block_count_y)));
bmp1 = copyResizeBicubic(bmp1, bmp1.getWidth() * 2, bmp1.getHeight() * 2);
//bmp1 = gaussian_fisheye(bmp1, 800.0);
auto v_plane = TinyDIP::getVplane(TinyDIP::rgb2hsv(bmp1));
auto SIFT_keypoints = TinyDIP::SIFT_impl::get_potential_keypoint(v_plane);
std::cout << "SIFT_keypoints = " << SIFT_keypoints.size() << "\n";
bmp1 = TinyDIP::draw_points(bmp1, SIFT_keypoints);
for (auto&& each_SIFT_keypoint : SIFT_keypoints)
{
TinyDIP::SIFT_impl::get_keypoint_descriptor(v_plane, each_SIFT_keypoint);
}
auto cv_mat = TinyDIP::to_cv_mat(bmp1);
cv::imshow("Image", cv_mat);
cv::waitKey(0);
TinyDIP::bmp_write("test20240816", bmp1);
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
std::time_t end_time = std::chrono::system_clock::to_time_t(end);
std::cout << "Computation finished at " << std::ctime(&end_time) << "elapsed time: " << elapsed_seconds.count() << " seconds\n";
return EXIT_SUCCESS;
//addLeadingZeros("../../../InputImages/", "../../../OutputImages/");
//bicubicInterpolationTest();
//bmp1 = TinyDIP::hsv2rgb(TinyDIP::rgb2hsv(bmp1));
bmp1 = TinyDIP::apply_each(bmp1, [](auto&& element) { return TinyDIP::copyResizeBicubic(element, 480, 320); });
TinyDIP::print_with_latex_to_file(bmp1, "test.txt");
TinyDIP::bmp_write("test", bmp1);
auto blank = TinyDIP::Image<GrayScale>(bmp1.getWidth(), bmp1.getHeight());
TinyDIP::bmp_write("test", TinyDIP::constructRGB(
TinyDIP::getRplane(bmp1),
TinyDIP::Image<GrayScale>(bmp1.getWidth(), bmp1.getHeight()),
TinyDIP::Image<GrayScale>(bmp1.getWidth(), bmp1.getHeight())
));
std::cout << "*********\n";
std::size_t size = 10;
auto img1 = TinyDIP::gaussianFigure2D(size, size, 5, 5, static_cast<double>(3));
img1.print(",");
return 0;
#ifdef USE_BOOST_SERIALIZATION
img1.Save("img1");
#endif // USE_BOOST_SERIALIZATION
auto img2 = TinyDIP::gaussianFigure2D(size, size, 5, 5, static_cast<double>(3));
auto img3 = TinyDIP::gaussianFigure2D(size, size, 5, 5, static_cast<double>(3));
auto img4 = TinyDIP::gaussianFigure2D(size, size, 5, 5, static_cast<double>(3));
TinyDIP::subtract(
TinyDIP::gaussianFigure2D(size, size, 5, 5, static_cast<double>(5)),
TinyDIP::gaussianFigure2D(size, size, 5, 5, static_cast<double>(3))).print(",");
TinyDIP::pixelwise_multiplies(
img1, img2, img3, img4, img2, img3, img4, img2, img3, img4,
img2, img3, img4, img2, img3, img4, img2, img3, img4,
img2, img3, img4, img2, img3, img4, img2, img3, img4,
img2, img3, img4, img2, img3, img4, img2, img3, img4).print();
return 0;
#ifdef USE_BOOST_ITERATOR
std::vector<int> a{1, 2, 3}, b{4, 5, 6};
auto result = TinyDIP::recursive_transform<1>(
std::execution::par,
[](int element1, int element2) { return element1 * element2; },
a, b);
for (size_t i = 0; i < result.size(); i++)
{
std::cout << result.at(i) << std::endl;
}
std::vector<decltype(a)> c{a, a, a}, d{b, b, b};
auto result2 = TinyDIP::recursive_transform<2>(
std::execution::par,
[](int element1, int element2) { return element1 * element2; },
c, d);
TinyDIP::recursive_print(result2);
#endif
return 0;
}
#endif
void test()
{
constexpr int dims = 5;
std::vector<std::string> test_vector1{ "1", "4", "7" };
auto test1 = TinyDIP::n_dim_vector_generator<dims>(test_vector1, 3);
std::vector<std::string> test_vector2{ "2", "5", "8" };
auto test2 = TinyDIP::n_dim_vector_generator<dims>(test_vector2, 3);
std::vector<std::string> test_vector3{ "3", "6", "9" };
auto test3 = TinyDIP::n_dim_vector_generator<dims>(test_vector3, 3);
std::vector<std::string> test_vector4{ "a", "b", "c" };
auto test4 = TinyDIP::n_dim_vector_generator<dims>(test_vector4, 3);
auto output = TinyDIP::recursive_transform<dims + 1>(
[](auto element1, auto element2, auto element3, auto element4) { return element1 + element2 + element3 + element4; },
test1, test2, test3, test4);
std::cout << typeid(output).name() << std::endl;
TinyDIP::recursive_print(output
.at(0).at(0).at(0).at(0).at(0));
return;
}
// bicubicInterpolationTest function implementation
void bicubicInterpolationTest()
{
TinyDIP::Image<TinyDIP::GrayScale> image1(3, 3);
image1.setAllValue(1);
std::cout << "Width: " + std::to_string(image1.getWidth()) + "\n";
std::cout << "Height: " + std::to_string(image1.getHeight()) + "\n";
image1.at(1, 1) = 100;
image1.print();
auto image2 = TinyDIP::copyResizeBicubic(image1, 12, 12);
image2.print();
}
void addLeadingZeros(std::string input_path, std::string output_path)
{
for (std::size_t i = 1; i <= 96; ++i)
{
std::string filename = input_path + std::to_string(i) + ".bmp";
std::cout << filename << "\n";
auto bmpimage = TinyDIP::bmp_read(filename.c_str(), true);
char buff[100];
snprintf(buff, sizeof(buff), "%s%05ld", output_path.c_str(), i);
TinyDIP::bmp_write(buff, bmpimage);
}
}