/
Statistics.cc
241 lines (219 loc) · 11.5 KB
/
Statistics.cc
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
// __BEGIN_LICENSE__
// Copyright (c) 2006-2012, United States Government as represented by the
// Administrator of the National Aeronautics and Space Administration. All
// rights reserved.
//
// The NASA Vision Workbench is licensed under the Apache License,
// Version 2.0 (the "License"); you may not use this file except in
// compliance with the License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// __END_LICENSE__
#include <vw/GPU/Statistics.h>
#include <vw/GPU/Algorithms.h>
#include <vw/GPU/Manipulation.h>
#include <vw/GPU/ImageMath.h>
#include <cmath>
//#include <vw/Math.h>
//#include <vw/Geometry/VectorFixed.h>
//#include <vw/Geometry/PointND.h>
namespace vw { namespace GPU {
#define MIN(n1, n2) n1 < n2 ? n1 : n2
#define MAX(n1, n2) n1 > n2 ? n1 : n2
// *******************************************************************
// min_channel_value()
// *******************************************************************
float min_channel_value(const GPUImageBase& image)
{
// Output Image Size
int POT_Level = MAX((int) ceilf(log2f(image.width())), (int) ceilf(log2f(image.height())));
int POT_Dimension = (int) powf(2.0, POT_Level);
// Reduce to 1 Channell; pad to POT dimensions; fill padded area with high value
GPUImageBase tex_StartLevel(MAX(1, POT_Dimension), MAX(1, POT_Dimension), GPU_RED, image.type());
GPUProgram* program_Level1;
if(image.num_channels() == 4)
program_Level1 = create_gpu_program("Statistics/min-channels-rgba");
else if(image.num_channels() == 3)
program_Level1 = create_gpu_program("Statistics/min-channels-rgb");
if(image.num_channels() == 1)
program_Level1 = create_gpu_program("Statistics/min-channels-r");
program_Level1->install();
((GPUImageBase&) image).rasterize_homography();
ShaderInvocation_SetupGLState(tex_StartLevel);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, tex_StartLevel.target(), tex_StartLevel.name(), 0);
program_Level1->set_input_image("image", image);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
int draw_size = image.width();
ShaderInvocation_DrawRectOneTexture(image, Rectangle2D<int>(0, 0, image.width(), image.height()),
Rectangle2D<int>(0, 0, image.width(), image.height()));
program_Level1->uninstall();
// Fill edge extend area
float high_value = 10000000.0;
int xEdgeSize = tex_StartLevel.width() - image.width();
int yEdgeSize = tex_StartLevel.height() - image.height();
GPUImageBase tex_Cropped;
if(xEdgeSize) {
fill(tex_StartLevel, high_value, 0, 0, 0, Rectangle2D<int>(image.width(), 0, xEdgeSize, tex_StartLevel.height()));
}
if(yEdgeSize) {
fill(tex_StartLevel, high_value, 0, 0, 0, Rectangle2D<int>(0, image.height(), image.width(), yEdgeSize));
}
// ITERATIONS: 1 to n
GPUImageBase tex_PreviousLevel = tex_StartLevel;
float output;
GPUProgram* program = create_gpu_program("Statistics/min-quad");
for(int iLevel = 1; iLevel <= POT_Level; iLevel++) {
program->install();
int reductionRatio = (int) powf(2.0, iLevel);
GPUImageBase tex_CurrentLevel(MAX(1, POT_Dimension / reductionRatio), MAX(1, POT_Dimension / reductionRatio), GPU_RED, image.type());
ShaderInvocation_SetupGLState(tex_CurrentLevel);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, tex_CurrentLevel.target(), tex_CurrentLevel.name(), 0);
program->set_input_image("image", tex_PreviousLevel);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP);
ShaderInvocation_DrawRectOneTexture(tex_PreviousLevel, Vector2(0, 0), Vector2(tex_CurrentLevel.width(), tex_CurrentLevel.height()),
Vector2(-0.5, -0.5), Vector2((tex_CurrentLevel.width() * 2) - 0.5, (tex_CurrentLevel.height() * 2) - 0.5));
tex_PreviousLevel = tex_CurrentLevel;
}
program->uninstall();
// CleanUp State
ShaderInvocation_CleanupGLState();
tex_PreviousLevel.read(GPU_RED, GPU_FLOAT32, &output);
return output;
}
// *******************************************************************
// max_channel_value()
// *******************************************************************
float max_channel_value(const GPUImageBase& image)
{
// Output Image Size
int POT_Level = MAX((int) ceilf(log2f(image.width())), (int) ceilf(log2f(image.height())));
int POT_Dimension = (int) powf(2.0, POT_Level);
// GLState - Setup
((GPUImageBase&) image).rasterize_homography();
ShaderInvocation_SetupGLState(POT_Dimension, POT_Dimension);
// Reduce to 1 Channell; pad to POT dimensions; fill padded area with high value
GPUProgram* program_Level1;
if(image.num_channels() == 4)
program_Level1 = create_gpu_program("Statistics/max-channels-rgba");
else if(image.num_channels() == 3)
program_Level1 = create_gpu_program("Statistics/max-channels-rgb");
if(image.num_channels() == 1)
program_Level1 = create_gpu_program("Statistics/max-channels-r");
program_Level1->install();
GPUImageBase tex_StartLevel(MAX(1, POT_Dimension), MAX(1, POT_Dimension), GPU_RED, image.type());
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, tex_StartLevel.target(), tex_StartLevel.name(), 0);
program_Level1->set_input_image("image", image);
ShaderInvocation_DrawRectOneTexture(image, Rectangle2D<int>(0, 0, tex_StartLevel.width(), tex_StartLevel.height()),
Rectangle2D<int>(0, 0, tex_StartLevel.width(), tex_StartLevel.height()));
program_Level1->uninstall();
// Fill edge extend area
float pad_value = -10000000.0;
int xEdgeSize = tex_StartLevel.width() - image.width();
int yEdgeSize = tex_StartLevel.height() - image.height();
GPUImageBase tex_Cropped;
if(xEdgeSize) {
fill(tex_StartLevel, pad_value, 0, 0, 0, Rectangle2D<int>(image.width(), 0, xEdgeSize, tex_StartLevel.height()));
}
if(yEdgeSize) {
fill(tex_StartLevel, pad_value, 0, 0, 0, Rectangle2D<int>(0, image.height(), image.width(), yEdgeSize));
}
// ITERATIONS: 1 to n
GPUImageBase tex_PreviousLevel = tex_StartLevel;
GPUProgram* program = create_gpu_program("Statistics/max-quad");
for(int iLevel = 1; iLevel <= POT_Level; iLevel++) {
program->install();
int reductionRatio = (int) powf(2.0, iLevel);
GPUImageBase tex_CurrentLevel(MAX(1, POT_Dimension / reductionRatio), MAX(1, POT_Dimension / reductionRatio), GPU_RED, image.type());
ShaderInvocation_SetupGLState(tex_CurrentLevel);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, tex_CurrentLevel.target(), tex_CurrentLevel.name(), 0);
program->set_input_image("image", tex_PreviousLevel);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP);
ShaderInvocation_DrawRectOneTexture(tex_PreviousLevel, Vector2(0, 0), Vector2(tex_CurrentLevel.width(), tex_CurrentLevel.height()),
Vector2(-0.5, -0.5), Vector2((tex_CurrentLevel.width() * 2) - 0.5, (tex_CurrentLevel.height() * 2) - 0.5));
tex_PreviousLevel = tex_CurrentLevel;
}
program->uninstall();
// CleanUp State
ShaderInvocation_CleanupGLState();
float output;
tex_PreviousLevel.read(GPU_RED, GPU_FLOAT32, &output);
return output;
}
// *******************************************************************
// sum_channel_value()
// *******************************************************************
float sum_channel_value(const GPUImageBase& image)
{
// Output Image Size
int POT_Level = MAX((int) ceilf(log2f(image.width())), (int) ceilf(log2f(image.height())));
int POT_Dimension = (int) powf(2.0, POT_Level);
// GLState - Setup
((GPUImageBase&) image).rasterize_homography();
ShaderInvocation_SetupGLState(POT_Dimension, POT_Dimension);
// Reduce to 1 Channell; pad to POT dimensions; fill padded area with high value
GPUProgram* program_Level1;
if(image.num_channels() == 4)
program_Level1 = create_gpu_program("Statistics/sum-channels-rgba");
else if(image.num_channels() == 3)
program_Level1 = create_gpu_program("Statistics/sum-channels-rgb");
if(image.num_channels() == 1)
program_Level1 = create_gpu_program("Statistics/sum-channels-r");
program_Level1->install();
GPUImageBase tex_StartLevel(MAX(1, POT_Dimension), MAX(1, POT_Dimension), GPU_RED, image.type());
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, tex_StartLevel.target(), tex_StartLevel.name(), 0);
program_Level1->set_input_image("image", image);
ShaderInvocation_DrawRectOneTexture(image, Rectangle2D<int>(0, 0, tex_StartLevel.width(), tex_StartLevel.height()),
Rectangle2D<int>(0, 0, tex_StartLevel.width(), tex_StartLevel.height()));
program_Level1->uninstall();
// Fill edge extend area
float pad_value = 0.0;
int xEdgeSize = tex_StartLevel.width() - image.width();
int yEdgeSize = tex_StartLevel.height() - image.height();
GPUImageBase tex_Cropped;
if(xEdgeSize) {
fill(tex_StartLevel, pad_value, 0, 0, 0, Rectangle2D<int>(image.width(), 0, xEdgeSize, tex_StartLevel.height()));
}
if(yEdgeSize) {
fill(tex_StartLevel, pad_value, 0, 0, 0, Rectangle2D<int>(0, image.height(), image.width(), yEdgeSize));
}
// ITERATIONS: 1 to n
GPUImageBase tex_PreviousLevel = tex_StartLevel;
GPUProgram* program = create_gpu_program("Statistics/sum-quad");
for(int iLevel = 1; iLevel <= POT_Level; iLevel++) {
program->install();
int reductionRatio = (int) powf(2.0, iLevel);
GPUImageBase tex_CurrentLevel(MAX(1, POT_Dimension / reductionRatio), MAX(1, POT_Dimension / reductionRatio), GPU_RED, image.type());
ShaderInvocation_SetupGLState(tex_CurrentLevel);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, tex_CurrentLevel.target(), tex_CurrentLevel.name(), 0);
program->set_input_image("image", tex_PreviousLevel);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP);
ShaderInvocation_DrawRectOneTexture(tex_PreviousLevel, Vector2(0, 0), Vector2(tex_CurrentLevel.width(), tex_CurrentLevel.height()),
Vector2(-0.5, -0.5), Vector2((tex_CurrentLevel.width() * 2) - 0.5, (tex_CurrentLevel.height() * 2) - 0.5));
tex_PreviousLevel = tex_CurrentLevel;
}
program->uninstall();
// CleanUp State
ShaderInvocation_CleanupGLState();
PixelGray<float> output_pixel = ((GPUImage<PixelGray<float> >&) tex_PreviousLevel)(0,0);
return output_pixel.v();
}
// *******************************************************************
// stddev_channel_value()
// *******************************************************************
float stddev_channel_value(const GPUImageBase& image)
{
float mean = mean_channel_value(image);
float sum = sum_channel_value(pow(image - mean, 2.0));
return sqrtf(sum) / (image.width() * image.height() * image.num_channels() - 1);
}
} // namespace GPU
} // namespace vw