Bsoft  2.1.0
Bernard's software package
bnorm.cpp
Go to the documentation of this file.
1 
9 #include "rwimg.h"
10 #include "rwmg.h"
11 #include "mg_processing.h"
12 #include "mg_tomography.h"
13 #include "mg_img_proc.h"
14 #include "file_util.h"
15 #include "utilities.h"
16 #include "options.h"
17 #include "timer.h"
18 
19 // Declaration of global variables
20 extern int verbose; // Level of output to the screen
21 
22 // Usage assistance
23 const char* use[] = {
24 " ",
25 "Usage: bnorm [options] input.star/input.img [input2.star/output.img]",
26 "--------------------------------------------------------------------",
27 "Normalizes a set of images based on their histograms.",
28 " ",
29 "Actions:",
30 "-replacemaxima 57.5 Replace maxima above the given threshold with surrounding average.",
31 "-type simple Normalization type: simple, Gaussian or Poisson.",
32 "-local 25 Normalization using local average and standard deviation.",
33 "-truncate -0.5,1.2 Truncate data to minimum and maximum before mass normalization.",
34 "-rescale 0.5,1.5 Average and standard deviation for output.",
35 "-images Input slices of a single 3D image as 2D images.",
36 "-slices Output 2D images as z-slices of a single 3D image.",
37 " ",
38 "Parameters:",
39 "-verbose 7 Verbosity of output.",
40 "-datatype u Force writing of a new data type.",
41 " ",
42 "Output:",
43 "-output file.star Output parameter file.",
44 " ",
45 NULL
46 };
47 
48 int main(int argc, char **argv)
49 {
50  // Initialize variables
51  int norm_type(-1); // Normalization type
52  int local(0); // Local normalization kernel size
53  DataType nudatatype(Unknown_Type); // Conversion to new type
54  double replace_threshold(0); // Threshold for replacing maxima
55  int replace_flag(0); // Flag for replacing maxima
56  int setinputZslices(0); // Interpret 3D slices as separate 2D images
57  int setoutputZslices(0); // Interpret 3D slices as separate 2D images
58  double cutmin(0), cutmax(0); // Truncation
59  double avg(0), std(0); // Average and standard deviation for output
60  Bstring paramfile; // Output parameter file
61 
62  int optind;
63  Boption* option = get_option_list(use, argc, argv, optind);
64  Boption* curropt;
65  for ( curropt = option; curropt; curropt = curropt->next ) {
66  if ( curropt->tag == "replacemaxima" ) {
67  if ( ( replace_threshold = curropt->value.real() ) < 0.0000001 )
68  cerr << "-replacemaxima: A threshold must be specified!" << endl;
69  else
70  replace_flag = 1;
71  }
72  if ( curropt->tag == "type" ) {
73  if ( curropt->value[0] == 's' || curropt->value[0] == 'S' ) norm_type = 0;
74  if ( curropt->value[0] == 'g' || curropt->value[0] == 'G' ) norm_type = 1;
75  if ( curropt->value[0] == 'p' || curropt->value[0] == 'P' ) norm_type = 2;
76  }
77  if ( curropt->tag == "local" )
78  if ( ( local = curropt->value.integer() ) < 3 )
79  cerr << "-local: A kernel size must be specified!" << endl;
80  if ( curropt->tag == "datatype" )
81  nudatatype = curropt->datatype();
82  if ( curropt->tag == "truncate" ) {
83  if ( curropt->values(cutmin, cutmax) < 2 )
84  cerr << "-truncate: Both min and max must be specified!" << endl;
85  if ( cutmin > cutmax ) swap(cutmin, cutmax);
86  }
87  if ( curropt->tag == "rescale" )
88  if ( curropt->values(avg, std) < 2 ) {
89  cerr << "-rescale: Both the average and standard deviation must be specified!" << endl;
90  bexit(-1);
91  }
92  if ( curropt->tag == "images" ) setinputZslices = 1;
93  if ( curropt->tag == "slices" ) setoutputZslices = 1;
94  if ( curropt->tag == "output" )
95  paramfile = curropt->filename();
96  }
97  option_kill(option);
98 
99  double ti = timer_start();
100 
101  Bproject* project = NULL;
102  Bimage* p = NULL;
103  Bstring filename = argv[optind];
104  Bstring* file_list = NULL;
105 
106  if ( file_type(filename) == Micrograph ) {
107  while ( optind < argc ) string_add(&file_list, argv[optind++]);
108  project = read_project(file_list);
109  string_kill(file_list);
110  project_mass_normalize(project, avg, std, norm_type,
111  nudatatype, setinputZslices, setoutputZslices,
112  cutmin, cutmax, replace_threshold);
113  } else {
114  p = read_img(filename, 1, -1);
115  if ( p == NULL ) bexit(-1);
116 
117  if ( nudatatype == Unknown_Type )
118  nudatatype = p->data_type();
119  else if ( nudatatype > p->data_type() )
120  p->change_type(nudatatype);
121 
122  if ( setinputZslices )
123  if ( p->slices_to_images() < 0 ) bexit(-1);
124 
125  if ( cutmin < cutmax ) p->truncate_to_min_max(cutmin, cutmax);
126 
127  if ( replace_flag )
128  p->replace_maxima(replace_threshold);
129 
130  if ( local > 0 )
131  p->normalize_local(local);
132  else if ( norm_type >= 0 )
133  p->normalize(avg, std, norm_type);
134 
135  if ( setoutputZslices )
136  if ( p->images_to_slices() < 0 ) bexit(-1);
137 
138  if ( optind < argc ) {
139  filename = argv[++optind];
140  if ( p->data_type() != nudatatype ) {
141  if ( nudatatype == UCharacter ) p->truncate_to_min_max(0, 255);
142  p->change_type(nudatatype);
143  }
144  write_img(filename, p, 0);
145  }
146  delete p;
147 
148  file_list = &filename;
149  if ( paramfile.length() && filename.length() )
150  project = project_create_from_images(file_list, "mg");
151  }
152 
153  if ( project && paramfile.length() ) {
154  if ( verbose )
155  cout << "Writing " << paramfile << endl;
156  write_project(paramfile, project, 0, 0);
157  }
158 
160 
161  if ( verbose & VERB_TIME )
162  timer_report(ti);
163 
164  bexit(0);
165 }
166 
Boption
A class for a command line input option.
Definition: options.h:26
project_kill
int project_kill(Bproject *project)
Frees a whole project.
Definition: mg_processing.cpp:429
string_kill
int string_kill(Bstring *list)
Kills a string linked list.
Definition: Bstring.cpp:768
UCharacter
@ UCharacter
Definition: utilities.h:153
file_util.h
Header file for file checking functions.
Bstring
Definition: Bstring.h:29
Bstring::integer
long integer()
Definition: Bstring.cpp:547
Boption::datatype
DataType datatype()
Definition: options.h:93
project_mass_normalize
int project_mass_normalize(Bproject *project, double avg, double std, int norm_type, DataType datatype, int setinputZslices, int setoutputZslices, double cutmin, double cutmax, double replace_threshold)
Normalizes a set of images in a project to a desired average and standard deviation.
Definition: mg_tomography.cpp:379
Boption::next
Boption * next
Definition: options.h:28
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
Bimage::normalize
int normalize(long imgnum, double average, double stdev, int norm_type, long bins)
Normalizes a sub-image to a desired average and standard deviation.
Definition: Bimage_rescale.cpp:481
Bstring::real
double real()
Definition: Bstring.cpp:553
options.h
Header file for option handlers.
Bimage::images_to_slices
int images_to_slices()
Changes the 2D images to slices in a 3D image.
Definition: Bimage.cpp:2115
project
Bproject * project
Definition: bshow.cpp:33
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
rwmg.h
Header file for reading and writing micrograph parameters.
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:281
main
int main(int argc, char **argv)
Definition: bnorm.cpp:48
Bimage::slices_to_images
int slices_to_images()
Changes the slices in a 3D image into a set of 2D images.
Definition: Bimage.cpp:2074
Micrograph
@ Micrograph
Definition: file_util.h:23
Bimage::normalize_local
int normalize_local(long kernel_size)
Normalizes by subtracting local average and dividing by local standard deviation.
Definition: Bimage_rescale.cpp:608
Boption::value
Bstring value
Definition: options.h:30
project_create_from_images
Bproject * project_create_from_images(Bstring *file_list, Bstring type)
Creates a project structure using image file names.
Definition: mg_img_proc.cpp:230
Bimage
General image parameter class.
Definition: Bimage.h:177
timer.h
Utilities for timing functions.
Bproject
Definition: mg_processing.h:336
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
verbose
int verbose
mg_img_proc.h
Header file for image processing from micrograph structures.
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
Bimage::truncate_to_min_max
int truncate_to_min_max(double minim, double maxim)
Truncates image data to a given minimum and maximum.
Definition: Bimage_rescale.cpp:331
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
read_project
Bproject * read_project(const char *filename, int flags=0)
Reading micrograph parameters from files.
Definition: rwmg.cpp:141
Bimage::data_type
DataType data_type()
Definition: Bimage.h:362
utilities.h
Header file for general utilities.
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::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
rwimg.h
Header file for 2D and 3D image I/O.
write_project
int write_project(const char *filename, Bproject *project)
Writing micrograph parameters to a file.
Definition: rwmg.cpp:321
mg_processing.h
Header file for micrograph processing.
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
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
use
const char * use[]
Definition: bnorm.cpp:23
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:713
mg_tomography.h
Header file for single particle image processing in Bsoft.
file_type
FileType file_type(const char *filename)
Checks the file type using the extension and contents.
Definition: file_util.cpp:178
Bimage::replace_maxima
long replace_maxima(double threshold)
Replaces maxima above a threshold using local averages.
Definition: Bimage_segment.cpp:678