-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.h
109 lines (82 loc) · 2.84 KB
/
image.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
#ifndef IMAGE_H
#define IMAGE_H
#include <cmath>
#include <cstring>
#include "uvector.h"
class Image
{
public:
typedef double* iterator;
typedef const double* const_iterator;
Image() : _data(), _width(0), _height(0) { }
Image(size_t width, size_t height);
Image(size_t width, size_t height, double initialValue);
~Image();
Image(const Image&) = default;
Image& operator=(const Image&) = default;
Image& operator=(double value);
Image(Image&& source) = default;
Image& operator=(Image&& source) = default;
double* data() { return _data.data(); }
const double* data() const { return _data.data(); }
size_t Width() const { return _width; }
size_t Height() const { return _height; }
size_t size() const { return _width * _height; }
bool empty() const { return _width == 0 || _height == 0; }
iterator begin() { return _data.begin(); }
const_iterator begin() const { return _data.begin(); }
iterator end() { return _data.end(); }
const_iterator end() const { return _data.end(); }
const double& operator[](size_t index) const { return _data[index]; }
double& operator[](size_t index) { return _data[index]; }
Image& operator*=(double factor);
Image& operator*=(const Image& other);
Image& operator/=(double factor)
{ return (*this) *= 1.0/factor; }
void reset();
/** Cut-off the borders of an image.
* @param outWidth Should be <= inWidth.
* @param outHeight Should be <= inHeight.
*/
static void Trim(double* output, size_t outWidth, size_t outHeight, const double* input, size_t inWidth, size_t inHeight);
template<typename T>
static void TrimBox(T* output, size_t x1, size_t y1, size_t boxWidth, size_t boxHeight, const T* input, size_t inWidth, size_t inHeight);
/** Extend an image with zeros, complement of Trim.
* @param outWidth Should be >= inWidth.
* @param outHeight Should be >= inHeight.
*/
static void Untrim(double* output, size_t outWidth, size_t outHeight, const double* input, size_t inWidth, size_t inHeight);
static double Median(const double* data, size_t size)
{
ao::uvector<double> copy;
return median_with_copy(data, size, copy);
}
static double MAD(const double* data, size_t size);
double Sum() const;
double Average() const;
double Min() const;
double Max() const;
double StdDevFromMAD() const { return StdDevFromMAD(_data.data(), _data.size()); }
static double StdDevFromMAD(const double* data, size_t size)
{
// norminv(0.75) x MAD
return 1.48260221850560 * MAD(data, size);
}
static double RMS(const double* data, size_t size)
{
double sum = 0.0;
for(size_t i=0; i!=size; ++i)
sum += data[i]*data[i];
return sqrt(sum/size);
}
void Negate()
{
for(double& d : *this)
d = -d;
}
private:
ao::uvector<double> _data;
size_t _width, _height;
static double median_with_copy(const double* data, size_t size, ao::uvector<double>& copy);
};
#endif