-
Notifications
You must be signed in to change notification settings - Fork 4
/
gridoperators.h
139 lines (119 loc) · 4.04 KB
/
gridoperators.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
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
/* gridoperators.h is part of gpumatting and is
* Copyright 2013 Philip G. Lee <[email protected]>
*
* gpumatting 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.
*
* gpumatting 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 gpumatting. If not, see <http://www.gnu.org/licenses/>.
*/
void downsampleOps(..., float const* im, int imW, int imH, int const* labels, int nlabels, int nlevels)
{
// Coordinate matrix representation of a downsample operator.
int* i;
int* j;
int* k;
int numpix = imW*imH;
int u,v;
int curLabel;
int prevNlabels;
float tmp;
float min;
int minU, minV;
int *__restrict__ mapping = (int*)malloc(nlabels*sizeof(int));
int *__restrict__ merged = (int*)malloc(nlabels*sizeof(int));
float *__restrict__ diffs = (float*)malloc(nlabels*nlabels*sizeof(float));
float *__restrict__ means = (float*)malloc(3*nlabels*sizeof(float));
int *__restrict__ segSize = (int*)malloc(nlabels*sizeof(int));
memset(segSize, 0x00, nlabels*sizeof(int));
...
// First level--------------------------------------------------------------
memset(means, 0x00, 3*nlabels*sizeof(float));
for( u = 0; u < numpix; ++u )
{
means[3*labels[u]+0] += im[4*u+0];
means[3*labels[u]+1] += im[4*u+1];
means[3*labels[u]+2] += im[4*u+2];
++segSize[labels[u]];
i[u] = labels[u];
j[u] = u;
k[u] = 1.0f;
}
for( u = 0; u < nlabels; ++u )
means[3*u+0] /= segSize[u]; means[3*u+1] /= segSize[u]; means[3*u+2] /= segSize[u];
...
// Subsequent levels--------------------------------------------------------
for( --nlevels; nlevels > 0; --nlevels )
{
prevNlabels = nlabels;
curLabel = 0;
memset(merged, 0x00, nlabels*sizeof(int));
// diffs[u][v] = ||means[u]-means[v]||_2^2
min = 1e6;
for( u = 0; u < prevNlabels; ++u )
{
for( v = u+1, v < prevNlabels; ++v )
{
tmp = (means[3*u+0]-means[3*v+0]);
tmp *= tmp;
diffs[v + u * nlabels] = tmp;
tmp = (means[3*u+1]-means[3*v+1]);
tmp *= tmp;
diffs[v + u * nlabels] += tmp;
tmp = (means[3*u+2]-means[3*v+2]);
tmp *= tmp;
diffs[v + u * nlabels] += tmp;
}
}
while( true )
{
min = 1e6;
for( u = 0; u < prevNlabels; ++u )
{
if( merged[u] )
continue;
for( v = u+1, v < prevNlabels; ++v )
{
if( merged[v] )
continue;
// NOTE: need to check here that segments u and v are spatially adjacent.
// Get location of minimum difference.
if( diffs[v + u * nlabels] < min )
{
min = diffs[v + u * nlabels]
minU = u;
minV = v;
}
}
}
// This condition means exp( -||mean[u]-mean[v]||_2 ) < 0.90.
if( min > 0.0111f )
break;
merged[minU] = 1;
merged[minV] = 1;
mapping[minU] = curLabel;
mapping[minV] = curLabel;
++curLabel;
}
// Finish the mapping.
for( u = 0; u < prevNlabels; ++u )
{
if( merged[u] )
i[u] = mapping[u];
else
i[u] = curLabel++;
j[u] = u;
k[u] = 1.0f;
}
...
// NOTE: need to update means[] here.
prevNlabels = curLabel;
}
}