forked from gerasimosmichalitsianos/pansharpen_c_cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpansharpen.cpp
202 lines (179 loc) · 6.55 KB
/
pansharpen.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
#include <stdio.h>
#include <stdlib.h>
#include "unistd.h"
#include "gdal_priv.h"
#include "cpl_conv.h"
#include "gdalwarper.h"
#include "ogr_spatialref.h"
#include "geotiffutil.h"
#include "pansharpen.h"
/* ********************************************************************
* void pansharpenBrovey(Geotiff,Geotiff,Geotiff,Geotiff,Geotiff):
* This function performs pan-sharpening via the Brovey or FIHS method.
* It does the basic computations for either algorithm, creating
* 4 pan-sharpened float** arrays (Red,Green,Blue,NIR). It then
* calls writeGeotiff() in geotiffutil.c to write the 4
* pan-sharpened arrays to a 4-band output Geotiff.
*
* The arguments are 5 Geotiff structure objects, which contain float**
* 2D array data for panchromatic band array data as well as that
* for the resampled red, NIR, green, and blue images (resampled to
* same dimensions as panchromatic band via bicubic resampling.)
*
* Args:
* struct Geotiff:
* geotiffNIR
* geotiffRed
* geotiffGreen
* geotiffBlue
* geotiffPan
* Returns:
* None. But calls either pansharpenFIHS or pansharpendBrovey, each of which
* subsequently call writeGeotiff() to write out new pan-sharpened
* 4-band Geotiff image file.
*/
void pansharpen(
Geotiff geotiffNIR,
Geotiff geotiffRed,
Geotiff geotiffGreen,
Geotiff geotiffBlue,
Geotiff geotiffPan
) {
pansharpenFIHS(geotiffNIR, /* call FIHS pan-sharpening method. */
geotiffRed,
geotiffGreen,
geotiffBlue,
geotiffPan
);
pansharpenBrovey(geotiffNIR, /* call the Brovey pan-sharpening method. */
geotiffRed,
geotiffGreen,
geotiffBlue,
geotiffPan
);
}
/* ********************************************************************
* void pansharpenBrovey(Geotiff,Geotiff,Geotiff,Geotiff,Geotiff):
* This function performs pan-sharpening via the Brovey method.
* It does the basic computations for this algorithm, creating
* 4 pan-sharpened float** arrays (Red,Green,Blue,NIR). It then
* calls writeGeotiff() in geotiffutil.c to write the 4
* pan-sharpened arrays to a 4-band output Geotiff.
*
* The arguments are 5 Geotiff structure objects, which contain float**
* 2D array data for panchromatic band array data as well as that
* for the resampled red, NIR, green, and blue images (resampled to
* same dimensions as panchromatic band via bicubic resampling.)
*
* Args:
* struct Geotiff:
* geotiffNIR
* geotiffRed
* geotiffGreen
* geotiffBlue
* geotiffPan
* Returns:
* None. But calls writeGeotiff() to write out new pan-sharpened
* 4-band Geotiff image file.
*/
void pansharpenBrovey(
Geotiff geotiffNIR,
Geotiff geotiffRed,
Geotiff geotiffGreen,
Geotiff geotiffBlue,
Geotiff geotiffPan
) {
float** NIR = geotiffNIR.band;
float** red = geotiffRed.band;
float** green = geotiffGreen.band;
float** blue = geotiffBlue.band;
float** pan = geotiffPan.band;
int nrows, ncols;
int row, col;
nrows = geotiffPan.ysize;
ncols = geotiffPan.xsize;
float** L = (float**)malloc(nrows*sizeof(float*));
float** NIRsharp = (float**)malloc(nrows*sizeof(float*));
float** redsharp = (float**)malloc(nrows*sizeof(float*));
float** greensharp = (float**)malloc(nrows*sizeof(float*));
float** bluesharp = (float**)malloc(nrows*sizeof(float*));
float sum;
for(row=0; row<nrows; row++) {
NIRsharp[row] = (float*)malloc(ncols*sizeof(float));
redsharp[row] = (float*)malloc(ncols*sizeof(float));
greensharp[row] = (float*)malloc(ncols*sizeof(float));
bluesharp[row] = (float*)malloc(ncols*sizeof(float));
for(col=0; col<ncols; col++) {
sum = (float)(red[row][col] + green[row][col] + blue[row][col] + NIR[row][col]);
redsharp[row][col] = ((float)red[row][col] / sum)*((float)pan[row][col]);
greensharp[row][col] = ((float)green[row][col] / sum)*((float)pan[row][col]);
bluesharp[row][col] = ((float)blue[row][col] / sum)*((float)pan[row][col]);
NIRsharp[row][col] = ((float)NIR[row][col] / sum)*((float)pan[row][col]);
}
}
writeGeotiff(geotiffPan,redsharp,greensharp,bluesharp,NIRsharp,"pansharpendBrovey.tif");
}
/* ********************************************************************
* void pansharpenFIHS(Geotiff,Geotiff,Geotiff,Geotiff,Geotiff):
* This function performs pan-sharpening via the FIHS method.
* This is the "Fast Intensity Hue Saturation" method.
*
* It does the basic computations for this algorithm, creating
* 4 pan-sharpened float** arrays (Red,Green,Blue,NIR). It then
* calls writeGeotiff() in geotiffutil.c to write the 4
* pan-sharpened arrays to a 4-band output Geotiff.
*
* The arguments are 5 Geotiff structure objects, which contain float**
* 2D array data for panchromatic band array data as well as that
* for the resampled red, NIR, green, and blue images (resampled to
* same dimensions as panchromatic band via bicubic resampling.)
*
* Args:
* struct Geotiff:
* geotiffNIR
* geotiffRed
* geotiffGreen
* geotiffBlue
* geotiffPan
* Returns:
* None. But calls writeGeotiff() to write out new pan-sharpened
* 4-band Geotiff image file.
*/
void pansharpenFIHS(
Geotiff geotiffNIR,
Geotiff geotiffRed,
Geotiff geotiffGreen,
Geotiff geotiffBlue,
Geotiff geotiffPan
) {
float** NIR = geotiffNIR.band;
float** red = geotiffRed.band;
float** green = geotiffGreen.band;
float** blue = geotiffBlue.band;
float** pan = geotiffPan.band;
int nrows, ncols;
int row, col;
nrows = geotiffPan.ysize;
ncols = geotiffPan.xsize;
float** L = (float**)malloc(nrows*sizeof(float*));
float** NIRsharp = (float**)malloc(nrows*sizeof(float*));
float** redsharp = (float**)malloc(nrows*sizeof(float*));
float** greensharp = (float**)malloc(nrows*sizeof(float*));
float** bluesharp = (float**)malloc(nrows*sizeof(float*));
for(row=0; row<nrows; row++) {
L[row] = (float*)malloc(ncols*sizeof(float));
NIRsharp[row] = (float*)malloc(ncols*sizeof(float));
redsharp[row] = (float*)malloc(ncols*sizeof(float));
greensharp[row] = (float*)malloc(ncols*sizeof(float));
bluesharp[row] = (float*)malloc(ncols*sizeof(float));
for(col=0; col<ncols; col++) {
L[row][col] = ((float)(red[row][col] + green[row][col] +
blue[row][col] + NIR[row][col]))/4.0;
redsharp[row][col] = red[row][col] + (pan[row][col] - L[row][col]);
greensharp[row][col] = green[row][col] + (pan[row][col] - L[row][col]);
bluesharp[row][col] = green[row][col] + (pan[row][col] - L[row][col]);
NIRsharp[row][col] = NIR[row][col] + (pan[row][col] - L[row][col]);
}
}
writeGeotiff(geotiffPan,redsharp,greensharp,bluesharp,NIRsharp,"pansharpendFIHS.tif");
}