-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.cpp
executable file
·284 lines (235 loc) · 10.3 KB
/
stats.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include "stats.h"
#include <utility>
#include "cs221util/PNG.h"
#include "cs221util/RGBAPixel.h"
#include <numeric>
using namespace std;
using namespace cs221util;
/* returns the sums of all pixel values across the given color channels.
* useful in computing the score of a rectangle
* PA3 function
* @param channel is one of r, g, or b
* @param ul is (x,y) of the upper left corner of the rectangle
* @param lr is (x,y) of the lower right corner of the rectangle */
long stats::getSum(char channel, pair<int,int> ul, pair<int,int> lr){
//std::cout<< "start of getsum"<<endl;
vector< vector< long >> colour;
long sum = 0;
if (channel == 'r'){
if (ul.first == 0 && ul.second == 0){
sum = sumRed[lr.first][lr.second];
}
else if (ul.first > 0 && ul.second == 0){
sum = sumRed[lr.first][lr.second] - sumRed[ul.first-1][lr.second];
}
else if (ul.first == 0 && ul.second > 0){
sum = sumRed[lr.first][lr.second] - sumRed[lr.first][ul.second-1];
}
else{
sum = sumRed[lr.first][lr.second] - sumRed[lr.first][ul.second-1] -
sumRed[ul.first-1][lr.second] + sumRed[ul.first-1][ul.second-1];
}
}
else if (channel == 'g'){
if (ul.first == 0 && ul.second == 0){
sum = sumGreen[lr.first][lr.second];
}
else if (ul.first > 0 && ul.second == 0){
sum = sumGreen[lr.first][lr.second] - sumGreen[ul.first-1][lr.second];
}
else if (ul.first == 0 && ul.second > 0){
sum = sumGreen[lr.first][lr.second] - sumGreen[lr.first][ul.second-1];
}
else{
sum = sumGreen[lr.first][lr.second] - sumGreen[lr.first][ul.second-1] -
sumGreen[ul.first-1][lr.second] + sumGreen[ul.first-1][ul.second-1];
}
}
else if (channel == 'b'){
if (ul.first == 0 && ul.second == 0){
sum = sumBlue[lr.first][lr.second];
}
else if (ul.first > 0 && ul.second == 0){
sum = sumBlue[lr.first][lr.second] - sumBlue[ul.first-1][lr.second];
}
else if (ul.first == 0 && ul.second > 0){
sum = sumBlue[lr.first][lr.second] - sumBlue[lr.first][ul.second-1];
}
else{
sum = sumBlue[lr.first][lr.second] - sumBlue[lr.first][ul.second-1] -
sumBlue[ul.first-1][lr.second] + sumBlue[ul.first-1][ul.second-1];
}
}
//std::cout<< "end of getsum"<<endl;
return sum;
}
/* returns the sums of squares of all pixel values across the color channel.
* useful in computing the score of a rectangle
* PA3 function
* @param channel is one of r, g, or b
* @param ul is (x,y) of the upper left corner of the rectangle
* @param lr is (x,y) of the lower right corner of the rectangle */
long stats::getSumSq(char channel, pair<int,int> ul, pair<int,int> lr){
//std::cout<< "start of getsumsq"<<endl;
//vector< vector< long >> colour;
long sum = 0;
if (channel == 'r'){
if (ul.first == 0 && ul.second == 0){
sum = sumsqRed[lr.first][lr.second];
}
else if (ul.first > 0 && ul.second == 0){
sum = sumsqRed[lr.first][lr.second] - sumsqRed[ul.first-1][lr.second];
}
else if (ul.first == 0 && ul.second > 0){
sum = sumsqRed[lr.first][lr.second] - sumsqRed[lr.first][ul.second-1];
}
else{
sum = sumsqRed[lr.first][lr.second] - sumsqRed[lr.first][ul.second-1] -
sumsqRed[ul.first-1][lr.second] + sumsqRed[ul.first-1][ul.second-1];
}
}
else if (channel == 'g'){
if (ul.first == 0 && ul.second == 0){
sum = sumsqGreen[lr.first][lr.second];
}
else if (ul.first > 0 && ul.second == 0){
sum = sumsqGreen[lr.first][lr.second] - sumsqGreen[ul.first-1][lr.second];
}
else if (ul.first == 0 && ul.second > 0){
sum = sumsqGreen[lr.first][lr.second] - sumsqGreen[lr.first][ul.second-1];
}
else{
sum = sumsqGreen[lr.first][lr.second] - sumsqGreen[lr.first][ul.second-1] -
sumsqGreen[ul.first-1][lr.second] + sumsqGreen[ul.first-1][ul.second-1];
}
}
else if (channel == 'b'){
if (ul.first == 0 && ul.second == 0){
sum = sumsqBlue[lr.first][lr.second];
}
else if (ul.first > 0 && ul.second == 0){
sum = sumsqBlue[lr.first][lr.second] - sumsqBlue[ul.first-1][lr.second];
}
else if (ul.first == 0 && ul.second > 0){
sum = sumsqBlue[lr.first][lr.second] - sumsqBlue[lr.first][ul.second-1];
}
else{
sum = sumsqBlue[lr.first][lr.second] - sumsqBlue[lr.first][ul.second-1] -
sumsqBlue[ul.first-1][lr.second] + sumsqBlue[ul.first-1][ul.second-1];
}
}
//std::cout<< "end of getsumsq"<<endl;
return sum;
}
stats::stats(PNG & im){
for (int x = 0 ; x < (int) im.width(); ++x){
vector<long> redVectorOne;
vector<long> greenVectorOne;
vector<long> blueVectorOne;
vector<long> redVectorOneSq;
vector<long> greenVectorOneSq;
vector<long> blueVectorOneSq;
for (int y = 0; y < (int) im.height(); ++y){
RGBAPixel* temp = im.getPixel(x, y);
long sumRR = 0;
long sumGG = 0;
long sumBB = 0;
long sumRRsq = 0;
long sumGGsq = 0;
long sumBBsq = 0;
if ((x+y == 0))
{
redVectorOne.push_back(temp->r);
greenVectorOne.push_back(temp->g);
blueVectorOne.push_back(temp->b);
redVectorOneSq.push_back((temp->r*temp->r));
greenVectorOneSq.push_back((temp->g*temp->g));
blueVectorOneSq.push_back((temp->b*temp->b));
}
else if (x < 1)
{
redVectorOne.push_back(redVectorOne[y-1]+temp->r);
greenVectorOne.push_back(greenVectorOne[y-1]+temp->g);
blueVectorOne.push_back(blueVectorOne[y-1]+temp->b);
redVectorOneSq.push_back(redVectorOneSq[y-1]+(temp->r*temp->r));
greenVectorOneSq.push_back(greenVectorOneSq[y-1]+(temp->g*temp->g));
blueVectorOneSq.push_back(blueVectorOneSq[y-1]+(temp->b*temp->b));
}
else
{
if (y == 0)
{
sumRR = temp->r + sumRed[x-1][y];
redVectorOne.push_back(sumRR);
sumGG = temp->g + sumGreen[x-1][y];
greenVectorOne.push_back(sumGG);
sumBB = temp->b + sumBlue[x-1][y];
blueVectorOne.push_back(sumBB);
sumRRsq = (temp->r*temp->r) + sumsqRed[x-1][y];
redVectorOneSq.push_back(sumRRsq);
sumGGsq = (temp->g*temp->g) + sumsqGreen[x-1][y];
greenVectorOneSq.push_back(sumGGsq);
sumBBsq = (temp->b*temp->b) + sumsqBlue[x-1][y];
blueVectorOneSq.push_back(sumBBsq);
}
else
{
sumRR= sumRed[x-1][y] + redVectorOne[y-1] + temp->r - sumRed[x-1][y-1];
redVectorOne.push_back(sumRR);
sumGG= sumGreen[x-1][y] + greenVectorOne[y-1] + temp->g - sumGreen[x-1][y-1];
greenVectorOne.push_back(sumGG);
sumBB= sumBlue[x-1][y] + blueVectorOne[y-1] + temp->b - sumBlue[x-1][y-1];
blueVectorOne.push_back(sumBB);
sumRRsq= sumsqRed[x-1][y] + redVectorOneSq[y-1] + (temp->r*temp->r) - sumsqRed[x-1][y-1];
redVectorOneSq.push_back(sumRRsq);
sumGGsq= sumsqGreen[x-1][y] + greenVectorOneSq[y-1] + (temp->g*temp->g) - sumsqGreen[x-1][y-1];
greenVectorOneSq.push_back(sumGGsq);
sumBBsq= sumsqBlue[x-1][y] + blueVectorOneSq[y-1] + (temp->b*temp->b) - sumsqBlue[x-1][y-1];
blueVectorOneSq.push_back(sumBBsq);
}
}
}
sumRed.push_back(redVectorOne);
sumGreen.push_back(greenVectorOne);
sumBlue.push_back(blueVectorOne);
sumsqRed.push_back(redVectorOneSq);
sumsqGreen.push_back(greenVectorOneSq);
sumsqBlue.push_back(blueVectorOneSq);
}
}
// given a rectangle, compute its sum of squared deviations from
// mean, over all color channels. Will be used to make split when
// building tree.
/* @param ul is (x,y) of the upper left corner of the rectangle
* @param lr is (x,y) of the lower right corner of the rectangle */
long stats::getScore(pair<int,int> ul, pair<int,int> lr){
long deviationRed = getSumSq('r',ul,lr)- (getSum('r',ul,lr)*getSum('r',ul,lr))/(double)rectArea(ul,lr);
long deviationGreen = getSumSq('g',ul,lr)- (getSum('g',ul,lr)*getSum('g',ul,lr))/(double)rectArea(ul,lr);
long deviationBlue = getSumSq('b',ul,lr)- (getSum('b',ul,lr)*getSum('b',ul,lr))/(double)rectArea(ul,lr);
return deviationRed+deviationGreen+deviationBlue;
}
// given a rectangle, return the average color value over the
// rectangle as a pixel.
/* Each color component of the pixel is the average value of that
* component over the rectangle.
* @param ul is (x,y) of the upper left corner of the rectangle
* @param lr is (x,y) of the lower right corner of the rectangle */
RGBAPixel stats::getAvg(pair<int,int> ul, pair<int,int> lr){
int pixelCount = rectArea(ul,lr);
long avgRed = getSum('r',ul,lr)/pixelCount;
long avgGreen = getSum('g',ul,lr)/pixelCount;
long avgBlue = getSum('b',ul,lr)/pixelCount;
return RGBAPixel(avgRed,avgGreen,avgBlue);
}
// given a rectangle, return the number of pixels in the rectangle
/* @param ul is (x,y) of the upper left corner of the rectangle
* @param lr is (x,y) of the lower right corner of the rectangle */
long stats::rectArea(pair<int,int> ul, pair<int,int> lr){
long area=1;
if (ul.first == lr.first && ul.second == lr.second){
area = 1;
}
else if(lr.first >= ul.first && lr.second >= lr.first){
area = (lr.first - ul.first+1)*(lr.second - ul.second +1);}
return area;
}