| Bsoft 2.0.5 Bernard's software package

bsupix.cpp
Go to the documentation of this file.
1
9 #include "rwimg.h"
10 #include "options.h"
11 #include "utilities.h"
12 #include "timer.h"
13
14
15 // Declaration of global variables
16 extern int verbose; // Level of output to the screen
17
18 vector<Bsuperpixel> superpixel_from_json(JSvalue& root);
19 JSvalue json_from_superpixel(vector<Bsuperpixel>& seg);
20
21 /* Usage assistence */
22 const char* use[] = {
23 " ",
24 "Usage: bsupix [options] input.img output.img",
25 "----------------------------------------------",
26 "Segments an image into superpixels.",
27 " ",
28 "Actions:",
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.",
40 " ",
41 "Parameters:",
42 "-verbose 7 Verbose output.",
43 "-datatype u Force writing of a new data type.",
44 " ",
45 "Parameters for superpixel segmentation:",
46 "-iterations 50 Number of segmentation iterations (default 10).",
47 "-weight 0.5 Color weight (default 0.2).",
48 "-stop 1.5 Stopping condition (default 1%).",
49 " ",
50 "Input:",
51 "-mask file.pif Multi-level mask.",
52 "-input segfile.json Segmentation input file.",
53 " ",
54 "Output:",
55 "-output segfile.json Segmentation output file.",
56 " ",
57 NULL
58 };
59
60 int main(int argc, char* argv[])
61 {
62 // Initialize variables
63 Vector3<long> average_kernel; // Average filter kernel size
64 long var_kernel(0); // Variance filter kernel size
65 long step(0); // Superpixel step size
66 double colorweight(0.2); // Color weighting
67 long iterations(10); // Segmentation iterations
68 double stop(1); // Stopping condition
69 int impose(0); // Flag to impose segment colors
70 int colorize(0); // Generate color mask
71 DataType nudatatype(Unknown_Type); // Conversion to new type
72 Bstring maskfile; // Multi-level mask file name
73 Bstring segin; // Segmentation input file name
74 Bstring segout; // Segmentation output file name
75
76 int i, optind;
77 Boption* option = get_option_list(use, argc, argv, optind);
78 Boption* curropt;
79 for ( curropt = option; curropt; curropt = curropt->next ) {
80 if ( curropt->tag == "average" ) {
81 if ( ( i = curropt->values(average_kernel[0],
82 average_kernel[1], average_kernel[2]) ) < 1 )
83 cerr << "-average: The kernel edge size must be specified!" << endl;
84 else
85 if ( i < 2 ) average_kernel[2] = average_kernel[1] = average_kernel[0];
86 }
87 if ( curropt->tag == "variance" )
88 if ( ( var_kernel = curropt->integer() ) < 2 )
89 cerr << "-variance: The kernel edge size must be specified!" << endl;
90 if ( curropt->tag == "superpixels" )
91 if ( ( step = curropt->integer() ) < 2 )
92 cerr << "-superpixels: A step size greater than 1 must be specified!" << endl;
93 if ( curropt->tag == "impose" )
94 if ( ( impose = curropt->integer() ) < 1 )
95 cerr << "-impose: One option must be specified!" << endl;
96 if ( curropt->tag == "colorize" )
97 colorize = 1;
98 if ( curropt->tag == "datatype" )
99 nudatatype = curropt->datatype();
100 if ( curropt->tag == "iterations" )
101 if ( ( iterations = curropt->integer() ) < 1 )
102 cerr << "-iterations: At least one iteration must be specified!" << endl;
103 if ( curropt->tag == "weight" )
104 if ( ( colorweight = curropt->real() ) < 0.001 )
105 cerr << "-weight: A color weight must be specified!" << endl;
106 if ( curropt->tag == "stop" )
107 if ( ( stop = curropt->real() ) < 0.001 )
108 cerr << "-stop: A stopping percentage must be specified!" << endl;
109 if ( curropt->tag == "mask" )
110 maskfile = curropt->filename();
111 if ( curropt->tag == "input" )
112 segin = curropt->filename();
113 if ( curropt->tag == "output" )
114 segout = curropt->filename();
115 }
116 option_kill(option);
117
118 double ti = timer_start();
119
120 // Read the input files
121 int dataflag = 0;
122 if ( optind < argc - 1 ) dataflag = 1;
123 Bimage* p = read_img(argv[optind++], dataflag, -1);
124
125 if ( !p ) {
126 cerr << "Error: No input file read!" << endl;
127 bexit(-1);
128 }
129
130 if ( !dataflag ) bexit(0);
131
132 if ( p->standard_deviation() <= 0 ) p->statistics();
133
134 if ( average_kernel[0] > 0 ) p->filter_average(average_kernel);
135
136 if ( var_kernel > 1 ) p->variance(var_kernel);
137
138 vector<Bsuperpixel> seg;
139 Bimage* pmask = NULL;
140
141 if ( maskfile.length() ) {
142 pmask = read_img(maskfile, 1, -1);
143 if ( !p->check_if_same_size(pmask) ) {
144 cerr << "Error: The image and mask must be the same size!" << endl;
145 bexit(-1);
146 }
147 }
148
149 if ( segin.length() ) {
150 if ( verbose )
151 cout << "Reading " << segin << endl;
152 JSvalue sp = JSparser().parse(segin.c_str());
153 seg = superpixel_from_json(sp);
154 /* if ( pmask )
155 vector<long> vstep = {step,step,step};
156 vstep *= 2;
157 if ( vstep[2] > z ) vstep[2] = z;
158 p->superpixels_update(pmask, vstep, colorweight, seg);
159 }*/
160 } else {
161 seg = p->superpixels(step, colorweight, iterations, stop);
162 pmask = p->next;
163 }
164
165 if ( segout.length() ) {
166 if ( verbose )
167 cout << "Writing " << segout << endl << endl;
168 JSvalue sp = json_from_superpixel(seg);
169 string fn(segout.c_str());
170 sp.write(fn);
171 }
172
173 if ( impose ) {
174 p->impose_superpixels(pmask, seg, impose);
175 } else {
176 p->next = NULL;
177 delete p;
178 p = pmask;
179 }
180
181 if ( colorize ) pmask->levelmask_colorize();
182
183 // Write an output multilevel mask file if a file name is given
184 if ( optind < argc ) {
185 p->change_type(nudatatype);
186 p->label(get_cmd_line(argc, argv));
187 write_img(argv[optind], p, 0);
188 }
189
190 delete p;
191
192 if ( verbose & VERB_TIME )
193 timer_report(ti);
194
195 return 0;
196 }
197
198 vector<Bsuperpixel> superpixel_from_json(JSvalue& root)
199 {
200 vector<Bsuperpixel> seg;
201
202 vector<JSvalue> splist = root.array();
203
204 for ( auto it = splist.begin(); it != splist.end(); ++it ) {
205 JSvalue& jv = *it;
206 Bsuperpixel sp;
207 vector<double> coor = jv["coordinates"].array_real();
208 vector<double> avg = jv["channel_averages"].array_real();
209 vector<long> nb = jv["neighbors"].array_integer();
210 sp.channels(avg.size());
211 sp.index(jv["id"].integer());
212 sp.count(jv["count"].integer());
213 sp.coordinates(coor);
214 sp.channels(avg);
215 sp.weight(jv["weight"].real());
216 sp.neighbors(nb);
217 seg.push_back(sp);
218 }
219
220 if ( verbose )
221 cout << "Superpixels: " << seg.size() << endl << endl;
222
223 return seg;
224 }
225
226 JSvalue jsvalue_from_neighbors(Bsuperpixel& sp)
227 {
228 vector<JSvalue> vec;
229
230 for ( long j=0; j<NNEIGHBOR && sp.neighbor(j) >= 0; ++j )
231 vec.push_back(sp.neighbor(j));
232
233 return JSvalue(vec);
234 }
235
236
237 JSvalue json_from_superpixel(vector<Bsuperpixel>& seg)
238 {
239 vector<JSvalue> splist;
240
241 for ( auto it = seg.begin(); it != seg.end(); ++it ) {
242 map<string, JSvalue> sp;
243 sp["id"] = it->index();
244 sp["count"] = it->count();
245 sp["coordinates"] = JSvalue(it->coordinates());
246 sp["channel_averages"] = JSvalue(it->channels());
247 sp["weight"] = it->weight();
248 sp["neighbors"] = jsvalue_from_neighbors(*it);
249 splist.push_back(sp);
250 }
251
252 return splist;
253 }
254
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
DataType
DataType
Base data type specifier.
Definition: utilities.h:146
Bsuperpixel::coordinates
void coordinates(vector< double > &c)
Definition: Bsuperpixel.h:32
superpixel_from_json
vector< Bsuperpixel > superpixel_from_json(JSvalue &root)
Definition: bsupix.cpp:198
utilities.h
Header file for general utilities.
Boption::tag
Bstring tag
Definition: options.h:29
Boption::real
double real()
Definition: options.h:107
Bimage::impose_superpixels
int impose_superpixels(Bimage *pmask, vector< Bsuperpixel > &seg, int impose)
Impose superpixel features onto an image.
Definition: Bimage_segment.cpp:1872
Bsuperpixel::count
void count(long i)
Definition: Bsuperpixel.h:30
verbose
int verbose
Bstring::length
long length() const
Definition: Bstring.cpp:263
Bsuperpixel::channels
void channels(long nc)
Definition: Bsuperpixel.h:34
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
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
Bsuperpixel
Definition: Bsuperpixel.h:15
timer.h
Utilities for timing functions.
Bimage
General image parameter class.
Definition: Bimage.h:163
JSvalue::array
vector< JSvalue > & array()
Definition: json.h:151
JSvalue::array_real
vector< double > array_real()
Definition: json.h:166
Boption::integer
long integer()
Definition: options.h:100
Bimage::filter_average
int filter_average(long kernel_size)
Applies an averaging filter to an image.
Definition: Bimage_filter.cpp:208
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22
Boption
A class for a command line input option.
Definition: options.h:26
json_from_superpixel
JSvalue json_from_superpixel(vector< Bsuperpixel > &seg)
Definition: bsupix.cpp:237
Bimage::label
void label(string s)
Definition: Bimage.h:235
VERB_TIME
#define VERB_TIME
Definition: utilities.h:110
use
const char * use[]
Definition: bsupix.cpp:22
main
int main(int argc, char *argv[])
Definition: bsupix.cpp:60
Bimage::levelmask_colorize
long levelmask_colorize()
Colorizes a multi-level mask with random color assignments.
Definition: Bimage_mask.cpp:1887
Bsuperpixel::neighbor
void neighbor(long i, long j)
Definition: Bsuperpixel.h:43
JSvalue::write
void write(string &filename)
Definition: json.h:345
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:280
JSvalue
Definition: json.h:53
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
Bimage::standard_deviation
double standard_deviation()
Definition: Bimage.h:537
Bimage::next
Bimage * next
Definition: Bimage.h:165
jsvalue_from_neighbors
JSvalue jsvalue_from_neighbors(Bsuperpixel &sp)
Definition: bsupix.cpp:226
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
Bsuperpixel::neighbors
void neighbors(vector< long > &v)
Definition: Bsuperpixel.h:45
Bstring
Definition: Bstring.h:29
Bsuperpixel::index
void index(long i)
Definition: Bsuperpixel.h:28
Vector3< long >
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:67
JSvalue::array_integer
vector< long > array_integer()
Definition: json.h:159
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:109
JSparser
Definition: json.h:358
Bimage::check_if_same_size
bool check_if_same_size(Bimage *p)
Check if two images are the same size.
Definition: Bimage.cpp:536
Unknown_Type
Definition: utilities.h:147
JSparser::parse
JSvalue parse()
Definition: json.h:388
rwimg.h
Header file for 2D and 3D image I/O.
options.h
Header file for option handlers.
NNEIGHBOR
#define NNEIGHBOR
Definition: Bsuperpixel.h:13
JSvalue::push_back
void push_back(const JSvalue &val)
Definition: json.h:191
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
Bsuperpixel::weight
void weight(double w)
Definition: Bsuperpixel.h:41
Bimage::superpixels
vector< Bsuperpixel > superpixels(long step, double colorweight=0.2, long iterations=10, double stop=1)
Segment the image into superpixels.
Definition: Bimage_segment.cpp:1812