| 1 |
/*=========================================================================== |
| 2 |
Copyright (C) 1987-2009 European Southern Observatory (ESO) |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or |
| 5 |
modify it under the terms of the GNU General Public License as |
| 6 |
published by the Free Software Foundation; either version 2 of |
| 7 |
the License, or (at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public |
| 15 |
License along with this program; if not, write to the Free |
| 16 |
Software Foundation, Inc., 675 Massachusetts Ave, Cambridge, |
| 17 |
MA 02139, USA. |
| 18 |
|
| 19 |
Correspondence concerning ESO-MIDAS should be addressed as follows: |
| 20 |
Internet e-mail: [email protected] |
| 21 |
Postal address: European Southern Observatory |
| 22 |
Data Management Division |
| 23 |
Karl-Schwarzschild-Strasse 2 |
| 24 |
D 85748 Garching bei Muenchen |
| 25 |
GERMANY |
| 26 |
===========================================================================*/ |
| 27 |
|
| 28 |
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 29 |
.IDENTIFIER module medfilt |
| 30 |
.LANGUAGE C |
| 31 |
.AUTHOR K. Banse IPG-ESO Garching |
| 32 |
.KEYWORDS |
| 33 |
.COMMENTS |
| 34 |
|
| 35 |
MEDFILT: Median filtering of single frame, general, v.2.1, NOV 97 |
| 36 |
See: T.B.Georgiev, 1996, Bull.SAO (Russia) 39, 124 |
| 37 |
MEDFILT inp[.fts] smt[.fts] rsd[.fts] W C Z |
| 38 |
inpfr - input frame; |
| 39 |
outfr - result (smoothed) frame; |
| 40 |
rsdfr - residual frame, rsdfr = inpfr - outfr; |
| 41 |
W: window diameter, W=2/3/4/5..,99 DEFAULT W=25 |
| 42 |
C=3-4: coeff. for deviation threshold -- the median M |
| 43 |
changes the current pixel value V if abs(V-M) > C*sigma |
| 44 |
the case of simple median filtering is: DEFAULT C=0 |
| 45 |
T.B.Georgiev, IA Sofia, [email protected] |
| 46 |
|
| 47 |
.VERSIONS 1.00 980708 implemented in Midas. KB |
| 48 |
|
| 49 |
090713 last modif |
| 50 |
------------------------------------------------------------------*/ |
| 51 |
|
| 52 |
#include <stdio.h> |
| 53 |
#include <math.h> |
| 54 |
#include <midas_def.h> |
| 55 |
|
| 56 |
#include <stdlib.h> |
| 57 |
|
| 58 |
#define MAXNC 8192 /* max image width in pixels */ |
| 59 |
#define MAXFW 99 /*max window diameter in pixels */ |
| 60 |
#define NRB 40 /* number of output buffer rows */ |
| 61 |
#define MAXHI 65535 /* max histogram size */ |
| 62 |
#define MAXIN 32767 |
| 63 |
|
| 64 |
extern int fits_open(), fits_get_data(), fits_put_data(); |
| 65 |
|
| 66 |
/* |
| 67 |
|
| 68 |
*/ |
| 69 |
|
| 70 |
int main() |
| 71 |
|
| 72 |
{ |
| 73 |
int finp, fout, frsd, iav, uni, nul; |
| 74 |
int nr, nc, nim, fw, fw0, hw, sum, med, hlf, npix, nb, ib, maxhi; |
| 75 |
int me1, me2, su1, su2, q1, q2, ncor1, ncor2; |
| 76 |
int niout, i, iinp, iout, j, k, l, ll, lk, n, np, npc, rr, ls, lr; |
| 77 |
|
| 78 |
static short int m[MAXFW][MAXNC+MAXFW-1]; /* median frame band */ |
| 79 |
static short int ms[NRB][MAXNC]; /* output-smt buffer */ |
| 80 |
static short int mr[NRB][MAXNC]; /* output-rsd buffer */ |
| 81 |
static short int h[MAXHI]; /* histogram in the window */ |
| 82 |
static short int r[MAXNC+MAXFW-1]; /* input or output image row */ |
| 83 |
static short int pn[MAXFW]; /*row number in the memory m[][] */ |
| 84 |
static short int lw[MAXFW/2+1]; /* limits of the circle window */ |
| 85 |
|
| 86 |
double d, dls, dlr, dp, sm, sd, ssm, ssd, sig, csig, ssig, sssig; |
| 87 |
|
| 88 |
char cbuf[88], infile[82], outfile[82], rsdfile[82]; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
fout = -1; |
| 94 |
frsd = med = sum = 0; |
| 95 |
|
| 96 |
(void) SCSPRO("medfilt"); |
| 97 |
|
| 98 |
(void) SCKGETC("IN_A",1,80,&iav,infile); |
| 99 |
(void) SCKGETC("OUT_A",1,80,&iav,outfile); |
| 100 |
(void) SCKGETC("OUT_B",1,80,&iav,rsdfile); |
| 101 |
(void) SCKRDI("NUM_OUT",1,1,&iav,&nim,&uni,&nul); |
| 102 |
(void) SCKRDI("FW",1,1,&iav,&fw,&uni,&nul); |
| 103 |
(void) SCKRDD("CSIG",1,1,&iav,&csig,&uni,&nul); |
| 104 |
|
| 105 |
if (fw < 2) fw=2; |
| 106 |
if (fw > MAXFW) fw=MAXFW; |
| 107 |
if (csig < 0.) csig=0.; |
| 108 |
|
| 109 |
|
| 110 |
/* --------------------- PRELIMINARY PART ------------------ */ |
| 111 |
|
| 112 |
maxhi = MAXHI - 2; /* hist.numbers: from 1 to maxhi */ |
| 113 |
fw0 = fw; |
| 114 |
hw = fw / 2; |
| 115 |
if (2 * hw == fw) |
| 116 |
rr = hw * hw; |
| 117 |
else |
| 118 |
rr = (int)((hw + .5) * (hw + .5)); |
| 119 |
|
| 120 |
npix = 1; |
| 121 |
for (k=0; k<=hw; k++) |
| 122 |
{ |
| 123 |
lw[k] = 0; /* the circle window limits */ |
| 124 |
for (l=0; l<=hw; l++) |
| 125 |
{ |
| 126 |
if (k*k + l*l <= rr) |
| 127 |
{ |
| 128 |
lw[k] = l; |
| 129 |
if (k > 0) npix += 4; |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
fw = 2 * hw + 1; |
| 135 |
hlf = npix / 2 + 1; /* half of the histogram */ |
| 136 |
q1 = (int)((double)npix * 0.17 + .5); /* left quantil of hist */ |
| 137 |
q2 = (int)((double)npix * 0.83 + .5); /* right quantil of hist */ |
| 138 |
|
| 139 |
|
| 140 |
/* ----------------------- MAIN PROCESS ----------------------- */ |
| 141 |
|
| 142 |
finp = fits_open(infile, "r", &nr, &nc); |
| 143 |
|
| 144 |
if (nc > MAXNC) |
| 145 |
{ |
| 146 |
sprintf(cbuf,"Too wide frame, ncol=%d",nc); |
| 147 |
SCETER(11,cbuf); |
| 148 |
} |
| 149 |
|
| 150 |
if (nim != 1) fout = fits_open(outfile, "w", &nr, &nc); |
| 151 |
if (nim != 0) frsd = fits_open(rsdfile, "w", &nr, &nc); |
| 152 |
|
| 153 |
dp = (double)nr * (double)nc; |
| 154 |
sprintf(cbuf,"%dx%d windim=%d (%dpix) thr=%2.1f",nr, nc, fw0, npix, csig); |
| 155 |
SCTPUT(cbuf); |
| 156 |
|
| 157 |
me1 = me2 = npix = ncor1 = ncor2 = 0; |
| 158 |
sm = sd = ssm = ssd = ssig = sssig = 0.; |
| 159 |
|
| 160 |
|
| 161 |
/* ---------------------- MAIN LOOP BY IMAGE ROWS ------------------- */ |
| 162 |
|
| 163 |
for (i= -hw; i<nr+hw; i++) |
| 164 |
{ |
| 165 |
iinp = i; /* inp.row number for the inner part of the image */ |
| 166 |
if (i < 0) |
| 167 |
iinp = nr + i; /* input row number for upper margin */ |
| 168 |
if (i > nr - 1) |
| 169 |
iinp = i - nr; /* input row number for lower margin*/ |
| 170 |
|
| 171 |
for (k=1; k<fw; k++) pn[k-1] = pn[k]; /* rotation of numbers */ |
| 172 |
|
| 173 |
k = i + hw; |
| 174 |
np = k - k / fw * fw; |
| 175 |
pn[fw-1] = np; /* pos.number of inp.row */ |
| 176 |
npc = pn[hw]; /* pos.num of curr.out.row for eventual output */ |
| 177 |
fits_get_data (finp, iinp, 0, r, nc); /* READING THE IMAGE ROW */ |
| 178 |
|
| 179 |
for (j=0; j<nc; j++) |
| 180 |
{ |
| 181 |
ll=r[j]; |
| 182 |
if (ll<-MAXIN+1) ll=-MAXIN+1; |
| 183 |
if (ll> MAXIN-1) ll= MAXIN-1; |
| 184 |
m[np][hw+j] = ll; /* filling the buffer m[][] */ |
| 185 |
} |
| 186 |
|
| 187 |
for (j=0; j<hw; j++) |
| 188 |
{ /* left-right margins */ |
| 189 |
m[np][j] = m[np][nc+j]; |
| 190 |
m[np][nc+hw+j] = m[np][hw+j]; |
| 191 |
} |
| 192 |
|
| 193 |
iout = i - hw; /* output row number */ |
| 194 |
nb = iout / NRB; /* num of the output buffer */ |
| 195 |
ib = iout - nb * NRB; /* num of the row in the output buffer */ |
| 196 |
if (iout < 0) goto Next; |
| 197 |
|
| 198 |
|
| 199 |
/* ---------------- LOOP IN THE CURRENT IMAGE ROW ---------------- */ |
| 200 |
|
| 201 |
for (j=hw; j<nc+hw; j++) |
| 202 |
{ |
| 203 |
if (j == hw) |
| 204 |
{ /* first pixel in the current row */ |
| 205 |
for (n=0; n<MAXHI; n++) h[n] = 0; /* histogram inital. */ |
| 206 |
|
| 207 |
for (k=0; k<fw; k++) |
| 208 |
{ |
| 209 |
np = pn[k]; |
| 210 |
l = abs(k - hw); |
| 211 |
lk = lw[l]; |
| 212 |
for (l=hw-lk; l<=hw+lk; l++) |
| 213 |
{ |
| 214 |
n = m[np][l] + MAXIN; |
| 215 |
h[n]++; |
| 216 |
} |
| 217 |
} |
| 218 |
med = 0; |
| 219 |
sum = 0; |
| 220 |
while (sum < hlf) |
| 221 |
{ |
| 222 |
med++; |
| 223 |
sum += h[med]; |
| 224 |
} /* MED */ |
| 225 |
|
| 226 |
if (csig > 0.) |
| 227 |
{ |
| 228 |
me1 = med - 1; |
| 229 |
su1 = sum - h[med]; |
| 230 |
|
| 231 |
while (su1 > q1) |
| 232 |
{ |
| 233 |
su1 -= h[me1]; |
| 234 |
me1--; |
| 235 |
} /* left q-limit of the histogram body */ |
| 236 |
|
| 237 |
me2 = med + 1; |
| 238 |
su2 = sum + h[me2]; |
| 239 |
while (su2 < q2) |
| 240 |
{ |
| 241 |
me2++; |
| 242 |
su2 += h[me2]; |
| 243 |
} |
| 244 |
} /* right q-limit of the histogram body */ |
| 245 |
} /* end of j==hw, i.e. the first pix */ |
| 246 |
|
| 247 |
else |
| 248 |
{ /* other pixels in the current row */ |
| 249 |
for (k=0; k<fw; k++) |
| 250 |
{ /* along the window edge columns only */ |
| 251 |
np = pn[k]; |
| 252 |
l = abs(k - hw); |
| 253 |
lk = lw[l]; |
| 254 |
n = m[np][j-lk-1] + MAXIN; |
| 255 |
h[n]--; |
| 256 |
if (n <= med) sum--; /* removing */ |
| 257 |
n = m[np][j+lk] + MAXIN; |
| 258 |
h[n]++; |
| 259 |
if (n <= med) sum++; |
| 260 |
} /* adding */ |
| 261 |
while (sum >= hlf) |
| 262 |
{ |
| 263 |
sum -= h[med]; |
| 264 |
med--; |
| 265 |
} /* search for median */ |
| 266 |
|
| 267 |
while (sum < hlf) |
| 268 |
{ |
| 269 |
med++; |
| 270 |
sum += h[med]; |
| 271 |
} /* MEDIAN */ |
| 272 |
if (csig > 0.) |
| 273 |
{ |
| 274 |
me1 = med - 1; |
| 275 |
su1 = sum - h[med]; |
| 276 |
while (su1 > q1) |
| 277 |
{ |
| 278 |
su1 -= h[me1]; |
| 279 |
me1--; |
| 280 |
} /* left limit of the histogram body */ |
| 281 |
|
| 282 |
me2 = med + 1; |
| 283 |
su2 = sum + h[me2]; |
| 284 |
while (su2 < q2) |
| 285 |
{ |
| 286 |
me2++; |
| 287 |
su2 += h[me2]; |
| 288 |
} |
| 289 |
} /* right limit of the histogram body */ |
| 290 |
} /* end of j -- "other pixels" */ |
| 291 |
|
| 292 |
|
| 293 |
/* ------------ DIFFERENT KINDS OF OUTPUT IMAGES ------------- */ |
| 294 |
|
| 295 |
ls = ms[ib][j-hw] = med-MAXIN; |
| 296 |
lr = mr[ib][j-hw] = m[npc][j] - ls; |
| 297 |
dls=(double)ls; |
| 298 |
dlr=(double)lr; |
| 299 |
|
| 300 |
if (csig==0.) |
| 301 |
{ |
| 302 |
sm += dls; ssm += dls * dls; |
| 303 |
sd += dlr; ssd += dlr * dlr; |
| 304 |
} |
| 305 |
if (csig > 0.) |
| 306 |
{ |
| 307 |
sig = (double)(me2 - me1) / 2.; /* hist.sigma */ |
| 308 |
npix++; ssig += sig; sssig += sig * sig; |
| 309 |
ll = (int)(csig * sig + 0.5); |
| 310 |
if (lr < -ll) ncor1++; |
| 311 |
if (lr > ll) ncor2++; |
| 312 |
if (-ll < lr && lr < ll) |
| 313 |
{ /* no replacing */ |
| 314 |
ms[ib][j-hw] = m[npc][j]; |
| 315 |
mr[ib][j-hw] = 0; |
| 316 |
dls = (double)m[npc][j]; |
| 317 |
sm += dls; |
| 318 |
ssm += dls * dls; |
| 319 |
} |
| 320 |
else |
| 321 |
{ /* save replacing made earlier */ |
| 322 |
sm += dls; ssm += dls * dls; |
| 323 |
sd += dlr; ssd += dlr * dlr; |
| 324 |
} |
| 325 |
} |
| 326 |
} /* END OF j-LOOP */ |
| 327 |
|
| 328 |
if (ib == NRB - 1 || iout == nr - 1) |
| 329 |
{ |
| 330 |
if (nim != 1) |
| 331 |
{ |
| 332 |
for (l = 0; l <= ib; l++) |
| 333 |
{ |
| 334 |
for (j = 0; j < nc; j++) r[j] = ms[l][j]; |
| 335 |
niout = nb * NRB + l; |
| 336 |
fits_put_data(fout, niout, 0, r, nc); |
| 337 |
} |
| 338 |
} |
| 339 |
if (nim != 0) |
| 340 |
{ |
| 341 |
for (l = 0; l <= ib; l++) |
| 342 |
{ |
| 343 |
for (j = 0; j < nc; j++) r[j] = mr[l][j]; |
| 344 |
niout = nb * NRB + l; |
| 345 |
fits_put_data(frsd, niout, 0, r, nc); |
| 346 |
} |
| 347 |
} |
| 348 |
} |
| 349 |
Next: |
| 350 |
; |
| 351 |
} /* END OF i-LOOP */ |
| 352 |
|
| 353 |
sd /= dp; |
| 354 |
ssd /= dp; |
| 355 |
ssd = sqrt(ssd - sd * sd); |
| 356 |
sm /= dp; |
| 357 |
ssm /= dp; |
| 358 |
ssm = sqrt(ssm - sm * sm); |
| 359 |
sprintf(cbuf,"out %3.1f+-%3.1f",sm,ssm); |
| 360 |
SCTPUT(cbuf); |
| 361 |
sprintf(cbuf,"resid.out %3.1f+-%3.1f",sd,ssd); |
| 362 |
SCTPUT(cbuf); |
| 363 |
|
| 364 |
if (csig > 0.) |
| 365 |
{ |
| 366 |
sm = (double)ncor1 / dp; |
| 367 |
sd = (double)ncor2 / dp; |
| 368 |
d = (double)npix; |
| 369 |
ssig /= d; |
| 370 |
sssig /= d; |
| 371 |
sssig = sqrt(sssig - ssig * ssig); |
| 372 |
sprintf(cbuf,"number of replacings: -%d (%4.3f) +%d (%4.3f)", |
| 373 |
ncor1, sm, ncor2, sd); |
| 374 |
SCTPUT(cbuf); |
| 375 |
} |
| 376 |
|
| 377 |
if (nim != 1) |
| 378 |
{ |
| 379 |
(void) SCDCOP(finp,fout,1,cbuf); /* copy all descriptors */ |
| 380 |
CGN_DSCUPD(finp,fout," "); /* update history */ |
| 381 |
} |
| 382 |
|
| 383 |
if (nim != 0) |
| 384 |
{ |
| 385 |
(void) SCDCOP(finp,frsd,1,cbuf); /* copy all descriptors */ |
| 386 |
CGN_DSCUPD(finp,frsd," "); /* update history */ |
| 387 |
} |
| 388 |
|
| 389 |
(void) SCSEPI(); /* close all files + exit */ |
| 390 |
|
| 391 |
return 0; |
| 392 |
} |
| 393 |
|