-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAH_RockModel.c
209 lines (181 loc) · 4.57 KB
/
AH_RockModel.c
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
/*******************************************************************************
AH_RockModel.c
Author:
Andres Huertas - Jet Propulsion Laboratory
Sponsor:
Mars Technology Program (AEDL) - Yang Cheng, PI
History:
01/23/06: A. Huertas. Created from original version in Java.
Usage:
Module to AH_Rocks.c
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include "AH_RockModel.h"
typedef struct {
long id;
float area;
long xloc;
long yloc;
float length;
float width;
float angle;
float perimeter;
float height;
} Rock;
double PI = 3.1415926535897;
double HALFPI = 1.5707963267949;
int npoints = 0, nfrags = 0, shape = CIRCULAR;
int xpoints[maxPoints];
int ypoints[maxPoints];
double xCenter, yCenter, xI, yI;
double major, minor, angle, theta;
int bitCount=0, pixelCount = 0;
double xsum, ysum, x2sum, y2sum, xysum;
double txsum, tysum, tx2sum, ty2sum, txysum;
int left, top, width, height, xleft, xrite; //BB of shadow
double nn, xm, ym, pw, ph;
double u20, u02, u11; // central moments
int BBt, BBb, BBl, BBr, rockPer; // bounding box of rock model
int xStart, yStart, intSamples=0;
double aveInt = 0.0; // for intensity stats later
Rock rockFrag[maxRocks];
// A few utils:
double deg2rad(double xDegrees) { return xDegrees*(PI/180.0); }
double sqr(double x) { return x*x; }
double getMag(double x1, double y1, double x2, double y2) {
return sqrt(sqr(x2-x1)+sqr(y2-y1));
}
int LabelConnectedRegions(unsigned char *B, int rows, int cols, unsigned short *CC, int* numRegions)
{
*numRegions = 0;
int r,c,l,kmax,kmin;
int nc = cols;
int nr = rows;
int maxNumBlobs = nc*nr;
int maxValidLabel = (int)pow(2, 17)-1;
int *h = (int *)malloc(sizeof(int)*maxNumBlobs); // label equivalency
for (int k = 1; k < maxNumBlobs; k++)
{
h[k] = k;
}
int k = 0;
//intialize labels
int imagesize = cols * rows;
for (int i = 0; i < imagesize; ++i)
{
CC[i] = 0;
}
//first label borders
// top left corner
if (B[0*cols+0]>0)
{
k++;
if (k > maxValidLabel)
{
return 0; //FAIL, to many labels for short
}
CC[cols*0+0] = (unsigned short)k;
}
// top
for(c=1;c<nc;c++) if (B[cols*0+c]>0)
{
if (B[0*cols +c-1]==0)
{
// new region
k++;
if (k > maxValidLabel)
{
return 0; //FAIL, to many labels for short
}
CC[0*cols+c] = (unsigned short)k;
}
else
{
CC[0 * cols + c] = CC[cols * 0 + c - 1];
}
}
// left
for(r=1;r<nr;r++) if (B[cols*r+0]>0)
{
if (B[(r-1)*cols+0]==0)
{
// new region
k++;
if (k > maxValidLabel)
{
return 0; //FAIL, to many labels for short
}
CC[r*cols+0] = (unsigned short)k;
}
else
{
CC[r * cols + 0] = CC[cols * (r - 1) + 0];
}
}
// initial assignment;
for(r=1;r<nr;r++)
for(c=1;c<nc;c++) {
if (B[r*cols+c]==0) CC[r*cols+c]=0;
else {
if ((B[(r-1)*cols+c]==0)&&(B[r*cols + c-1]==0)) {
k++;
if (k > maxValidLabel)
{
return 0; //FAIL, to many labels for short
}
CC[r*cols +c] = (unsigned short)k;
if (k>=maxNumBlobs) printf("More blobs than expected");
}
else {
if (B[r*cols + c-1]==0) CC[r*cols +c] = CC[(r-1)*cols+c];
else {
if (B[(r-1)*cols +c]==0) CC[r*cols + c] = CC[r*cols + c-1];
else {
if (CC[(r-1)*cols +c]==CC[r*cols +c-1]) CC[r*cols +c] = CC[(r-1)*cols +c];
else {
CC[r*cols +c] = CC[(r-1)*cols + c];
kmin = MIN(h[CC[(r-1)*cols + c]],h[CC[r*cols +c-1]]); // reassign works
kmax = MAX(h[CC[(r-1)*cols + c]],h[CC[r*cols + c-1]]); // swapping did not
for(int i=kmin;i<=k;i++) if (h[i] == kmax) h[i] = kmin;
}
}
}
}
}
}
// pack labels
l = 1;
for(int p=1;p<=k;p++) {
if (h[p]==p) {
h[p] = l;
l++;
if (l > maxValidLabel)
{
return 0; //FAIL, to many labels for short
}
}
else
{
h[p] = h[h[p]];
}
}
// label image
for (r = 0; r < nr; r++)
{
for (c = 0; c < nc; c++)
{
if (B[r * cols + c] > 0)
{
int newLabel = h[CC[r * cols + c]];
assert(newLabel < maxValidLabel);
CC[r * cols + c] = (unsigned short)newLabel;
}
}
}
free(h);
*numRegions = l-1;
return 1; //SUCCESS
}