 |
Bsoft
2.1.0
Bernard's software package
|
Go to the documentation of this file.
24 "Usage: bsupix [options] input.img output.img",
25 "--------------------------------------------",
26 "Segments an image into superpixels.",
29 "-average 7,5,3 Averaging/smoothing filter: kernel size.",
30 "-variance 11 Calculate a local variance image using the given size kernel.",
31 "-superpixels 123 Segment into superpixels with the given step size.",
32 "-impose 3 Assign segment feature:",
33 " 1: segment average",
34 " 2: lowest neighboring segment average",
35 " 3: difference from lowest neighboring segment average",
36 " 4: highest neighboring segment average",
37 " 5: difference from highest neighboring segment average",
38 "-colorize Generate random segment colors.",
39 "-pattern neigbor Calculate local or neighbor binary pattern.",
42 "-verbose 7 Verbose output.",
43 "-datatype u Force writing of a new data type.",
45 "Parameters for superpixel segmentation:",
46 "-iterations 50 Number of segmentation iterations (default 10).",
47 "-bin 3 Number of binning levels (default 1).",
48 "-weight 0.5 Color weight (default 0.2).",
49 "-stop 1.5 Stopping condition (default 1%).",
54 "-maskin mask.mrc Multi-level input mask.",
55 "-input segfile.json Segmentation input file.",
58 "-maskout mask.mrc Multi-level output mask.",
59 "-output segfile.json Segmentation output file.",
64 int main(
int argc,
char* argv[])
70 double colorweight(0.2);
85 for ( curropt = option; curropt; curropt = curropt->
next ) {
86 if ( curropt->
tag ==
"average" ) {
87 if ( ( i = curropt->
values(average_kernel[0],
88 average_kernel[1], average_kernel[2]) ) < 1 )
89 cerr <<
"-average: The kernel edge size must be specified!" << endl;
91 if ( i < 2 ) average_kernel[2] = average_kernel[1] = average_kernel[0];
93 if ( curropt->
tag ==
"variance" )
94 if ( ( var_kernel = curropt->
integer() ) < 2 )
95 cerr <<
"-variance: The kernel edge size must be specified!" << endl;
96 if ( curropt->
tag ==
"superpixels" )
97 if ( ( step = curropt->
integer() ) < 2 )
98 cerr <<
"-superpixels: A step size greater than 1 must be specified!" << endl;
99 if ( curropt->
tag ==
"impose" )
100 if ( ( impose = curropt->
integer() ) < 1 )
101 cerr <<
"-impose: One option must be specified!" << endl;
102 if ( curropt->
tag ==
"colorize" )
104 if ( curropt->
tag ==
"datatype" )
106 if ( curropt->
tag ==
"iterations" )
107 if ( ( iterations = curropt->
integer() ) < 1 )
108 cerr <<
"-iterations: At least one iteration must be specified!" << endl;
109 if ( curropt->
tag ==
"bin" )
110 if ( ( bin = curropt->
integer() ) < 1 )
111 cerr <<
"-bin: At number of binning levels must be specified!" << endl;
112 if ( curropt->
tag ==
"weight" )
113 if ( ( colorweight = curropt->
real() ) < 0.001 )
114 cerr <<
"-weight: A color weight must be specified!" << endl;
115 if ( curropt->
tag ==
"stop" )
116 if ( ( stop = curropt->
real() ) < 0.001 )
117 cerr <<
"-stop: A stopping percentage must be specified!" << endl;
118 if ( curropt->
tag ==
"maskin" )
120 if ( curropt->
tag ==
"maskout" )
122 if ( curropt->
tag ==
"input" )
124 if ( curropt->
tag ==
"output" )
133 if ( optind < argc - 1 || maskin.
length() ) dataflag = 1;
137 cerr <<
"Error: No input file read!" << endl;
141 if ( !dataflag )
bexit(0);
147 if ( var_kernel > 1 ) p->
variance(var_kernel);
154 cerr <<
"Error: The image and mask must be the same size!" << endl;
159 vector<Bsuperpixel> seg;
163 cout <<
"Reading " << segin << endl;
174 seg = p->
superpixels(step, colorweight, iterations, stop);
176 seg = p->
superpixels(step, colorweight, iterations, bin, stop);
183 cout <<
"Writing " << segout << endl << endl;
185 string fn(segout.
c_str());
192 else if ( impose == 1 )
202 Bstring filename(argv[optind]);
203 if ( optind < argc ) {
206 if ( impose == 1 && p->
next ) {
223 vector<Bsuperpixel> seg;
225 vector<JSvalue> splist = root.
array();
227 for (
auto it = splist.begin(); it != splist.end(); ++it ) {
230 vector<double> coor = jv[
"coordinates"].
array_real();
231 vector<double> avg = jv[
"channel_averages"].
array_real();
232 vector<double> var = jv[
"channel_variances"].
array_real();
235 sp.
index(jv[
"id"].integer());
236 sp.
count(jv[
"count"].integer());
240 sp.
weight(jv[
"weight"].real());
246 cout <<
"Superpixels: " << seg.size() << endl << endl;
264 vector<JSvalue> splist;
266 for (
auto it = seg.begin(); it != seg.end(); ++it ) {
267 map<string, JSvalue> sp;
268 sp[
"id"] = it->index();
269 sp[
"count"] = it->count();
270 sp[
"coordinates"] =
JSvalue(it->coordinates());
271 sp[
"channel_averages"] =
JSvalue(it->channels());
272 sp[
"channel_variances"] =
JSvalue(it->variances());
273 sp[
"weight"] = it->weight();
275 splist.push_back(sp);
A class for a command line input option.
int level_masked_stats(Bimage *pmask)
Caclulates statistics for all the regions defined by a multi-level mask.
void neighbor(long i, long j)
int variance(long kernel_size, int flag=0)
Calculates the local variance within the given kernel.
int bexit(int value)
Exit function for cleanup and debugging.
Boption * get_option_list(const char *use[], int argc, char *argv[], int &optind)
Parses command line arguments based on a template.
Header file for option handlers.
void variances(vector< double > &v)
double standard_deviation()
vector< JSvalue > & array()
long values(T1 &v1, T2 &v2)
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
vector< Bsuperpixel > superpixel_from_json(JSvalue &root)
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
vector< Bsuperpixel > superpixels(long step, double colorweight=0.2, long iterations=10, double stop=1)
Segment the image into superpixels.
JSvalue json_from_superpixel(vector< Bsuperpixel > &seg)
General image parameter class.
Utilities for timing functions.
void coordinates(vector< double > &c)
int main(int argc, char *argv[])
void push_back(const JSvalue &val)
double timer_start()
Starts timer and prints the time.
Header file for general utilities.
vector< double > array_real()
int impose_superpixels(Bimage *pmask, vector< Bsuperpixel > &seg, int impose)
Impose superpixel features onto an image.
double timer_report(double lasttime)
Reports the time elapsed since a given time.
vector< long > array_integer()
void change_type(char letter)
Get the data type indicated by a single letter code.
Header file for 2D and 3D image I/O.
int filter_average(long kernel_size)
Applies an averaging filter to an image.
int option_kill(Boption *option)
Deallocates a linked list of option structures.
JSvalue jsvalue_from_neighbors(Bsuperpixel &sp)
DataType
Base data type specifier.
long levelmask_colorize()
Colorizes a multi-level mask with random color assignments.
void neighbors(vector< long > &v)
void write(string filename)
long statistics()
Calculates the statistics for an image.
bool check_if_same_size(Bimage *p)
Check if two images are the same size.