& keep,
54 | bool invert = false
55 | );
56 |
57 |
58 |
59 |
60 |
61 | template
62 | float ratio_of(const std::vector& data, const T& value);
63 |
64 |
65 | template
66 | auto max(const iterator begin, const iterator end);
67 |
68 | template
69 | auto min(const iterator begin, const iterator end);
70 |
71 | template
72 | auto sum(const iterator begin, const iterator end);
73 |
74 | template
75 | auto mean(const iterator begin, const iterator end);
76 |
77 | template
78 | auto variance(const iterator begin, const iterator end);
79 |
80 | }
81 |
82 | #include "Container.tpp"
83 |
84 |
--------------------------------------------------------------------------------
/LiveVisionKit/Functions/Extensions.hpp:
--------------------------------------------------------------------------------
1 | // *************************** LiveVisionKit ****************************
2 | // Copyright (C) 2022 Sebastian Di Marco (crowsinc.dev@gmail.com)
3 | //
4 | // This program is free software: you can redistribute it and/or modify
5 | // it under the terms of the GNU General Public License as published by
6 | // the Free Software Foundation, either version 3 of the License, or
7 | // (at your option) any later version.
8 | //
9 | // This program is distributed in the hope that it will be useful,
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | // GNU General Public License for more details.
13 | //
14 | // You should have received a copy of the GNU General Public License
15 | // along with this program. If not, see .
16 | // **********************************************************************
17 |
18 | #include
19 |
20 | namespace lvk
21 | {
22 | // cv::Size2f Operators
23 | cv::Size2f operator*(const cv::Size2f& v1, const cv::Size2f& v2);
24 |
25 | cv::Size2f operator/(const cv::Size2f& v1, const cv::Size2f& v2);
26 |
27 | cv::Size2f operator/(const float v1, const cv::Size2f& v2);
28 |
29 | cv::Size2f operator+(const cv::Size2f& v1, const float v2);
30 |
31 | cv::Size2f operator-(const cv::Size2f& v1, const float v2);
32 |
33 | // cv::Size Operators
34 | cv::Size operator*(const cv::Size& v1, const cv::Size& v2);
35 |
36 | cv::Size operator/(const cv::Size& v1, const cv::Size& v2);
37 |
38 | cv::Size operator/(const int v1, const cv::Size& v2);
39 |
40 | cv::Size operator+(const cv::Size& v1, const int v2);
41 |
42 | cv::Size operator-(const cv::Size& v1, const int v2);
43 |
44 |
45 | // cv::Point2f Operators
46 | cv::Point2f operator*(const cv::Point2f& p, const cv::Size2f& s);
47 |
48 | cv::Point2f operator/(const cv::Point2f& p, const cv::Size2f& s);
49 |
50 | cv::Point2f operator+(const cv::Point2f& p, const cv::Size2f& s);
51 |
52 | cv::Point2f operator-(const cv::Point2f& p, const cv::Size2f& s);
53 |
54 | cv::Point2f operator*(const cv::Point2f& p, const cv::Scalar& s);
55 |
56 | cv::Point2f operator/(const cv::Point2f& p, const cv::Scalar& s);
57 |
58 | cv::Point2f operator+(const cv::Point2f& p, const cv::Scalar& s);
59 |
60 | cv::Point2f operator-(const cv::Point2f& p, const cv::Scalar& s);
61 |
62 |
63 | // cv::Point Operators
64 | cv::Point operator*(const cv::Point& p, const cv::Size& s);
65 |
66 | cv::Point operator/(const cv::Point& p, const cv::Size& s);
67 |
68 | cv::Point operator+(const cv::Point& p, const cv::Size& s);
69 |
70 | cv::Point operator-(const cv::Point& p, const cv::Size& s);
71 |
72 |
73 | // cv::Scalar Operators
74 | cv::Scalar operator*(const cv::Scalar& v1, const cv::Scalar& v2);
75 |
76 | cv::Scalar operator/(const cv::Scalar& v1, const cv::Scalar& v2);
77 |
78 | cv::Scalar operator/(const cv::Scalar& v1, const double v2);
79 |
80 | cv::Scalar operator+(const cv::Scalar& v1, const double v2);
81 |
82 | cv::Scalar operator-(const cv::Scalar& v1, const double v2);
83 |
84 | }
--------------------------------------------------------------------------------
/LiveVisionKit/Functions/Image.hpp:
--------------------------------------------------------------------------------
1 | // *************************** LiveVisionKit ****************************
2 | // Copyright (C) 2022 Sebastian Di Marco (crowsinc.dev@gmail.com)
3 | //
4 | // This program is free software: you can redistribute it and/or modify
5 | // it under the terms of the GNU General Public License as published by
6 | // the Free Software Foundation, either version 3 of the License, or
7 | // (at your option) any later version.
8 | //
9 | // This program is distributed in the hope that it will be useful,
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | // GNU General Public License for more details.
13 | //
14 | // You should have received a copy of the GNU General Public License
15 | // along with this program. If not, see .
16 | // **********************************************************************
17 |
18 | #pragma once
19 |
20 | #include
21 | #include "Data/VideoFrame.hpp"
22 |
23 | namespace lvk
24 | {
25 |
26 | void remap(
27 | const VideoFrame& src,
28 | VideoFrame& dst,
29 | const cv::Mat& homography,
30 | const cv::Scalar& background,
31 | const bool inverted = false
32 | );
33 |
34 | void remap(const VideoFrame& src, VideoFrame& dst, const cv::UMat& offset_map, const cv::Scalar& background);
35 |
36 | void upscale(const cv::UMat& src, cv::UMat& dst, const cv::Size& size, const bool yuv = true);
37 |
38 | void sharpen(const cv::UMat& src, cv::UMat& dst, const float sharpness = 0.7f);
39 |
40 | }
--------------------------------------------------------------------------------
/LiveVisionKit/Functions/Logic.hpp:
--------------------------------------------------------------------------------
1 | // *************************** LiveVisionKit ****************************
2 | // Copyright (C) 2022 Sebastian Di Marco (crowsinc.dev@gmail.com)
3 | //
4 | // This program is free software: you can redistribute it and/or modify
5 | // it under the terms of the GNU General Public License as published by
6 | // the Free Software Foundation, either version 3 of the License, or
7 | // (at your option) any later version.
8 | //
9 | // This program is distributed in the hope that it will be useful,
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | // GNU General Public License for more details.
13 | //
14 | // You should have received a copy of the GNU General Public License
15 | // along with this program. If not, see .
16 | // **********************************************************************
17 |
18 | #pragma once
19 |
20 | namespace lvk
21 | {
22 | template
23 | bool test_bits(const T bits, const T test_flag);
24 |
25 | template
26 | bool any_of(const T value, Options ...option);
27 |
28 | template
29 | bool all_of(const T value, Options ...option);
30 |
31 |
32 | template
33 | T hysteresis(const T state, const T thresh_lower, const T state_lower, const T thresh_upper, const T state_upper);
34 |
35 | }
36 |
37 | #include "Logic.tpp"
38 |
--------------------------------------------------------------------------------
/LiveVisionKit/Functions/Logic.tpp:
--------------------------------------------------------------------------------
1 | // *************************** LiveVisionKit ****************************
2 | // Copyright (C) 2022 Sebastian Di Marco (crowsinc.dev@gmail.com)
3 | //
4 | // This program is free software: you can redistribute it and/or modify
5 | // it under the terms of the GNU General Public License as published by
6 | // the Free Software Foundation, either version 3 of the License, or
7 | // (at your option) any later version.
8 | //
9 | // This program is distributed in the hope that it will be useful,
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | // GNU General Public License for more details.
13 | //
14 | // You should have received a copy of the GNU General Public License
15 | // along with this program. If not, see .
16 | // **********************************************************************
17 |
18 | #pragma once
19 |
20 | #include
21 |
22 | namespace lvk
23 | {
24 |
25 | //---------------------------------------------------------------------------------------------------------------------
26 |
27 | template
28 | inline bool test_bits(const T bits, const T test_flag)
29 | {
30 | static_assert(std::is_integral_v);
31 |
32 | return (bits & test_flag) == test_flag;
33 | }
34 |
35 | //---------------------------------------------------------------------------------------------------------------------
36 |
37 | template
38 | inline bool any_of(const T value, Options ...option)
39 | {
40 | return (... || (value == option));
41 | }
42 |
43 | //---------------------------------------------------------------------------------------------------------------------
44 |
45 | template
46 | inline bool all_of(const T value, Options ...option)
47 | {
48 | return (... && (value == option));
49 | }
50 |
51 | //---------------------------------------------------------------------------------------------------------------------
52 |
53 | template
54 | inline T hysteresis(
55 | const T state,
56 | const T thresh_lower, const T state_lower,
57 | const T thresh_upper, const T state_upper
58 | )
59 | {
60 | if(state >= thresh_upper)
61 | return state_upper;
62 | if(state <= thresh_lower)
63 | return state_lower;
64 | return state;
65 | }
66 |
67 | //---------------------------------------------------------------------------------------------------------------------
68 |
69 | }
--------------------------------------------------------------------------------
/LiveVisionKit/Functions/Math.hpp:
--------------------------------------------------------------------------------
1 | // *************************** LiveVisionKit ****************************
2 | // Copyright (C) 2022 Sebastian Di Marco (crowsinc.dev@gmail.com)
3 | //
4 | // This program is free software: you can redistribute it and/or modify
5 | // it under the terms of the GNU General Public License as published by
6 | // the Free Software Foundation, either version 3 of the License, or
7 | // (at your option) any later version.
8 | //
9 | // This program is distributed in the hope that it will be useful,
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | // GNU General Public License for more details.
13 | //
14 | // You should have received a copy of the GNU General Public License
15 | // along with this program. If not, see .
16 | // **********************************************************************
17 |
18 | #pragma once
19 |
20 | #include
21 |
22 | namespace lvk
23 | {
24 |
25 | template
26 | T to_degrees(const T& radians);
27 |
28 | template
29 | T to_radians(const T& degrees);
30 |
31 | template
32 | T angle_of(const cv::Point_& v, const cv::Point_& ref = {1, 0});
33 |
34 |
35 | template
36 | T round_multiple(const T& value, const T& base);
37 |
38 | template
39 | T round_even(const T& value);
40 |
41 |
42 | template
43 | T ratio_of(const V& numerator, const V& denominator);
44 |
45 |
46 | template
47 | T index_2d(T x, T y, T row_length);
48 |
49 | template
50 | cv::Point_ inv_index_2d(T index, T row_length);
51 |
52 |
53 | template
54 | int sign(const T& value, const T& origin = T());
55 |
56 | template
57 | int sign_2d(const cv::Point_& point, const cv::Point_& l1, const cv::Point_& l2);
58 |
59 |
60 | template
61 | inline V lerp(const V& current, const V& target, const T& amount);
62 |
63 | template
64 | inline V step(const V& current, const V& target, const T& amount);
65 |
66 |
67 | template
68 | bool between_01(const T& value);
69 |
70 | template
71 | bool between_01_strict(const T& value);
72 |
73 | template
74 | bool between(const T& value, const T& min, const T& max);
75 |
76 | template
77 | bool between_strict(const T& value, const T& min, const T& max);
78 |
79 |
80 | template
81 | bool within(const T& value, const T& target, const T& tolerance);
82 |
83 | template
84 | bool within_strict(const T& value, const T& target, const T& tolerance);
85 |
86 |
87 | template
88 | T exp_moving_average(const T average, const T new_sample, const float smoothing_factor);
89 |
90 | template
91 | T moving_median(const T median, const T new_sample, const float learning_rate);
92 |
93 |
94 | template
95 | cv::Rect_ crop(const cv::Size_& region, const cv::Size2f& proportions);
96 |
97 | template
98 | cv::Rect_ crop(const cv::Size_& region, const float proportion);
99 |
100 |
101 | template
102 | cv::Scalar barycentric_rect(const cv::Rect_& rect, const cv::Point_& point);
103 |
104 | }
105 |
106 | #include "Math.tpp"
107 |
--------------------------------------------------------------------------------
/LiveVisionKit/Functions/OpenCL/Kernels.cpp:
--------------------------------------------------------------------------------
1 | // *************************** LiveVisionKit ****************************
2 | // Copyright (C) 2022 Sebastian Di Marco (crowsinc.dev@gmail.com)
3 | //
4 | // This program is free software: you can redistribute it and/or modify
5 | // it under the terms of the GNU General Public License as published by
6 | // the Free Software Foundation, either version 3 of the License, or
7 | // (at your option) any later version.
8 | //
9 | // This program is distributed in the hope that it will be useful,
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | // GNU General Public License for more details.
13 | //
14 | // You should have received a copy of the GNU General Public License
15 | // along with this program. If not, see .
16 | // **********************************************************************
17 |
18 | #include "Kernels.hpp"
19 |
20 | #include "Directives.hpp"
21 |
22 | namespace lvk::ocl
23 | {
24 |
25 | //---------------------------------------------------------------------------------------------------------------------
26 |
27 | cv::ocl::Program load_program(const char* name, const char* source, const char* flags)
28 | {
29 | cv::String compilation_log;
30 |
31 | cv::ocl::ProgramSource program_source(name, name, source, "");
32 | cv::ocl::Program program(program_source, flags, compilation_log);
33 | if(program.ptr() == nullptr)
34 | {
35 | // Perform custom assert with compilation error log.
36 | lvk::context::assert_handler(
37 | LVK_FILE,
38 | __func__,
39 | std::string("Failed to compile OpenCL program \'")
40 | + name + "\' with compilation log: \n\n" + compilation_log
41 | );
42 | return {};
43 | };
44 | return std::move(program);
45 | }
46 |
47 | //---------------------------------------------------------------------------------------------------------------------
48 |
49 | void optimal_groups(const cv::UMat& buffer, size_t global_groups[3], size_t local_groups[3])
50 | {
51 | // Reset all group sizings to identity.
52 | local_groups[0] = 1; local_groups[1] = 1; local_groups[2] = 1;
53 | global_groups[0] = 1; global_groups[1] = 1; global_groups[2] = 1;
54 |
55 | // Figure out optimal and compatible 2D local and global work sizes for a kernel.
56 | // Note that this is just based on rule of thumbs, rather than concrete optimality.
57 | if(buffer.dims == 1 || buffer.cols == 1)
58 | {
59 | // 1D buffers: default to a work group of 64x1 threads.
60 | local_groups[0] = 64;
61 | global_groups[0] = static_cast(std::ceil(static_cast(buffer.rows) / 64.0f)) * 64;
62 | }
63 | else if(buffer.dims == 2)
64 | {
65 | // 2D buffers: default to a work group of 8x8 threads.
66 | local_groups[0] = 8; local_groups[1] = 8;
67 | global_groups[0] = static_cast(std::ceil(static_cast(buffer.cols) / 8.0f)) * 8;
68 | global_groups[1] = static_cast(std::ceil(static_cast(buffer.rows) / 8.0f)) * 8;
69 | }
70 | else LVK_ASSERT(false && "Buffer dimensions are not supported");
71 | }
72 |
73 | //---------------------------------------------------------------------------------------------------------------------
74 |
75 | }
--------------------------------------------------------------------------------
/LiveVisionKit/Functions/OpenCL/Kernels.hpp:
--------------------------------------------------------------------------------
1 | // *************************** LiveVisionKit ****************************
2 | // Copyright (C) 2022 Sebastian Di Marco (crowsinc.dev@gmail.com)
3 | //
4 | // This program is free software: you can redistribute it and/or modify
5 | // it under the terms of the GNU General Public License as published by
6 | // the Free Software Foundation, either version 3 of the License, or
7 | // (at your option) any later version.
8 | //
9 | // This program is distributed in the hope that it will be useful,
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | // GNU General Public License for more details.
13 | //
14 | // You should have received a copy of the GNU General Public License
15 | // along with this program. If not, see .
16 | // **********************************************************************
17 |
18 | #pragma once
19 |
20 | #include
21 | #include
22 | #include
23 |
24 | namespace lvk::ocl
25 | {
26 |
27 | cv::ocl::Program load_program(const char* name, const char* source, const char* flags = "");
28 |
29 | void optimal_groups(const cv::UMat& buffer, size_t global_groups[3], size_t local_groups[3]);
30 |
31 | // OpenCL Kernel Sources
32 | namespace src
33 | {
34 | inline const char* fsr_source =
35 | #include "Sources/FSR.cl"
36 | ;
37 |
38 | inline const char* drawing_source =
39 | #include "Sources/Drawing.cl"
40 | ;
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/LiveVisionKit/Functions/Text.hpp:
--------------------------------------------------------------------------------
1 | // *************************** LiveVisionKit ****************************
2 | // Copyright (C) 2022 Sebastian Di Marco (crowsinc.dev@gmail.com)
3 | //
4 | // This program is free software: you can redistribute it and/or modify
5 | // it under the terms of the GNU General Public License as published by
6 | // the Free Software Foundation, either version 3 of the License, or
7 | // (at your option) any later version.
8 | //
9 | // This program is distributed in the hope that it will be useful,
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | // GNU General Public License for more details.
13 | //
14 | // You should have received a copy of the GNU General Public License
15 | // along with this program. If not, see .
16 | // **********************************************************************
17 |
18 | #pragma once
19 |
20 | #include
21 | #include
22 | #include
23 |
24 | namespace lvk
25 | {
26 |
27 | template
28 | std::vector parse_sequence(
29 | const std::string& string,
30 | const char delimiter = ',',
31 | const std::function& validate = [](auto index, auto value, auto failed){
32 | return !failed;
33 | }
34 | );
35 |
36 | }
37 |
38 | #include "Text.tpp"
39 |
--------------------------------------------------------------------------------
/LiveVisionKit/Functions/Text.tpp:
--------------------------------------------------------------------------------
1 | // *************************** LiveVisionKit ****************************
2 | // Copyright (C) 2022 Sebastian Di Marco (crowsinc.dev@gmail.com)
3 | //
4 | // This program is free software: you can redistribute it and/or modify
5 | // it under the terms of the GNU General Public License as published by
6 | // the Free Software Foundation, either version 3 of the License, or
7 | // (at your option) any later version.
8 | //
9 | // This program is distributed in the hope that it will be useful,
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | // GNU General Public License for more details.
13 | //
14 | // You should have received a copy of the GNU General Public License
15 | // along with this program. If not, see .
16 | // **********************************************************************
17 |
18 | #pragma once
19 |
20 | #include
21 | #include
22 | #include
23 | #include
24 | #include
25 |
26 | #include "Directives.hpp"
27 |
28 | namespace lvk
29 | {
30 |
31 | //---------------------------------------------------------------------------------------------------------------------
32 |
33 | template
34 | inline std::vector parse_sequence(
35 | const std::string& string,
36 | const char delimiter,
37 | const std::function& validate
38 | )
39 | {
40 | static_assert(std::is_fundamental_v);
41 |
42 | std::stringstream parser, stream(string);
43 | std::string token;
44 |
45 | size_t index = 0;
46 | std::vector output;
47 | while(std::getline(stream, token, delimiter))
48 | {
49 | parser.clear();
50 | parser.str(token);
51 |
52 | T value = T{};
53 | parser >> value;
54 |
55 | if(validate(index, value, parser.fail()))
56 | output.emplace_back(value);
57 |
58 | index++;
59 | }
60 |
61 | return output;
62 | }
63 |
64 | //---------------------------------------------------------------------------------------------------------------------
65 |
66 | }
67 |
--------------------------------------------------------------------------------
/LiveVisionKit/LiveVisionKit.hpp:
--------------------------------------------------------------------------------
1 | // *************************** LiveVisionKit ****************************
2 | // Copyright (C) 2022 Sebastian Di Marco (crowsinc.dev@gmail.com)
3 | //
4 | // This program is free software: you can redistribute it and/or modify
5 | // it under the terms of the GNU General Public License as published by
6 | // the Free Software Foundation, either version 3 of the License, or
7 | // (at your option) any later version.
8 | //
9 | // This program is distributed in the hope that it will be useful,
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | // GNU General Public License for more details.
13 | //
14 | // You should have received a copy of the GNU General Public License
15 | // along with this program. If not, see .
16 | // **********************************************************************
17 |
18 | // Library
19 |
20 | #include
21 | #include
22 |
23 | #include "Directives.hpp"
24 |
25 | #include "Functions/Math.hpp"
26 | #include "Functions/Text.hpp"
27 | #include "Functions/Image.hpp"
28 | #include "Functions/Logic.hpp"
29 | #include "Functions/Drawing.hpp"
30 | #include "Functions/Container.hpp"
31 | #include "Functions/Extensions.hpp"
32 |
33 |
34 | #include "Filters/VideoFilter.hpp"
35 | #include "Filters/CompositeFilter.hpp"
36 | #include "Filters/ConversionFilter.hpp"
37 | #include "Filters/DeblockingFilter.hpp"
38 | #include "Filters/StabilizationFilter.hpp"
39 |
40 | #include "Logging/Logger.hpp"
41 | #include "Logging/CSVLogger.hpp"
42 |
43 | #include "Math/WarpMesh.hpp"
44 | #include "Math/Homography.hpp"
45 | #include "Math/VirtualGrid.hpp"
46 | #include "Math/BoundingQuad.hpp"
47 |
48 | #include "Data/VideoFrame.hpp"
49 | #include "Data/SpatialMap.hpp"
50 | #include "Data/StreamBuffer.hpp"
51 |
52 | #include "Timing/Time.hpp"
53 | #include "Timing/Stopwatch.hpp"
54 | #include "Timing/TickTimer.hpp"
55 |
56 | #include "Utility/Unique.hpp"
57 | #include "Utility/Configurable.hpp"
58 |
59 | #include "Vision/FrameTracker.hpp"
60 | #include "Vision/PathSmoother.hpp"
61 | #include "Vision/FeatureDetector.hpp"
62 | #include "Vision/CameraCalibrator.hpp"
63 |
64 |
65 |
66 |
--------------------------------------------------------------------------------
/LiveVisionKit/Logging/CSVLogger.cpp:
--------------------------------------------------------------------------------
1 | // *************************** LiveVisionKit ****************************
2 | // Copyright (C) 2022 Sebastian Di Marco (crowsinc.dev@gmail.com)
3 | //
4 | // This program is free software: you can redistribute it and/or modify
5 | // it under the terms of the GNU General Public License as published by
6 | // the Free Software Foundation, either version 3 of the License, or
7 | // (at your option) any later version.
8 | //
9 | // This program is distributed in the hope that it will be useful,
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | // GNU General Public License for more details.
13 | //
14 | // You should have received a copy of the GNU General Public License
15 | // along with this program. If not, see .
16 | // **********************************************************************
17 |
18 | #include "CSVLogger.hpp"
19 |
20 | namespace lvk
21 | {
22 |
23 | //---------------------------------------------------------------------------------------------------------------------
24 |
25 | CSVLogger::CSVLogger(std::ostream& target)
26 | : Logger(target)
27 | {
28 | LVK_ASSERT(target.good());
29 | }
30 |
31 | //---------------------------------------------------------------------------------------------------------------------
32 |
33 | void CSVLogger::end_record(std::ostream& stream)
34 | {
35 | stream << "\n";
36 | }
37 |
38 | //---------------------------------------------------------------------------------------------------------------------
39 |
40 | void CSVLogger::begin_object(std::ostream& stream)
41 | {
42 | if(!is_new_record())
43 | stream << ",";
44 | }
45 |
46 | //---------------------------------------------------------------------------------------------------------------------
47 |
48 | }
--------------------------------------------------------------------------------
/LiveVisionKit/Logging/CSVLogger.hpp:
--------------------------------------------------------------------------------
1 | // *************************** LiveVisionKit ****************************
2 | // Copyright (C) 2022 Sebastian Di Marco (crowsinc.dev@gmail.com)
3 | //
4 | // This program is free software: you can redistribute it and/or modify
5 | // it under the terms of the GNU General Public License as published by
6 | // the Free Software Foundation, either version 3 of the License, or
7 | // (at your option) any later version.
8 | //
9 | // This program is distributed in the hope that it will be useful,
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | // GNU General Public License for more details.
13 | //
14 | // You should have received a copy of the GNU General Public License
15 | // along with this program. If not, see .
16 | // **********************************************************************
17 |
18 | #pragma once
19 |
20 | #include "Logger.hpp"
21 |
22 | #include
23 |
24 | namespace lvk
25 | {
26 |
27 | class CSVLogger : public Logger
28 | {
29 | public:
30 |
31 | explicit CSVLogger(std::ostream& target);
32 |
33 | ~CSVLogger() override = default;
34 |
35 | protected:
36 |
37 | void end_record(std::ostream& stream) override;
38 |
39 | void begin_object(std::ostream& stream) override;
40 |
41 | };
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/LiveVisionKit/Logging/Logger.hpp:
--------------------------------------------------------------------------------
1 | // *************************** LiveVisionKit ****************************
2 | // Copyright (C) 2022 Sebastian Di Marco (crowsinc.dev@gmail.com)
3 | //
4 | // This program is free software: you can redistribute it and/or modify
5 | // it under the terms of the GNU General Public License as published by
6 | // the Free Software Foundation, either version 3 of the License, or
7 | // (at your option) any later version.
8 | //
9 | // This program is distributed in the hope that it will be useful,
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | // GNU General Public License for more details.
13 | //
14 | // You should have received a copy of the GNU General Public License
15 | // along with this program. If not, see .
16 | // **********************************************************************
17 |
18 | #pragma once
19 |
20 | #include
21 | #include
22 | #include
23 |
24 | namespace lvk
25 | {
26 |
27 | class Logger
28 | {
29 | struct NextSignal {};
30 | public:
31 | inline const static NextSignal Next = {};
32 |
33 | explicit Logger(std::ostream& target = std::cout);
34 |
35 | virtual ~Logger() = default;
36 |
37 | template
38 | Logger& write(const T& object);
39 |
40 | template
41 | Logger& operator<<(const T& object);
42 |
43 | Logger& operator<<(const NextSignal& signal);
44 |
45 | template
46 | Logger& append(const T& object);
47 |
48 | template
49 | void operator+=(const T& object);
50 |
51 | template
52 | Logger& operator+(const T& object);
53 |
54 | std::ostream& raw();
55 |
56 | void next();
57 |
58 | virtual void flush();
59 |
60 | void hold(const bool all_inputs = false);
61 |
62 | void resume();
63 |
64 | virtual void reformat();
65 |
66 | bool has_error() const;
67 |
68 | bool has_started() const;
69 |
70 | protected:
71 |
72 | bool is_new_record() const;
73 |
74 | const std::ios& base_format() const;
75 |
76 | virtual void begin_record(std::ostream& stream);
77 |
78 | virtual void end_record(std::ostream& stream);
79 |
80 | virtual void begin_object(std::ostream& stream);
81 |
82 | virtual void end_object(std::ostream& stream);
83 |
84 | private:
85 | std::ostream& m_Stream;
86 | std::ios m_BaseFormat;
87 |
88 | bool m_Started = false;
89 | bool m_NewRecord = true;
90 | bool m_HoldRecord = false;
91 | bool m_HoldInputs = false;
92 | };
93 |
94 | }
95 |
96 | #include "Logger.tpp"
97 |
--------------------------------------------------------------------------------
/LiveVisionKit/Math/BoundingQuad.cpp:
--------------------------------------------------------------------------------
1 | // *************************** LiveVisionKit ****************************
2 | // Copyright (C) 2022 Sebastian Di Marco (crowsinc.dev@gmail.com)
3 | //
4 | // This program is free software: you can redistribute it and/or modify
5 | // it under the terms of the GNU General Public License as published by
6 | // the Free Software Foundation, either version 3 of the License, or
7 | // (at your option) any later version.
8 | //
9 | // This program is distributed in the hope that it will be useful,
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | // GNU General Public License for more details.
13 | //
14 | // You should have received a copy of the GNU General Public License
15 | // along with this program. If not, see .
16 | // **********************************************************************
17 |
18 | #include "BoundingQuad.hpp"
19 |
20 | #include "Functions/Math.hpp"
21 |
22 | namespace lvk
23 | {
24 |
25 | //---------------------------------------------------------------------------------------------------------------------
26 |
27 | BoundingQuad::BoundingQuad(const cv::Size2d& size, const Homography& homography)
28 | {
29 | // NOTE: Must follow counter-clockwise ordering
30 | m_LocalVertices.emplace_back(0, 0);
31 | m_LocalVertices.emplace_back(size.width, 0);
32 | m_LocalVertices.emplace_back(size.width, size.height);
33 | m_LocalVertices.emplace_back(0, size.height);
34 |
35 | this->transform(homography);
36 | }
37 |
38 | //---------------------------------------------------------------------------------------------------------------------
39 |
40 | void BoundingQuad::transform(const Homography& homography)
41 | {
42 | homography.transform(m_LocalVertices, m_Vertices);
43 | }
44 |
45 | //---------------------------------------------------------------------------------------------------------------------
46 |
47 | bool BoundingQuad::encloses(const cv::Rect2d& rect) const
48 | {
49 | const cv::Point2d tl = rect.tl();
50 | const cv::Point2d br = rect.br();
51 | const cv::Point2d tr(br.x, tl.y);
52 | const cv::Point2d bl(tl.x, br.y);
53 |
54 | return encloses(tl)
55 | && encloses(br)
56 | && encloses(tr)
57 | && encloses(bl);
58 | }
59 |
60 | //---------------------------------------------------------------------------------------------------------------------
61 |
62 | bool BoundingQuad::encloses(const BoundingQuad& quad) const
63 | {
64 | return encloses(quad.m_Vertices[0])
65 | && encloses(quad.m_Vertices[1])
66 | && encloses(quad.m_Vertices[2])
67 | && encloses(quad.m_Vertices[3]);
68 | }
69 |
70 | //---------------------------------------------------------------------------------------------------------------------
71 |
72 | bool BoundingQuad::encloses(const cv::Point2d& point) const
73 | {
74 | // A point is enclosed within the quad if it is left of
75 | // every quad edge, going in a counter-clockwise order.
76 | return sign_2d(point, m_Vertices[0], m_Vertices[1]) <= 0
77 | && sign_2d(point, m_Vertices[1], m_Vertices[2]) <= 0
78 | && sign_2d(point, m_Vertices[2], m_Vertices[3]) <= 0
79 | && sign_2d(point, m_Vertices[3], m_Vertices[0]) <= 0;
80 | }
81 |
82 | //---------------------------------------------------------------------------------------------------------------------
83 |
84 | }
85 |
--------------------------------------------------------------------------------
/LiveVisionKit/Math/BoundingQuad.hpp:
--------------------------------------------------------------------------------
1 | // *************************** LiveVisionKit ****************************
2 | // Copyright (C) 2022 Sebastian Di Marco (crowsinc.dev@gmail.com)
3 | //
4 | // This program is free software: you can redistribute it and/or modify
5 | // it under the terms of the GNU General Public License as published by
6 | // the Free Software Foundation, either version 3 of the License, or
7 | // (at your option) any later version.
8 | //
9 | // This program is distributed in the hope that it will be useful,
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | // GNU General Public License for more details.
13 | //
14 | // You should have received a copy of the GNU General Public License
15 | // along with this program. If not, see .
16 | // **********************************************************************
17 |
18 | #pragma once
19 |
20 | #include
21 |
22 | #include "Homography.hpp"
23 |
24 | namespace lvk
25 | {
26 |
27 | class BoundingQuad
28 | {
29 | public:
30 |
31 | explicit BoundingQuad(const cv::Size2d& size, const Homography& homography = Homography::Identity());
32 |
33 | void transform(const Homography& homography);
34 |
35 | bool encloses(const cv::Rect2d& rect) const;
36 |
37 | bool encloses(const BoundingQuad& quad) const;
38 |
39 | bool encloses(const cv::Point2d& point) const;
40 |
41 | private:
42 | std::vector m_LocalVertices, m_Vertices;
43 | };
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/LiveVisionKit/Math/Homography.hpp:
--------------------------------------------------------------------------------
1 | // *************************** LiveVisionKit ****************************
2 | // Copyright (C) 2022 Sebastian Di Marco (crowsinc.dev@gmail.com)
3 | //
4 | // This program is free software: you can redistribute it and/or modify
5 | // it under the terms of the GNU General Public License as published by
6 | // the Free Software Foundation, either version 3 of the License, or
7 | // (at your option) any later version.
8 | //
9 | // This program is distributed in the hope that it will be useful,
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | // GNU General Public License for more details.
13 | //
14 | // You should have received a copy of the GNU General Public License
15 | // along with this program. If not, see .
16 | // **********************************************************************
17 |
18 | #pragma once
19 |
20 | #include
21 | #include
22 |
23 | namespace lvk
24 | {
25 |
26 | class Homography
27 | {
28 | public:
29 |
30 | static const Homography& Zero();
31 |
32 | static const Homography& Identity();
33 |
34 | static Homography FromAffineMatrix(const cv::Mat& affine);
35 |
36 |
37 | Homography();
38 |
39 | Homography(const Homography& other);
40 |
41 | Homography(Homography&& other) noexcept;
42 |
43 | explicit Homography(const cv::Mat& matrix);
44 |
45 | explicit Homography(cv::Mat&& matrix) noexcept;
46 |
47 |
48 | void set_zero();
49 |
50 | void set_identity();
51 |
52 |
53 | cv::Point2d transform(const cv::Point2d& point) const;
54 |
55 | cv::Point2f transform(const cv::Point2f& point) const;
56 |
57 | cv::Point2d operator*(const cv::Point2d& point) const;
58 |
59 | cv::Point2f operator*(const cv::Point2f& point) const;
60 |
61 |
62 | void transform(const std::vector& points, std::vector& dst) const;
63 |
64 | void transform(const std::vector& points, std::vector& dst) const;
65 |
66 | std::vector operator*(const std::vector& points) const;
67 |
68 | std::vector operator*(const std::vector& points) const;
69 |
70 |
71 | void warp(const cv::UMat& src, cv::UMat& dst) const;
72 |
73 |
74 | const cv::Mat& data() const;
75 |
76 | Homography invert() const;
77 |
78 | bool is_identity() const;
79 |
80 | bool is_affine() const;
81 |
82 | bool is_zero() const;
83 |
84 |
85 | Homography& operator=(const cv::Mat& other);
86 |
87 | Homography& operator=(cv::Mat&& other) noexcept;
88 |
89 | Homography& operator=(const Homography& other);
90 |
91 | Homography& operator=(Homography&& other) noexcept;
92 |
93 |
94 | void operator+=(const Homography& other);
95 |
96 | void operator+=(const cv::Mat& other);
97 |
98 | void operator-=(const Homography& other);
99 |
100 | void operator-=(const cv::Mat& other);
101 |
102 | void operator*=(const Homography& other);
103 |
104 | void operator*=(const cv::Mat& other);
105 |
106 | void operator*=(const double scaling);
107 |
108 | void operator/=(const double scaling);
109 |
110 | private:
111 | cv::Mat m_Matrix;
112 | };
113 |
114 | Homography operator+(const Homography& left, const Homography& right);
115 |
116 | Homography operator-(const Homography& left, const Homography& right);
117 |
118 |
119 | Homography operator*(const Homography& left, const Homography& right);
120 |
121 | Homography operator*(const Homography& homography, const double scaling);
122 |
123 | Homography operator*(const double scaling, const Homography& homography);
124 |
125 |
126 | Homography operator/(const Homography& homography, const double scaling);
127 |
128 | Homography operator/(const double scaling, const Homography& homography);
129 |
130 | }
131 |
--------------------------------------------------------------------------------
/LiveVisionKit/Math/VirtualGrid.hpp:
--------------------------------------------------------------------------------
1 | // *************************** LiveVisionKit ****************************
2 | // Copyright (C) 2022 Sebastian Di Marco (crowsinc.dev@gmail.com)
3 | //
4 | // This program is free software: you can redistribute it and/or modify
5 | // it under the terms of the GNU General Public License as published by
6 | // the Free Software Foundation, either version 3 of the License, or
7 | // (at your option) any later version.
8 | //
9 | // This program is distributed in the hope that it will be useful,
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | // GNU General Public License for more details.
13 | //
14 | // You should have received a copy of the GNU General Public License
15 | // along with this program. If not, see .
16 | // **********************************************************************
17 |
18 | #pragma once
19 |
20 | #include
21 | #include
22 |
23 | namespace lvk
24 | {
25 | using SpatialKey = cv::Point_;
26 |
27 | class VirtualGrid
28 | {
29 | public:
30 |
31 | explicit VirtualGrid(const cv::Size& size);
32 |
33 | VirtualGrid(const cv::Size& size, const cv::Rect2f& alignment);
34 |
35 | VirtualGrid(const VirtualGrid& grid);
36 |
37 |
38 | void resize(const cv::Size& size);
39 |
40 | const cv::Size& size() const;
41 |
42 | int cols() const;
43 |
44 | int rows() const;
45 |
46 |
47 | void align(const cv::Rect2f& region);
48 |
49 | void align(const cv::Size& size, const cv::Rect2f& region);
50 |
51 |
52 | const cv::Rect2f& alignment() const;
53 |
54 | const cv::Size2f& key_size() const;
55 |
56 |
57 | cv::Mat make_grid() const;
58 |
59 | cv::Mat make_aligned_grid() const;
60 |
61 |
62 | bool test_key(const SpatialKey& key) const;
63 |
64 | size_t key_to_index(const SpatialKey& key) const;
65 |
66 | SpatialKey index_to_key(const size_t index) const;
67 |
68 |
69 | bool test_point(const cv::Point2f& point) const;
70 |
71 | SpatialKey key_of(const cv::Point2f& point) const;
72 |
73 | std::optional try_key_of(const cv::Point2f& point) const;
74 |
75 | // TODO: rename these
76 | cv::Point2f key_to_point(const SpatialKey& key) const;
77 |
78 | cv::Point2f index_to_point(const size_t index) const;
79 |
80 |
81 |
82 | void for_each(const std::function& operation) const;
83 |
84 | void for_each_aligned(const std::function& operation) const;
85 |
86 | private:
87 | cv::Size m_Resolution;
88 | cv::Rect2f m_Alignment;
89 | cv::Size2f m_KeySize;
90 | };
91 |
92 | }
93 |
--------------------------------------------------------------------------------
/LiveVisionKit/Timing/Stopwatch.hpp:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2022 Sebastian Di Marco (crowsinc.dev@gmail.com)
2 | //
3 | // This program is free software: you can redistribute it and/or modify
4 | // it under the terms of the GNU General Public License as published by
5 | // the Free Software Foundation, either version 3 of the License, or
6 | // (at your option) any later version.
7 | //
8 | // This program is distributed in the hope that it will be useful,
9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 | // GNU General Public License for more details.
12 | //
13 | // You should have received a copy of the GNU General Public License
14 | // along with this program. If not, see .
15 | // **********************************************************************
16 |
17 | #pragma once
18 |
19 | #include "Time.hpp"
20 | #include "Data/StreamBuffer.hpp"
21 |
22 | namespace lvk
23 | {
24 |
25 | class Stopwatch
26 | {
27 | public:
28 |
29 | explicit Stopwatch(const size_t history = 1);
30 |
31 | void start();
32 |
33 | Time stop();
34 |
35 | Time pause();
36 |
37 | Time restart();
38 |
39 |
40 | bool is_paused() const;
41 |
42 | bool is_running() const;
43 |
44 |
45 | Time wait_until(const Time& target_elapsed_time);
46 |
47 | Stopwatch& sync_gpu(const bool trigger = true);
48 |
49 |
50 | Time elapsed() const;
51 |
52 | Time average() const;
53 |
54 | Time deviation() const;
55 |
56 |
57 | void reset_history();
58 |
59 | const StreamBuffer