| Bsoft 2.0.5 Bernard's software package

bfilter.cpp
Go to the documentation of this file.
1
9 #include "rwimg.h"
10 #include "rwkernel.h"
11 #include "utilities.h"
12 #include "options.h"
13 #include "timer.h"
14
15 // Declaration of global variables
16 extern int verbose; // Level of output to the screen
17
18 // Usage assistance
19 const char* use[] = {
20 " ",
21 "Usage: bfilter [options] input.img output.img",
22 "---------------------------------------------",
23 "Filters images.",
24 " ",
25 "Actions:",
26 "-invert Invert density in the image.",
27 "-rescale -0.1,5.2 Rescale data to average and standard deviation after filtering.",
28 "-minmax -0.5,1.2 Rescale data to minimum and maximum.",
29 "-average 7,5,3 Averaging/smoothing filter: kernel size.",
30 "-gaussian 11,2.6 Gaussian smoothing filter: kernel size and sigma.",
31 "-median 3 Median filter: kernel edge size.",
32 "-peak 5 Peak filter: kernel edge size.",
33 "-gradient Gradient filter (3x3x3).",
34 "-laplacian Laplacian filter (3x3x3).",
35 "-LoG 2.5 Laplacian-of-Gaussian filter.",
36 "-DoG 1.5,3.1 Difference-of-Gaussians filter.",
37 "-denoise 2,0.4 Denoising filter: distance and density difference sigmas.",
38 "-rollingball 5,4.3 Rolling ball filter: radius and density scaling (default scaling=1).",
39 "-variance 11,1 Calculate a local variance image using the given size kernel,",
40 " with a flag to indicate standard deviation rather than variance.",
41 "-extremes his Filter image extremes: his=histogram-based, mg=for micrograph.",
42 "-replacemaxima 1.5 Replace maxima above the given threshold with surrounding average.",
43 "-bandpass 25.3,200,0.02 Bandpass filter: resolution limits (angstrom) and band edge width (1/angstrom).",
44 "-amplitude 8.3 Amplitude filter: setting all amplitudes below the threshold to zero.",
45 "-phasesonly Set all amplitudes to one.",
46 "-Bfactor 44,25.3 B-factor application: B-factor (A^2), high resolution limit (A,optional)",
47 " Multiplied in reciprocal space by exp(-B-factor/4 * s^2).",
48 " ",
49 "Parameters:",
50 "-verbose 1 Verbosity of output.",
51 "-datatype u Force writing of a new data type.",
52 "-sampling 1.5,1.5,1.5 Sampling (A/pixel; default from input file; a single value can be given).",
53 "-wrap Turn wrapping on (default off, use with -denoise option).",
54 " ",
55 "Input:",
56 "-kernel file.txt Convolve image with a kernel specified in a text file.",
57 " ",
58 NULL
59 };
60
61 int main(int argc, char **argv)
62 {
63 // Initialize variables
64 DataType nudatatype(Unknown_Type); // Conversion to new type
65 Vector3<double> sam; // Units for the three axes (A/pixel)
66 int setinvert(0); // Flag to invert density
67 double nuavg(0), nustd(0); // Values for rescaling
68 int setminmax(0); // Flag to rescale to min and max
69 double newmin(0), newmax(0); // Rescaling to new min and max
70 Vector3<long> average_kernel; // Average filter kernel size
71 long gauss_kernel(0), sigma(0); // Gaussian kernel size and sigma
72 long median_kernel(0); // Median filter kernel size
73 long peak_kernel(0); // Peak filter kernel size
74 int gradient(0); // Gradient filter
75 int laplacian(0); // Laplacian filter
76 double log_sigma(0); // Laplacian-of-Gaussian filter sigma
77 int dog1(0), dog2(0); // Difference-of-Gaussians filter
78 double sigma1(0), sigma2(0); // Denoising filter sigmas
79 long ball_radius(0); // Rolling ball radius
80 double ball_scale(0); // Denisty scale for rolling ball
81 // int wrap(0); // Flag to wrap around
82 long var_kernel(0); // Variance filter kernel size
83 int std_flag(0); // Flag to claculate StDev rather than variance
84 Bstring kernel_file; // Convolution kernel file
85 int dofft(0); // Flag to do FFT
86 double res_hi(0); // High resolution limit
87 double res_lo(0); // Low resolution limit
88 double Bfactor(0); // B-factor
89 int bandpass(0); // Fourier bandpass filter
90 double bandpass_width(0); // Edge width for the bandpass filter
91 double amp_threshold(0); // Fourier amplitude filter
92 int phases_only(0); // Flag to set all amplitudes to one
93 int filter_extremes(0); // Filter extremes, 1=histogram, 2=micrograph
94 double replace_threshold(0); // Threshold for replacing maxima
95 int replace_flag(0); // Flag for replacing maxima
96
97 int i, optind;
98 Boption* option = get_option_list(use, argc, argv, optind);
99 Boption* curropt;
100 for ( curropt = option; curropt; curropt = curropt->next ) {
101 if ( curropt->tag == "datatype" )
102 nudatatype = curropt->datatype();
103 if ( curropt->tag == "sampling" )
104 sam = curropt->sampling();
105 if ( curropt->tag == "invert" )
106 setinvert = 1;
107 if ( curropt->tag == "rescale" ) {
108 if ( curropt->values(nuavg, nustd) < 2 )
109 cerr << "-rescale: Both average and standard deviation must be specified!" << endl;
110 else if ( nustd <= 0 )
111 cerr << "-rescale: A positive standard deviation must be specified!" << endl;
112 }
113 if ( curropt->tag == "minmax" ) {
114 if ( curropt->values(newmin, newmax) < 2 )
115 cerr << "-minmax: Both min and max must be specified!" << endl;
116 else
117 setminmax = 1;
118 if ( newmin == newmax ) setminmax = 0;
119 }
120 if ( curropt->tag == "average" ) {
121 if ( ( i = curropt->values(average_kernel[0],
122 average_kernel[1], average_kernel[2]) ) < 1 )
123 cerr << "-average: The kernel edge size must be specified!" << endl;
124 else
125 if ( i < 2 ) average_kernel[2] = average_kernel[1] = average_kernel[0];
126 }
127 if ( curropt->tag == "gaussian" )
128 if ( curropt->values(gauss_kernel, sigma) < 2 )
129 cerr << "-gaussian: The kernel edge size and sigma must be specified!" << endl;
130 if ( curropt->tag == "median" )
131 if ( ( median_kernel = curropt->value.integer() ) < 1 )
132 cerr << "-median: The kernel edge size must be specified!" << endl;
133 if ( curropt->tag == "peak" )
134 if ( ( peak_kernel = curropt->value.integer() ) < 1 )
135 cerr << "-peak: The kernel edge size must be specified!" << endl;
136 if ( curropt->tag == "gradient" ) gradient = 1;
137 if ( curropt->tag == "laplacian" ) laplacian = 1;
138 if ( curropt->tag == "LoG" ) {
139 if ( ( log_sigma = curropt->real() ) < 0.1 )
140 cerr << "-LoG: A sigma value must be specified!" << endl;
141 else
142 dofft = 1;
143 }
144 if ( curropt->tag == "DoG" )
145 if ( curropt->values(dog1, dog2) < 2 )
146 cerr << "-DoG: Two sigma values must be specified!" << endl;
147 if ( curropt->tag == "denoise" )
148 if ( curropt->values(sigma1, sigma2) < 2 )
149 cerr << "-denoise: Both denoising sigmas must be specified!" << endl;
150 if ( curropt->tag == "rollingball" )
151 if ( curropt->values(ball_radius, ball_scale) < 1 )
152 cerr << "-rollingball: At least a ball radius must be specified!" << endl;
153 if ( curropt->tag == "variance" )
154 if ( curropt->values(var_kernel, std_flag) < 1 )
155 cerr << "-variance: The kernel edge size must be specified!" << endl;
156 if ( curropt->tag == "kernel" )
157 kernel_file = curropt->filename();
158 if ( curropt->tag == "extremes" ) {
159 if ( curropt->value.contains("his") ) filter_extremes = 1;
160 else if ( curropt->value.contains("mg") ) filter_extremes = 2;
161 else cerr << "-extremes: The type of filtering must be specified!" << endl;
162 }
163 if ( curropt->tag == "replacemaxima" ) {
164 if ( ( replace_threshold = curropt->value.real() ) < 0.0000001 )
165 cerr << "-replacemaxima: A threshold must be specified!" << endl;
166 else
167 replace_flag = 1;
168 }
169 if ( curropt->tag == "bandpass" ) {
170 if ( curropt->values(res_hi, res_lo, bandpass_width) < 1 )
171 cerr << "-bandpass: A resolution limit must be specified!" << endl;
172 else {
173 if ( res_lo > 0 && res_hi > res_lo ) swap(res_hi, res_lo);
174 if ( res_hi != res_lo ) bandpass = dofft = 1;
175 }
176 }
177 if ( curropt->tag == "amplitude" ) {
178 if ( ( amp_threshold = curropt->value.real() ) < 0.0000001 )
179 cerr << "-amplitude: A threshold must be specified!" << endl;
180 else
181 dofft = 1;
182 }
183 if ( curropt->tag == "phasesonly" )
184 phases_only = dofft = 1;
185 if ( curropt->tag == "Bfactor" ) {
186 if ( curropt->values(Bfactor, res_hi) < 1 )
187 cerr << "-Bfactor: A B-factor must be specified!" << endl;
188 else
189 dofft = 1;
190 }
191 // if ( curropt->tag == "wrap" ) wrap = 1;
192 }
193 option_kill(option);
194
195 double ti = timer_start();
196
197 #ifdef HAVE_GCD
198 fftwf_init_threads();
199 fftwf_plan_with_nthreads(system_processors());
200 #endif
201 if ( verbose )
202 cout << "Number of threads: " << system_processors() << endl;
203
204 // Read image file
205 int dataflag(0);
206 if ( optind < argc - 1 ) dataflag = 1;
207 Bimage* p = read_img(argv[optind++], dataflag, -1);
208 if ( p == NULL ) bexit(-1);
209
210 if ( nudatatype == Unknown_Type )
211 nudatatype = p->data_type(); // Preserve old type
212
213 if ( sam.volume() ) p->sampling(sam);
214
215 if ( filter_extremes == 1 ) p->filter_extremes(1);
216
217 if ( filter_extremes == 2 ) p->filter_extremes();
218
219 if ( replace_flag ) p->replace_maxima(replace_threshold);
220
221 if ( average_kernel[0] > 0 ) p->filter_average(average_kernel);
222
223 if ( sigma > 0 ) p->filter_gaussian(gauss_kernel, sigma);
224
225 if ( median_kernel > 0 ) p->filter_rank(median_kernel, 0.5);
226
227 if ( peak_kernel > 0 ) {
228 Bimage* pp = p->filter_peak(peak_kernel);
229 delete p;
230 p = pp;
231 }
232
233 Bimage* pkernel = NULL;
234
235 if ( var_kernel > 0 ) {
236 if ( std_flag & 2 ) {
237 Vector3<long> ksize(var_kernel, var_kernel, var_kernel);
238 ksize = ksize.min(p->size());
239 pkernel = new Bimage(Float, TSimple, ksize, 1);
240 pkernel->origin(ksize/2);
241 pkernel->sphere(ksize/2, var_kernel/2, 1, FILL_USER, 1);
242 // write_img("pk.mrc", pkernel);
243 p->variance(pkernel);
244 delete pkernel;
245 } else {
246 p->variance(var_kernel);
247 }
248 if ( std_flag & 1 ) p->square_root();
249 }
250
251 if ( kernel_file.length() ) {
252 pkernel = read_img(kernel_file, 1, 0);
253 p->convolve(pkernel);
254 // write_img("t.krn", pkernel);
255 delete pkernel;
256 }
257
258 if ( gradient ) p->filter_ortho(0);
259 if ( laplacian ) p->filter_ortho(1);
260
261 if ( dog1 >0 && dog2 > 0 )
262 p->filter_dog(dog1, dog2);
263
264 if ( sigma1 > 0 && sigma2 > 0 )
265 p->filter_bilateral(sigma1, sigma2, 1, 0);
266
267 if ( ball_radius )
268 p->filter_rolling_ball(ball_radius, ball_scale);
269
270 if ( dofft ) {
271 FourierType old_fourier_type = p->fourier_type();
272 if ( p->fourier_type() == NoTransform ) p->fft();
273
274 if ( amp_threshold ) p->fspace_amp_threshold(amp_threshold);
275
276 if ( phases_only ) p->fspace_amp_one();
277
278 if ( Bfactor != 0 ) p->fspace_weigh_B_factor(Bfactor, res_hi);
279
280 if ( log_sigma > 0 ) p->fspace_weigh_LoG(res_hi, log_sigma);
281
282 if ( bandpass ) p->fspace_bandpass(res_hi, res_lo, bandpass_width);
283
284 if ( old_fourier_type == NoTransform ) p->fft_back();
285 }
286
287 if ( setinvert ) p->invert();
288
289 if ( nustd > 0 ) p->rescale_to_avg_std(nuavg, nustd);
290
291 if ( setminmax ) p->rescale_to_min_max(newmin, newmax);
292
293 if ( optind < argc ) {
294 p->label(get_cmd_line(argc, argv));
295 p->change_type(nudatatype);
296 write_img(argv[optind], p, 0);
297 }
298
299 delete p;
300
301 #ifdef HAVE_GCD
302 fftwf_cleanup_threads();
303 #endif
304
305 if ( verbose & VERB_TIME )
306 timer_report(ti);
307
308 bexit(0);
309 }
310
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
Bstring::real
double real()
Definition: Bstring.cpp:545
DataType
DataType
Base data type specifier.
Definition: utilities.h:146
Bimage::rescale_to_min_max
int rescale_to_min_max(double numin, double numax)
Rescales the image data to a given minimum and maximum.
Definition: Bimage_rescale.cpp:100
Bimage::sampling
Vector3< double > sampling()
Definition: Bimage.h:505
rwkernel.h
Header file for functions to read and write kernel files.
Bimage::fspace_bandpass
int fspace_bandpass(double res_hi, double res_lo=0, double width=0)
Applies a bandpass filter to an image.
Definition: Bimage_fspace.cpp:499
Bimage::invert
void invert()
Inverts the data in the image.
Definition: Bimage.cpp:3108
Bimage::fft
int fft()
Definition: Bimage.h:822
Bimage::fft_back
int fft_back()
Definition: Bimage.h:823
utilities.h
Header file for general utilities.
Boption::sampling
Vector3< double > sampling()
Definition: options.h:202
Boption::value
Bstring value
Definition: options.h:30
Boption::tag
Bstring tag
Definition: options.h:29
system_processors
size_t system_processors()
Returns the number of processors.
Definition: utilities.cpp:186
Boption::real
double real()
Definition: options.h:107
Bimage::filter_rolling_ball
int filter_rolling_ball(long radius, double scale)
Apply a rolling ball filter.
Definition: Bimage_filter.cpp:836
Bstring::length
long length() const
Definition: Bstring.cpp:263
Bimage::filter_bilateral
int filter_bilateral(double sigma1, double sigma2, int kernel_type, long kernel_radius)
Denoise an image with combined gaussian distance and density difference kernel.
Definition: Bimage_filter.cpp:784
FourierType
FourierType
Fourier transform format specifier.
Definition: Bimage.h:39
Bimage::variance
int variance(long kernel_size)
Calculates the local variance within the given kernel.
Definition: Bimage_stats.cpp:615
get_option_list
Boption * get_option_list(const char *use[], int argc, char *argv[], int &optind)
Parses command line arguments based on a template.
Definition: options.cpp:40
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:76
Bimage::fspace_amp_threshold
int fspace_amp_threshold(double threshold)
Filters the amplitudes of the Fourier transform of an image.
Definition: Bimage_fspace.cpp:371
get_cmd_line
string get_cmd_line(int argc, char **argv)
Returns the command line as a string.
Definition: utilities.cpp:36
Boption::filename
Bstring filename()
Definition: options.h:85
TSimple
Definition: Bimage.h:58
timer.h
Utilities for timing functions.
Float
Definition: utilities.h:157
Bimage
General image parameter class.
Definition: Bimage.h:163
Bimage::filter_gaussian
int filter_gaussian(long kernel_size, double sigma)
Applies a gaussian filter to an image.
Definition: Bimage_filter.cpp:325
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:584
Bimage::filter_average
int filter_average(long kernel_size)
Applies an averaging filter to an image.
Definition: Bimage_filter.cpp:208
Boption
A class for a command line input option.
Definition: options.h:26
Bimage::sphere
int sphere(Vector3< double > center, double radius, double width=0, int fill_type=0, double fill=0)
Fills a sphere within an image with a uniform value.
Definition: Bimage_edit.cpp:438
Bimage::label
void label(string s)
Definition: Bimage.h:235
VERB_TIME
#define VERB_TIME
Definition: utilities.h:110
main
int main(int argc, char **argv)
Definition: bfilter.cpp:61
Bimage::rescale_to_avg_std
int rescale_to_avg_std(double nuavg, double nustd)
Rescales the image data to a given average and standard deviation.
Definition: Bimage_rescale.cpp:163
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:280
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:373
Bimage::filter_ortho
int filter_ortho(int type)
Convolves the image with an orthogonal kernel with wrapping.
Definition: Bimage_filter.cpp:545
FILL_USER
#define FILL_USER
Definition: utilities.h:172
Bimage::filter_rank
int filter_rank(long kernel_size, double rank)
Applies a median filter to an image.
Definition: Bimage_filter.cpp:1022
Bimage::convolve
int convolve(Bimage *pkernel)
Convolves an image with an arbitrary size convolution filter.
Definition: Bimage_filter.cpp:474
Boption::datatype
DataType datatype()
Definition: options.h:93
Bimage::filter_peak
Bimage * filter_peak(long kernel_size)
Finds the peaks in an image.
Definition: Bimage_filter.cpp:1076
Bimage::filter_extremes
int filter_extremes()
Filters the extremes out of a micrograph image.
Definition: Bimage_segment.cpp:634
Boption::next
Boption * next
Definition: options.h:28
verbose
int verbose
Bstring
Definition: Bstring.h:29
Bimage::fspace_weigh_B_factor
int fspace_weigh_B_factor(double B, double resolution=0)
Weighs an image's amplitudes with B-factor (gaussian) curve.
Definition: Bimage_fspace.cpp:1185
Vector3< double >
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:67
Bimage::filter_dog
int filter_dog(double sigma1, double sigma2)
Convolves the image with a difference of gausians kernel.
Definition: Bimage_filter.cpp:642
Vector3::min
Type min()
Definition: Vector3.h:169
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:109
Bstring::integer
long integer()
Definition: Bstring.cpp:539
Bimage::fspace_amp_one
int fspace_amp_one()
Sets all amplitudes to one.
Definition: Bimage_fspace.cpp:326
Unknown_Type
Definition: utilities.h:147
rwimg.h
Header file for 2D and 3D image I/O.
options.h
Header file for option handlers.
Bimage::replace_maxima
long replace_maxima(double threshold)
Replaces maxima above a threshold using local averages.
Definition: Bimage_segment.cpp:692
NoTransform
Definition: Bimage.h:40
Vector3::volume
double volume()
Definition: Vector3.h:235
Bimage::data_type
DataType data_type()
Definition: Bimage.h:349
Bimage::square_root
void square_root()
Calculates the square root of an image.
Definition: Bimage_rescale.cpp:698
use
const char * use[]
Definition: bfilter.cpp:19
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:273
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:52
Bimage::fourier_type
FourierType fourier_type()
Definition: Bimage.h:367
Bimage::fspace_weigh_LoG
int fspace_weigh_LoG(double resolution, double sigma)
Weighs an image's amplitudes with a Laplacian-of-Gaussian function.
Definition: Bimage_fspace.cpp:1263
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:563