Bsoft  2.1.0
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 "-origin 10,-10,20 Origin for rotation (default 0,0,0).",
54 "-wrap Turn wrapping on (default off, use with -denoise option).",
55 " ",
56 "Input:",
57 "-kernel file.txt Convolve image with a kernel specified in a text file.",
58 " ",
59 NULL
60 };
61 
62 int main(int argc, char **argv)
63 {
64  // Initialize variables
65  DataType nudatatype(Unknown_Type); // Conversion to new type
66  Vector3<double> sam; // Units for the three axes (A/pixel)
67  Vector3<double> origin; // Origin for rotation
68  int set_origin(0); // Flag to set origin
69  int setinvert(0); // Flag to invert density
70  double nuavg(0), nustd(0); // Values for rescaling
71  int setminmax(0); // Flag to rescale to min and max
72  double newmin(0), newmax(0); // Rescaling to new min and max
73  Vector3<long> average_kernel; // Average filter kernel size
74  long gauss_kernel(0), sigma(0); // Gaussian kernel size and sigma
75  long median_kernel(0); // Median filter kernel size
76  long peak_kernel(0); // Peak filter kernel size
77  int gradient(0); // Gradient filter
78  int laplacian(0); // Laplacian filter
79  double log_sigma(0); // Laplacian-of-Gaussian filter sigma
80  int dog1(0), dog2(0); // Difference-of-Gaussians filter
81  double sigma1(0), sigma2(0); // Denoising filter sigmas
82  long ball_radius(0); // Rolling ball radius
83  double ball_scale(0); // Denisty scale for rolling ball
84 // int wrap(0); // Flag to wrap around
85  long var_kernel(0); // Variance filter kernel size
86  int std_flag(0); // Flag to claculate StDev rather than variance
87  Bstring kernel_file; // Convolution kernel file
88  int dofft(0); // Flag to do FFT
89  double res_hi(0); // High resolution limit
90  double res_lo(0); // Low resolution limit
91  double Bfactor(0); // B-factor
92  int bandpass(0); // Fourier bandpass filter
93  double bandpass_width(0); // Edge width for the bandpass filter
94  double amp_threshold(0); // Fourier amplitude filter
95  int phases_only(0); // Flag to set all amplitudes to one
96  int filter_extremes(0); // Filter extremes, 1=histogram, 2=micrograph
97  double replace_threshold(0); // Threshold for replacing maxima
98  int replace_flag(0); // Flag for replacing maxima
99 
100  int i, optind;
101  Boption* option = get_option_list(use, argc, argv, optind);
102  Boption* curropt;
103  for ( curropt = option; curropt; curropt = curropt->next ) {
104  if ( curropt->tag == "datatype" )
105  nudatatype = curropt->datatype();
106  if ( curropt->tag == "sampling" )
107  sam = curropt->scale();
108  if ( curropt->tag == "origin" ) {
109  if ( curropt->value[0] == 'c' ) {
110  set_origin = 2;
111  } else {
112  origin = curropt->origin();
113  set_origin = 1;
114  }
115  }
116  if ( curropt->tag == "invert" )
117  setinvert = 1;
118  if ( curropt->tag == "rescale" ) {
119  if ( curropt->values(nuavg, nustd) < 2 )
120  cerr << "-rescale: Both average and standard deviation must be specified!" << endl;
121  else if ( nustd <= 0 )
122  cerr << "-rescale: A positive standard deviation must be specified!" << endl;
123  }
124  if ( curropt->tag == "minmax" ) {
125  if ( curropt->values(newmin, newmax) < 2 )
126  cerr << "-minmax: Both min and max must be specified!" << endl;
127  else
128  setminmax = 1;
129  if ( newmin == newmax ) setminmax = 0;
130  }
131  if ( curropt->tag == "average" ) {
132  if ( ( i = curropt->values(average_kernel[0],
133  average_kernel[1], average_kernel[2]) ) < 1 )
134  cerr << "-average: The kernel edge size must be specified!" << endl;
135  else
136  if ( i < 2 ) average_kernel[2] = average_kernel[1] = average_kernel[0];
137  }
138  if ( curropt->tag == "gaussian" )
139  if ( curropt->values(gauss_kernel, sigma) < 2 )
140  cerr << "-gaussian: The kernel edge size and sigma must be specified!" << endl;
141  if ( curropt->tag == "median" )
142  if ( ( median_kernel = curropt->value.integer() ) < 1 )
143  cerr << "-median: The kernel edge size must be specified!" << endl;
144  if ( curropt->tag == "peak" )
145  if ( ( peak_kernel = curropt->value.integer() ) < 1 )
146  cerr << "-peak: The kernel edge size must be specified!" << endl;
147  if ( curropt->tag == "gradient" ) gradient = 1;
148  if ( curropt->tag == "laplacian" ) laplacian = 1;
149  if ( curropt->tag == "LoG" ) {
150  if ( ( log_sigma = curropt->real() ) < 0.1 )
151  cerr << "-LoG: A sigma value must be specified!" << endl;
152  else
153  dofft = 1;
154  }
155  if ( curropt->tag == "DoG" )
156  if ( curropt->values(dog1, dog2) < 2 )
157  cerr << "-DoG: Two sigma values must be specified!" << endl;
158  if ( curropt->tag == "denoise" )
159  if ( curropt->values(sigma1, sigma2) < 2 )
160  cerr << "-denoise: Both denoising sigmas must be specified!" << endl;
161  if ( curropt->tag == "rollingball" )
162  if ( curropt->values(ball_radius, ball_scale) < 1 )
163  cerr << "-rollingball: At least a ball radius must be specified!" << endl;
164  if ( curropt->tag == "variance" )
165  if ( curropt->values(var_kernel, std_flag) < 1 )
166  cerr << "-variance: The kernel edge size must be specified!" << endl;
167  if ( curropt->tag == "kernel" )
168  kernel_file = curropt->filename();
169  if ( curropt->tag == "extremes" ) {
170  if ( curropt->value.contains("his") ) filter_extremes = 1;
171  else if ( curropt->value.contains("mg") ) filter_extremes = 2;
172  else cerr << "-extremes: The type of filtering must be specified!" << endl;
173  }
174  if ( curropt->tag == "replacemaxima" ) {
175  if ( ( replace_threshold = curropt->value.real() ) < 0.0000001 )
176  cerr << "-replacemaxima: A threshold must be specified!" << endl;
177  else
178  replace_flag = 1;
179  }
180  if ( curropt->tag == "bandpass" ) {
181  if ( curropt->values(res_hi, res_lo, bandpass_width) < 1 )
182  cerr << "-bandpass: A resolution limit must be specified!" << endl;
183  else {
184  if ( res_lo > 0 && res_hi > res_lo ) swap(res_hi, res_lo);
185  if ( res_hi != res_lo ) bandpass = dofft = 1;
186  }
187  }
188  if ( curropt->tag == "amplitude" ) {
189  if ( ( amp_threshold = curropt->value.real() ) < 0.0000001 )
190  cerr << "-amplitude: A threshold must be specified!" << endl;
191  else
192  dofft = 1;
193  }
194  if ( curropt->tag == "phasesonly" )
195  phases_only = dofft = 1;
196  if ( curropt->tag == "Bfactor" ) {
197  if ( curropt->values(Bfactor, res_hi) < 1 )
198  cerr << "-Bfactor: A B-factor must be specified!" << endl;
199  else
200  dofft = 1;
201  }
202 // if ( curropt->tag == "wrap" ) wrap = 1;
203  }
204  option_kill(option);
205 
206  double ti = timer_start();
207 
208 #ifdef HAVE_GCD
209  fftwf_init_threads();
210  fftwf_plan_with_nthreads(system_processors());
211 #endif
212  if ( verbose )
213  cout << "Number of threads: " << system_processors() << endl;
214 
215  // Read image file
216  int dataflag(0);
217  if ( optind < argc - 1 ) dataflag = 1;
218  Bimage* p = read_img(argv[optind++], dataflag, -1);
219  if ( p == NULL ) bexit(-1);
220 
221  if ( nudatatype == Unknown_Type )
222  nudatatype = p->data_type(); // Preserve old type
223 
224  if ( sam.volume() ) p->sampling(sam);
225 
226  if ( set_origin ) {
227  if ( set_origin == 2 ) p->origin(p->size()/2);
228  else p->origin(origin);
229  }
230 
231  if ( filter_extremes == 1 ) p->filter_extremes(1);
232 
233  if ( filter_extremes == 2 ) p->filter_extremes();
234 
235  if ( replace_flag ) p->replace_maxima(replace_threshold);
236 
237  if ( average_kernel[0] > 0 ) p->filter_average(average_kernel);
238 
239  if ( sigma > 0 ) p->filter_gaussian(gauss_kernel, sigma);
240 
241  if ( median_kernel > 0 ) p->filter_rank(median_kernel, 0.5);
242 
243  if ( peak_kernel > 0 ) {
244  Bimage* pp = p->filter_peak(peak_kernel);
245  delete p;
246  p = pp;
247  }
248 
249  Bimage* pkernel = NULL;
250 
251  if ( var_kernel > 0 ) {
252  if ( std_flag & 2 ) {
253  Vector3<long> ksize(var_kernel, var_kernel, var_kernel);
254  ksize = ksize.min(p->size());
255  pkernel = new Bimage(Float, TSimple, ksize, 1);
256  pkernel->origin(ksize/2);
257  pkernel->sphere(ksize/2, var_kernel/2, 1, FILL_USER, 1);
258 // write_img("pk.mrc", pkernel);
259  p->variance(pkernel);
260  delete pkernel;
261  } else {
262  p->variance(var_kernel);
263  }
264  if ( std_flag & 1 ) p->square_root();
265  }
266 
267  if ( kernel_file.length() ) {
268  pkernel = read_img(kernel_file, 1, 0);
269  p->convolve(pkernel);
270 // write_img("t.krn", pkernel);
271  delete pkernel;
272  }
273 
274  if ( gradient ) p->filter_ortho(0);
275  if ( laplacian ) p->filter_ortho(1);
276 
277  if ( dog1 >0 && dog2 > 0 )
278  p->filter_dog(dog1, dog2);
279 
280  if ( sigma1 > 0 && sigma2 > 0 )
281  p->filter_bilateral(sigma1, sigma2, 1, 0);
282 
283  if ( ball_radius )
284  p->filter_rolling_ball(ball_radius, ball_scale);
285 
286  if ( dofft ) {
287  FourierType old_fourier_type = p->fourier_type();
288  if ( p->fourier_type() == NoTransform ) p->fft();
289 
290  if ( amp_threshold ) p->fspace_amp_threshold(amp_threshold);
291 
292  if ( phases_only ) p->fspace_amp_one();
293 
294  if ( Bfactor != 0 ) p->fspace_weigh_B_factor(Bfactor, res_hi);
295 
296  if ( log_sigma > 0 ) p->fspace_weigh_LoG(res_hi, log_sigma);
297 
298  if ( bandpass ) p->fspace_bandpass(res_hi, res_lo, bandpass_width);
299 
300  if ( old_fourier_type == NoTransform ) p->fft_back();
301  }
302 
303  if ( setinvert ) p->invert();
304 
305  if ( nustd > 0 ) p->rescale_to_avg_std(nuavg, nustd);
306 
307  if ( setminmax ) p->rescale_to_min_max(newmin, newmax);
308 
309  if ( optind < argc ) {
310  p->change_type(nudatatype);
311  write_img(argv[optind], p, 0);
312  }
313 
314  delete p;
315 
316 #ifdef HAVE_GCD
317  fftwf_cleanup_threads();
318 #endif
319 
320  if ( verbose & VERB_TIME )
321  timer_report(ti);
322 
323  bexit(0);
324 }
325 
Boption
A class for a command line input option.
Definition: options.h:26
Bimage::filter_gaussian
int filter_gaussian(long kernel_size, double sigma)
Applies a gaussian filter to an image.
Definition: Bimage_filter.cpp:324
Boption::scale
Vector3< double > scale()
Definition: options.h:190
FourierType
FourierType
Fourier transform format specifier.
Definition: Bimage.h:40
system_processors
size_t system_processors()
Returns the number of processors.
Definition: utilities.cpp:193
Bstring
Definition: Bstring.h:29
Bstring::integer
long integer()
Definition: Bstring.cpp:547
rwkernel.h
Header file for functions to read and write kernel files.
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
Bimage::variance
int variance(long kernel_size, int flag=0)
Calculates the local variance within the given kernel.
Definition: Bimage_stats.cpp:625
Vector3< double >
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
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
FILL_USER
#define FILL_USER
Definition: utilities.h:176
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
Bstring::real
double real()
Definition: Bstring.cpp:553
options.h
Header file for option handlers.
Bimage::fft_back
int fft_back()
Definition: Bimage.h:831
Boption::origin
Vector3< double > origin()
Definition: options.h:180
Bimage::filter_rank
int filter_rank(long kernel_size, double rank)
Applies a median filter to an image.
Definition: Bimage_filter.cpp:1021
Bimage::convolve
int convolve(Bimage *pkernel)
Convolves an image with an arbitrary size convolution filter.
Definition: Bimage_filter.cpp:473
Boption::tag
Bstring tag
Definition: options.h:29
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:77
TSimple
@ TSimple
Definition: Bimage.h:59
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:281
Boption::real
double real()
Definition: options.h:107
main
int main(int argc, char **argv)
Definition: bfilter.cpp:62
Bimage::fft
int fft()
Definition: Bimage.h:830
Bimage::fourier_type
FourierType fourier_type()
Definition: Bimage.h:380
Boption::value
Bstring value
Definition: options.h:30
Bimage
General image parameter class.
Definition: Bimage.h:177
timer.h
Utilities for timing functions.
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
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:496
Bimage::data_type
DataType data_type()
Definition: Bimage.h:362
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:1183
utilities.h
Header file for general utilities.
Bimage::fspace_amp_threshold
int fspace_amp_threshold(double threshold)
Filters the amplitudes of the Fourier transform of an image.
Definition: Bimage_fspace.cpp:368
Bstring::length
long length() const
Definition: Bstring.cpp:263
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:76
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
Vector3::volume
double volume()
Definition: Vector3.h:237
rwimg.h
Header file for 2D and 3D image I/O.
Bimage::fspace_amp_one
int fspace_amp_one()
Sets all amplitudes to one.
Definition: Bimage_fspace.cpp:323
verbose
int verbose
Bimage::filter_average
int filter_average(long kernel_size)
Applies an averaging filter to an image.
Definition: Bimage_filter.cpp:207
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
Boption::filename
Bstring filename()
Definition: options.h:85
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
Bimage::invert
void invert()
Inverts the data in the image.
Definition: Bimage.cpp:3111
Bimage::filter_peak
Bimage * filter_peak(long kernel_size)
Finds the peaks in an image.
Definition: Bimage_filter.cpp:1075
Vector3::min
Type min()
Definition: Vector3.h:169
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
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
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:1261
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
Bimage::filter_dog
int filter_dog(double sigma1, double sigma2)
Convolves the image with a difference of gausians kernel.
Definition: Bimage_filter.cpp:641
Bimage::filter_ortho
int filter_ortho(int type)
Convolves the image with an orthogonal kernel with wrapping.
Definition: Bimage_filter.cpp:544
Bimage::filter_rolling_ball
int filter_rolling_ball(long radius, double scale)
Apply a rolling ball filter.
Definition: Bimage_filter.cpp:835
Float
@ Float
Definition: utilities.h:161
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:783
NoTransform
@ NoTransform
Definition: Bimage.h:41
Bimage::filter_extremes
int filter_extremes()
Filters the extremes out of a micrograph image.
Definition: Bimage_segment.cpp:620
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:440
Bimage::replace_maxima
long replace_maxima(double threshold)
Replaces maxima above a threshold using local averages.
Definition: Bimage_segment.cpp:678
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