-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutility.hpp
126 lines (106 loc) · 3.08 KB
/
utility.hpp
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
/*
* file: utility.hpp
* purpose: Holds functions for doing various small manipulations, hiding the backend details.
*/
#pragma once
#include <cstdint>
#include <opencv2/core/types.hpp>
#include <vector>
namespace cv
{
class Mat;
} // namespace cv
enum GrayLevel : uint8_t
{
LOW = 0,
MIDDLE = 1,
HIGH = 2,
};
struct Rectangle
{
unsigned int x;
unsigned int y;
unsigned int width;
unsigned int height;
Rectangle(unsigned int _x, unsigned int _y, unsigned int _width, unsigned int _height)
: x(_x), y(_y), width(_width), height(_height)
{}
};
struct ImageHistogram
{
std::vector<unsigned int> histogram;
explicit ImageHistogram()
: histogram(256, 0)
{
// Empty
}
friend GrayLevel classifyGrayLevel(ImageHistogram const & histogram);
friend void clipHistogram(ImageHistogram & histogram, double clipLimit);
inline unsigned int operator[](unsigned int index) const noexcept
{
return histogram[index];
}
unsigned int max() const
{
return *std::max_element(histogram.cbegin(), histogram.cend());
}
};
struct Pixel
{
unsigned int x;
unsigned int y;
unsigned int intensity;
Pixel(unsigned int _x, unsigned int _y, unsigned int _intensity) : x(_x), y(_y), intensity(_intensity)
{
// Empty
}
bool operator<(Pixel const & rhs) const
{
if (this->x < rhs.x)
{
return true;
}
else
{
return this->y < rhs.y;
}
}
};
/*
* Generates the pixel intensity histogram for a grayscale image.
*
* image: An OpenCV matrix containing a grayscale image.
* outputHistogram: Structure in which the output is to be populated.
*/
int generateGrayscaleHistogram(cv::Mat const & image, ImageHistogram & outputHistogram);
/*
* Generates the pixel intensity histogram for a subregion of a grayscale image.
*
* image: An OpenCV matrix containing a grayscale image.
* region: The subimage over which to create the histogram from.
*/
ImageHistogram generateGrayscaleHistogramForSubregion(cv::Mat const & image, Rectangle const & region);
/*
* Classifies the image into one of three categories based on where the highest
* number of gray scale intensities falls.
*
* Based on gray level definition of Youlian Zhu and Cheng Huang in "An Adaptive
* Histogram Equalization Algorithm on the Image Gray Level Mapping".
*/
GrayLevel classifyGrayLevel(ImageHistogram const & histogram);
/*
* Interpolates the value of a pixel based on it's linear distance in two
* dimensions from four pixels.
*/
Pixel bilinearInterpolate(std::vector<Pixel> & pixels, float outX, float outY);
/*
* Interpolates the value of a pixel based on it's linear distance in one
* dimension from two pixels.
*/
Pixel linearInterpolate(Pixel pixel0, Pixel pixel1, float outX, float outY);
/*
* Finds all bins of the histogram with a quantity over the clip limit and
* removes the excess. The number of excess is added as equally as possible to
* all bins in the histogram.
*/
void clipHistogram(ImageHistogram & histogram, double clipLimit);