forked from dkefalos/Spectral-Unmixing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SADtxt.c
309 lines (249 loc) · 7.81 KB
/
SADtxt.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* SAD: Calculate Spectral Angle Distance of a set Signatures with an Image
Copyright (C) 2014 Dimitris Kefalos
This program 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.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.*/
# include <stdio.h>
# include <stdlib.h>
# include <math.h>
# include <string.h>
#define FP_TYPE double
FP_TYPE mySQRT(FP_TYPE);
#define myABS(x) (((x)<0)?-(x):(x))
#define mySQR(x) ((x)*(x))
#define EPSILON 1.e-19
#define ROTATE(a,i,j,k,l) {g=a[i][j];h=a[k][l];a[i][j]=g-s*(h+g*tau);a[k][l]=h+s*(g-h*tau);}
int readtxt(FILE*, FILE*, int, int, float**);
int SAD_txt(FILE*, int, int, int, float**, float**);
int compare (const void * x, const void * y){
double da = *(const double *)x;
double db = *(const double *)y;
if (da == db)
return 0;
return (da > db) ? -1 : 1;
}
int main(int argc, char *argv[]) {
FILE *ftxt1, *report, *ftxt2;
int bands, nsign1, nsign2, i, nthreads;
float **a, **b;
if(argc!=7){
puts(" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
puts(" * *");
puts(" * SAD: Calculate Spectral Angle Distance of a set Signatures *");
puts(" * with another set of Signatures *");
puts(" * *");
puts(" * - Input parameters (at command line) : *");
puts(" * - First Text File Path *");
puts(" * - Second Text File Path (Endmembers) *");
puts(" * - Number of Signatures in First set *");
puts(" * - Number of Signatures in Second set *");
puts(" * - Number of Bands *");
puts(" * - Output File Path *");
puts(" * *");
puts(" * - Works on the file types supported by GDAL *");
puts(" * *");
puts(" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
puts(" * *");
puts(" * SAD by Dimitris Kefalos ([email protected]) *");
puts(" * *");
puts(" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
system("PAUSE");
exit(1);
}
/* Argument Handling */
nsign1=atoi(argv[3]);
nsign2=atoi(argv[4]);
bands=atoi(argv[5]);
if (nsign1<0){
puts("Please give a valid number of signatures");
exit(-1);
}
if (nsign2<0){
puts("Please give a valid number of signatures");
exit(-1);
}
if (bands<0){
puts("Please give a valid number of bands");
exit(-1);
}
/* File Opening */
if ( (report=fopen(argv[6] , "wt")) == NULL){
printf("\nCannot open output file.\n");
exit (-1);
}
if ((ftxt1=fopen(argv[1], "rt")) == NULL){
printf("\nCannot open text file 1.\n");
exit (-1);
}
if ((ftxt2=fopen(argv[2], "rt")) == NULL){
printf("\nCannot open text file 2.\n");
exit (-1);
}
if ( (a = malloc(nsign1*sizeof(float*))) == NULL){
puts("\nBad Memory Allocation.\n");
return(-1);
}
for (i=0; i< nsign1; i++){
if ( (a[i] = malloc(bands*sizeof(float))) == NULL){
puts("\nBad Memory Allocation.\n");
return(-1);
}
}
if ( (b = malloc(nsign2*sizeof(float*))) == NULL){
puts("\nBad Memory Allocation.\n");
return(-1);
}
for (i=0; i< nsign2; i++){
if ( (b[i] = malloc(bands*sizeof(float))) == NULL){
puts("\nBad Memory Allocation.\n");
return(-1);
}
}
/* Read Signature Text File */
fprintf(report, "Signatures :\n");
readtxt(ftxt1, report, bands, nsign1, a);
fprintf(report, "Endmembers :\n");
readtxt(ftxt2, report, bands, nsign2, b);
/* Calculate SADs */
SAD_txt(report, nsign1, nsign2, bands, a, b);
puts("SADtxt Executed");
/* Closing */
for (i=0; i<nsign2; i++){
free(b[i]);
}
free(b);
for (i=0; i<nsign1; i++){
free(a[i]);
}
free(a);
fflush(NULL);
if (fclose(ftxt2)!=0){
puts("Error in txt2 closing");
}
if (fclose(ftxt1)!=0){
puts("Error in txt1 closing");
}
if (fclose(report)!=0){
puts("Error in report closing");
}
return(0);
}
int readtxt(FILE* txtfile, FILE* report, int bands, int nendmembers, float** a){
int i, j;
double tmp;
char ignore[256];
fgets(ignore, sizeof(ignore), txtfile);
for (i=0; i<bands; i++){
for(j=0; j<nendmembers; j++){
if (fscanf(txtfile, "%lf ", &tmp)!=1){
puts("not found");
}
a[j][i]=tmp;
}
fscanf(txtfile, "\n");
}
for (i=0; i<bands; i++){
for(j=0; j<nendmembers; j++){
fprintf(report, "%f ", a[j][i]);
}
fprintf(report, "\n");
}
fprintf(report, "\n");
return (0);
}
int SAD_txt(FILE* report, int nsign1, int nsign2, int bands, float** a, float** b){
int i, j, k, l;
double **pro, *sum, **sqr, **sad;
/* Memory Allocation */
if ((sum=calloc(nsign1,sizeof(double)))==NULL){
puts("Error in Memory Allocation");
exit(1);
}
if ((pro=calloc(nsign1,sizeof(double*)))==NULL){
puts("Error in Memory Allocation");
exit(1);
}
if ((sqr=calloc(nsign1,sizeof(double*)))==NULL){
puts("Error in Memory Allocation");
exit(1);
}
if ((sad=malloc(nsign1*sizeof(double*)))==NULL){
puts("Error in Memory Allocation");
exit(1);
}
for (i=0; i<nsign1; i++){
if ((pro[i]=calloc(nsign2,sizeof(double)))==NULL){
puts("Error in Memory Allocation");
exit(1);
}
if ((sqr[i]=calloc(nsign2,sizeof(double)))==NULL){
puts("Error in Memory Allocation");
exit(1);
}
if ((sad[i]=malloc(nsign2*sizeof(double)))==NULL){
puts("Error in Memory Allocation");
exit(1);
}
}
/* SAD Calculation */
for (i=0; i<nsign1; i++){
for (j=0; j<nsign2; j++){
for (k=0; k<bands; k++){
pro[i][j]+=a[i][k]*b[j][k];
sqr[i][j]+=b[j][k]*b[j][k];
}
}
for(l=0; l<bands; l++){
sum[i]+=a[i][l]*a[i][l];
}
}
fprintf(report, "\npro :\n");
for (i=0; i<nsign1; i++){
for (j=0; j<nsign2; j++){
fprintf(report, " %lf", pro[i][j]);
}
fprintf(report, "\n");
}
fprintf(report, "\nsqr :\n");
for (i=0; i<nsign1; i++){
for (j=0; j<nsign2; j++){
fprintf(report, " %lf", sqr[i][j]);
}
fprintf(report, "\n");
}
fprintf(report, "\nsum :\n");
for (i=0; i<nsign1; i++){
fprintf(report, "%lf\n", sum[i]);
}
for (i=0; i<nsign1; i++){
for (j=0; j<nsign2; j++){
sad[i][j]=acos((pro[i][j])/((sqrt(sum[i]))*(sqrt(sqr[i][j]))));
}
}
/* File Writing */
fprintf(report, "\nSADs :\n");
for (i=0; i<nsign1; i++){
for (j=0; j<nsign2; j++){
fprintf(report, " %lf", sad[i][j]);
}
fprintf(report, "\n");
}
/* Closing SAD_txt */
for (i=0; i<nsign1; i++){
free(sad[i]);
free(sqr[i]);
free(pro[i]);
}
free(sad);
free(sqr);
free(pro);
free(sum);
return (0);
}