diff options
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | imageutil.hpp | 2 | ||||
-rw-r--r-- | subslice_signature.cpp | 62 | ||||
-rw-r--r-- | subslice_signature.hpp | 32 | ||||
-rw-r--r-- | tests/signature_test.cpp | 41 |
5 files changed, 132 insertions, 7 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 5025d31..6249575 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,6 @@ include_directories(${OpenCV_INCLUDE_DIRS}) include_directories(.) -add_library(xsig STATIC imageutil.cpp signature.cpp) +add_library(xsig STATIC imageutil.cpp signature.cpp subslice_signature.cpp) add_subdirectory(tests) diff --git a/imageutil.hpp b/imageutil.hpp index 4a3e041..5c01d9b 100644 --- a/imageutil.hpp +++ b/imageutil.hpp @@ -12,8 +12,6 @@ #define sqr(x) ((x) * (x)) - - namespace image_util { cv::Mat crop(cv::InputArray s, double contrast_threshold, double max_crop_ratio); diff --git a/subslice_signature.cpp b/subslice_signature.cpp new file mode 100644 index 0000000..75b1a43 --- /dev/null +++ b/subslice_signature.cpp @@ -0,0 +1,62 @@ +//Chris Xiong 2022 +//License: MPL-2.0 +#include "subslice_signature.hpp" + +#include <opencv2/core.hpp> +#include <opencv2/imgcodecs.hpp> +#include <opencv2/imgproc.hpp> + +#include "imageutil.hpp" + +subsliced_signature subsliced_signature::from_path(const std::filesystem::path &path, + size_t nhslices, size_t nvslices, + const signature_config &fcfg, + const signature_config &scfg) +{ + cv::Mat img = image_util::imread_path(path, cv::IMREAD_UNCHANGED); + return subsliced_signature::from_cvmatrix(&img, nhslices, nvslices, fcfg, scfg); +} + +subsliced_signature subsliced_signature::from_file(const char *fn, + size_t nhslices, size_t nvslices, + const signature_config &fcfg, + const signature_config &scfg) +{ + cv::Mat img = cv::imread(fn, cv::IMREAD_UNCHANGED); + return subsliced_signature::from_cvmatrix(&img, nhslices, nvslices, fcfg, scfg); +} + +subsliced_signature subsliced_signature::from_cvmatrix(cv::Mat *m, + size_t nhslices, size_t nvslices, + const signature_config &fcfg, + const signature_config &scfg) +{ + subsliced_signature ret; + ret.full = signature::from_cvmatrix(m, fcfg); + cv::Mat *sm = m; + if (m->size().width / nhslices > 100 || m->size().height / nvslices > 100) + { + double sc = 100. * nhslices / m->size().width; + if (100. * nvslices / m->size().height < sc) + sc = 100. * nvslices / m->size().height; + sm = new cv::Mat(); + cv::resize(*m, *sm, cv::Size(), sc, sc, cv::InterpolationFlags::INTER_LINEAR); + } + ret.nhslices = nhslices; + ret.nvslices = nvslices; + int ssw = sm->size().width / nhslices; + int ssh = sm->size().height / nvslices; + for (int i = 0; i < nhslices; ++i) + for (int j = 0; j < nvslices; ++j) + { + int l = i * ssw; + int r = (i == nhslices) ? sm->size().width : (i + 1) * ssw; + int t = j * ssh; + int b = (j == nvslices) ? sm->size().height : (j + 1) * ssh; + cv::Mat slice = (*sm)(cv::Range(t, b), cv::Range(l, r)); + ret.subslices.push_back(std::move(signature::from_cvmatrix(&slice, scfg))); + } + if (sm != m) + delete sm; + return ret; +} diff --git a/subslice_signature.hpp b/subslice_signature.hpp new file mode 100644 index 0000000..bf3822f --- /dev/null +++ b/subslice_signature.hpp @@ -0,0 +1,32 @@ +//Chris Xiong 2022 +//License: MPL-2.0 +#include "signature.hpp" + +#include <vector> +#include <filesystem> + +namespace cv +{ + class Mat; +}; + +class subsliced_signature +{ +public: + signature full; + std::vector<signature> subslices; + size_t nhslices, nvslices; + + static subsliced_signature from_path(const std::filesystem::path &path, + size_t nhslices, size_t nvslices, + const signature_config &fcfg, + const signature_config &scfg); + static subsliced_signature from_file(const char *fn, + size_t nhslices, size_t nvslices, + const signature_config &fcfg, + const signature_config &scfg); + static subsliced_signature from_cvmatrix(cv::Mat *m, + size_t nhslices, size_t nvslices, + const signature_config &fcfg, + const signature_config &scfg); +}; diff --git a/tests/signature_test.cpp b/tests/signature_test.cpp index 32a763c..df22b0f 100644 --- a/tests/signature_test.cpp +++ b/tests/signature_test.cpp @@ -1,18 +1,51 @@ #include <cstdio> #include <vector> +#include "subslice_signature.hpp" #include "signature.hpp" -//#include <opencv2/highgui.hpp> + +signature_config cfg_full = +{ + 9, //slices + 3, //blur_window + 2, //min_window + true, //crop + true, //comp + 0.5, //pr + 1./128, //noise_threshold + 0.05, //contrast_threshold + 0.25 //max_cropping +}; + +signature_config cfg_subslice = +{ + 4, //slices + 16, //blur_window + 2, //min_window + false, //crop + true, //comp + 0.5, //pr + 1./64, //noise_threshold + 0.05, //contrast_threshold + 0.25 //max_cropping +}; + int main(int argc, char **argv) { - std::vector<signature> a; + std::vector<subsliced_signature> a; for (int i = 1; i < argc; ++i) - a.push_back(std::move(signature::from_file(argv[i], signature::default_cfg()))); + a.push_back(std::move(subsliced_signature::from_file(argv[i], 3, 3, cfg_full, cfg_subslice))); + for (auto& ss : a) + { + for (auto& s : ss.subslices) + printf("%lu ", signature_hash{}(s)); + puts(""); + } if (a.size() < 2) return 0; for (size_t i = 0; i < a.size(); ++i) for (size_t j = i + 1; j < a.size(); ++j) { printf("%lu <-> %lu:", i, j); - double d = a[i].distance(a[j]); + double d = a[i].full.distance(a[j].full); printf("%f\n", d); } //while (cv::waitKey(0) != 'q'); |