aboutsummaryrefslogtreecommitdiff
path: root/libcrystfel/src/filters.c
blob: 4bc41f3d35362deb1cbd36ba08abdf9a08700bbf (plain)
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
/*
 * filters.c
 *
 * Image filtering
 *
 * Copyright © 2012-2021 Deutsches Elektronen-Synchrotron DESY,
 *                       a research centre of the Helmholtz Association.
 *
 * Authors:
 *   2010-2020 Thomas White <taw@physics.org>
 *   2013      Anton Barty <anton.barty@desy.de>
 *
 * This file is part of CrystFEL.
 *
 * CrystFEL 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.
 *
 * CrystFEL 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 CrystFEL.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <libcrystfel-config.h>

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <assert.h>
#include <gsl/gsl_statistics_int.h>
#include <gsl/gsl_blas.h>

#include "image.h"

/** \file filters.h */

static void filter_noise_in_panel(float *data, int width, int height)
{
	int x, y;

	for ( y=0; y<height; y++ ) {
	for ( x=0; x<width; x++ ) {

		int dx, dy;
		float val = data[x+width*y];

		/* This isn't really the right thing to do at the edges,
		 * but this filter is so horrible it's unlikely to matter. */
		if ( (x==0) || (x==width-1)
		  || (y==0) || (y==height-1) ) {
			if ( val < 0 ) {
				data[x+width*y] = 0.0;
			}
			continue;
		}

		for ( dy=-1; dy<=+1; dy++ ) {
		for ( dx=-1; dx<=+1; dx++ ) {
			if ( data[(x+dx)+width*(y+dy)] ) val = 0.0;
		}
		}

		data[x+width*y] = val;

	}
	}
}


void filter_noise(struct image *image)
{
	int i;

	for ( i=0; i<image->detgeom->n_panels; i++ ) {
		struct detgeom_panel *p = &image->detgeom->panels[i];
		filter_noise_in_panel(image->dp[i], p->w, p->h);
	}
}


/* Force the linker to bring in CBLAS to make GSL happy */
void filters_fudge_gslcblas()
{
        STATUS("%p\n", cblas_sgemm);
}


#define SWAP(a,b) { float t=(a);(a)=(b);(b)=t; }
static float kth_smallest(float *a, int n, int k)
{
	long l, m;

	l = 0;
	m = n-1;

	while ( l < m ) {
		long i, j;
		float x;
		x=a[k];
		i=l;
		j=m;
		do {
			while (a[i]<x) i++;
			while (x<a[j]) j--;
			if ( i<=j ) {
				SWAP(a[i],a[j]);
				i++;
				j--;
			}
		} while (i<=j);
		if ( j<k ) l = i;
		if ( k<i ) m = j;
	}
	return a[k];
}
#undef SWAP


void filter_median(struct image *image, int size)
{
	int counter;
	int nn;
	float *buffer;
	int pn;

	if ( size <= 0 ) return;

	nn = 2*size+1;
	nn = nn*nn;

	/* "localBg" is way too big, but guaranteed big enough */
	buffer = calloc(nn, sizeof(float));
	if ( buffer == NULL ) {
		ERROR("Failed to allocate LB buffer.\n");
		return;
	}

	/* Determine local background
	 * (median over window width either side of current pixel) */
	for ( pn=0; pn<image->detgeom->n_panels; pn++ ) {

		int fs, ss;
		int i;
		struct detgeom_panel *p;
		float *localBg;

		p = &image->detgeom->panels[pn];

		localBg = calloc(p->w*p->h, sizeof(float));
		if ( localBg == NULL ) {
			ERROR("Failed to allocate LB buffer.\n");
			return;
		}

		for ( ss=0; ss<p->h; ss++ ) {
		for ( fs=0; fs<p->w; fs++ ) {

			int ifs, iss;

			counter = 0;

			// Loop over median window
			for ( iss=-size; iss<=size; iss++ ) {
			for ( ifs=-size; ifs<=size; ifs++ ) {

				int idx;

				if ( (fs+ifs) < 0 ) continue;
				if ( (fs+ifs) >= p->w ) continue;
				if ( (ss+iss) < 0 ) continue;
				if ( (ss+iss) >= p->h ) continue;

				idx = fs+ifs + (ss+iss)*p->w;
				buffer[counter++] = image->dp[pn][idx];

			}
			}

			// Find median value
			localBg[fs+p->w*ss] = kth_smallest(buffer, counter,
			                                   counter/2);

		}
		}

		/* Do the background subtraction */
		for ( i=0; i<p->w*p->h; i++ ) {
			image->dp[pn][i] -= localBg[i];
		}

		free(localBg);
	}

	free(buffer);
}