| Bsoft 2.0.5 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 string label = get_cmd_line(argc, argv);
106
107 if ( file_type(filename) == Micrograph ) {
108 while ( optind < argc ) string_add(&file_list, argv[optind++]);
109 project = read_project(file_list);
110 string_kill(file_list);
111 project_mass_normalize(project, avg, std, norm_type,
112 nudatatype, setinputZslices, setoutputZslices,
113 cutmin, cutmax, replace_threshold, label);
114 } else {
115 p = read_img(filename, 1, -1);
116 if ( p == NULL ) bexit(-1);
117
118 if ( nudatatype == Unknown_Type )
119 nudatatype = p->data_type();
120 else if ( nudatatype > p->data_type() )
121 p->change_type(nudatatype);
122
123 if ( setinputZslices )
124 if ( p->slices_to_images() < 0 ) bexit(-1);
125
126 if ( cutmin < cutmax ) p->truncate_to_min_max(cutmin, cutmax);
127
128 if ( replace_flag )
129 p->replace_maxima(replace_threshold);
130
131 if ( local > 0 )
132 p->normalize_local(local);
133 else if ( norm_type >= 0 )
134 p->normalize(avg, std, norm_type);
135
136 if ( setoutputZslices )
137 if ( p->images_to_slices() < 0 ) bexit(-1);
138
139 if ( optind < argc ) {
140 filename = argv[++optind];
141 if ( p->data_type() != nudatatype ) {
142 if ( nudatatype == UCharacter ) p->truncate_to_min_max(0, 255);
143 p->change_type(nudatatype);
144 }
145 p->label(label);
146 write_img(filename, p, 0);
147 }
148 delete p;
149
150 file_list = &filename;
151 if ( paramfile.length() && filename.length() )
152 project = project_create_from_images(file_list, "mg");
153 }
154
155 if ( project && paramfile.length() ) {
156 project_update_comment(project, argc, argv);
157 if ( verbose )
158 cout << "Writing " << paramfile << endl;
159 write_project(paramfile, project, 0, 0);
160 }
161
162 project_kill(project);
163
164 if ( verbose & VERB_TIME )
165 timer_report(ti);
166
167 bexit(0);
168 }
169
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
write_project
int write_project(const char *filename, Bproject *project)
Writing micrograph parameters to a file.
Definition: rwmg.cpp:320
rwmg.h
Header file for reading and writing micrograph parameters.
Bstring::real
double real()
Definition: Bstring.cpp:545
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:705
DataType
DataType
Base data type specifier.
Definition: utilities.h:146
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
Bimage::slices_to_images
int slices_to_images()
Changes the slices in a 3D image into a set of 2D images.
Definition: Bimage.cpp:2052
utilities.h
Header file for general utilities.
Boption::value
Bstring value
Definition: options.h:30
Boption::tag
Bstring tag
Definition: options.h:29
mg_img_proc.h
Header file for image processing from micrograph structures.
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:224
file_type
FileType file_type(const char *filename)
Checks the file type using the extension and contents.
Definition: file_util.cpp:178
read_project
Bproject * read_project(const char *filename, int flags=0)
Reading micrograph parameters from files.
Definition: rwmg.cpp:140
Bstring::length
long length() const
Definition: Bstring.cpp:263
project
Bproject * project
Definition: bshow.cpp:31
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
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
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
timer.h
Utilities for timing functions.
Bimage
General image parameter class.
Definition: Bimage.h:163
verbose
int verbose
Micrograph
Definition: file_util.h:23
main
int main(int argc, char **argv)
Definition: bnorm.cpp:48
Boption
A class for a command line input option.
Definition: options.h:26
Bimage::label
void label(string s)
Definition: Bimage.h:235
VERB_TIME
#define VERB_TIME
Definition: utilities.h:110
mg_tomography.h
Header file for single particle image processing in Bsoft.
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:280
file_util.h
Header file for file checking functions.
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
UCharacter
Definition: utilities.h:149
Bstring
Definition: Bstring.h:29
Bimage::images_to_slices
int images_to_slices()
Changes the 2D images to slices in a 3D image.
Definition: Bimage.cpp:2093
project_kill
int project_kill(Bproject *project)
Frees a whole project.
Definition: mg_processing.cpp:1224
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:67
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
mg_processing.h
Header file for micrograph processing.
Unknown_Type
Definition: utilities.h:147
rwimg.h
Header file for 2D and 3D image I/O.
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, string &label)
Normalizes a set of images in a project to a desired average and standard deviation.
Definition: mg_tomography.cpp:379
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
use
const char * use[]
Definition: bnorm.cpp:23
project_update_comment
int project_update_comment(Bproject *project, int n, char **strings)
Puts a set of strings and time in the main comment of a project.
Definition: rwmg.cpp:468
Bimage::data_type
DataType data_type()
Definition: Bimage.h:349
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::normalize_local
int normalize_local(long kernel_size)
Normalizes by subtracting local average and dividing by local standard deviation.
Definition: Bimage_rescale.cpp:608
Bproject
Definition: mg_processing.h:333
string_kill
int string_kill(Bstring *list)
Kills a string linked list.
Definition: Bstring.cpp:760