-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHistogram.cpp
143 lines (128 loc) · 3.91 KB
/
Histogram.cpp
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* This file is part of Tomato Analyzer.
Tomato Analyzer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Tomato Analyzer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Tomato Analyzer. If not, see <http://www.gnu.org/licenses/>. */
// Histogram.cpp: implementation of the CHistogram class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "resource.h"
#include "AdvImage.h"
#include "Histogram.h"
#include <string.h>
#include <assert.h>
#include <math.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
/**
* Builds a histogram of L* values of pixels within the given rectangle of an image, where
* r, g, and b are the red, green, and blue bands of the image.
*/
CHistogram::CHistogram(CRect rect, std::vector< std::vector<double> > lum)
{
CPoint tl = rect.TopLeft();
CPoint lr = rect.BottomRight();
hist.resize(101, 0); // L* ranges from 0 through 100
for(int i = tl.y; i < lr.y; i++)
{
for(int j = tl.x; j < lr.x; j++)
{
int roundedLum = (int) (lum[i][j] + 0.5);
hist[roundedLum]++;
}
}
}
/**
* Returns the average of histogram elements in index range [start, end),
* adjusting start and end if necessary to keep them within bounds.
* Returns 0 if there are no histogram elements in range.
*/
float CHistogram::avgInRange(int start, int end) {
if (start < 0)
start = 0;
if (end > hist.size())
end = hist.size();
float sum = 0;
for (int i = start; i < end; i++) {
sum += hist[i];
}
int range = end - start;
return range > 0 ? sum/range : 0;
}
/**
* Returns the average of L* values in index range [start, end),
* adjusting start and end if necessary to keep them within bounds.
* Returns 0 if there are no histogram elements in range.
*/
float CHistogram::avgLValueInRange(int start, int end) {
if (start < 0)
start = 0;
if (end > hist.size())
end = hist.size();
float sum = 0;
int count = 0;
for (int i = start; i < end; i++) {
sum += hist[i] * i;
count += hist[i];
}
return count > 0 ? sum/count : 0;
}
/**
* Returns the index of the minimum histogram element in index range [start, end),
* adjusting start and end if necessary to keep them within bounds.
* Returns 0 if there are no histogram elements in range.
*/
int CHistogram::minIndexInRange(int start, int end) {
if (start < 0)
start = 0;
if (end > hist.size())
end = hist.size();
if (end - start > 0) {
int min = start;
for (int i = start + 1; i < end; i++) {
if (hist[i] < hist[min])
min = i;
}
return min;
} else {
return 0;
}
}
/**
* Returns the smallest index where the histogram element is greater than 0,
* or the last index + 1 if all elements are 0.
*/
int CHistogram::firstNonZeroIndex() {
for (int i = 0; i < hist.size(); i++) {
if (hist[i] > 0)
return i;
}
return hist.size();
}
/**
* Returns the largest index where the histogram element is greater than 0,
* or -1 if all elements are 0.
*/
int CHistogram::lastNonZeroIndex() {
for (int i = hist.size() - 1; i >= 0; i--) {
if (hist[i] > 0)
return i;
}
return -1;
}
int CHistogram::operator[](int i) {
return hist[i];
}