aboutsummaryrefslogtreecommitdiff
path: root/xsig/src/imageutil.hpp
blob: f3831b074c318d8c50edd023e244ff156495d19a (plain) (blame)
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
//Chris Xiong 2022
//License: MPL-2.0
#ifndef IMAGEUTIL_HPP
#define IMAGEUTIL_HPP

#include <cmath>
#include <cstdlib>
#include <filesystem>
#include <vector>
#include <opencv2/core.hpp>

#include "compressed_vector.hpp"

#define sqr(x) ((x) * (x))

namespace image_util
{
    cv::Mat crop(cv::InputArray s, double contrast_threshold, double max_crop_ratio);
    cv::Range crop_axis(cv::InputArray s, int axis, double contrast_threshold, double max_crop_ratio);
    double median(std::vector<double> &v);
    cv::Mat blend_white(cv::Mat m);
    cv::Mat imread_path(const std::filesystem::path &p, int flags);

    template<class T, int B>
    static double length(const compressed_vector<T, B> &v, T center)
    {
        double ret = 0;
        for (size_t i = 0; i < v.size(); ++i)
        {
            ret += sqr(1. * v.get(i) - center);
        }
        return sqrt(ret);
    }
    template<class T, int B>
    static double distance(const compressed_vector<T, B> &v1, const compressed_vector<T, B> &v2)
    {
        //assert(v1.size() == v2.size())
        double ret = 0;
        for (size_t i = 0; i < v1.size(); ++i)
        {
            if (abs((int)v1.get(i) - (int)v2.get(i)) == 2 && (v1.get(i) == 2 || v2.get(i) == 2))
            ret += 9;
            else
            ret += sqr(1. * v1.get(i) - v2.get(i));
        }
        return sqrt(ret);
    }
    static double length(const std::vector<uint8_t> &v, uint8_t center)
    {
        double ret = 0;
        for (size_t i = 0; i < v.size(); ++i)
        {
            ret += sqr(1. * v[i] - center);
        }
        return sqrt(ret);
    }
    static double distance(const std::vector<uint8_t> &v1, const std::vector<uint8_t> &v2)
    {
        //assert(v1.size() == v2.size())
        double ret = 0;
        for (size_t i = 0; i < v1.size(); ++i)
        {
            if (abs((int)v1[i] - (int)v2[i]) == 2 && (v1[i] == 2 || v2[i] == 2))
            ret += 9;
            else
            ret += sqr(1. * v1[i] - v2[i]);
        }
        return sqrt(ret);
    }
};

#endif