1 /* GCoptimization.h */
\r
2 /* Olga Veksler, 2005, olga@csd.uwo.ca */
\r
3 /* Copyright 2005 Olga Veksler (olga@csd.uwo.ca)*/
\r
4 /* email olga@csd.uwo.ca for any questions, suggestions and bug reports
\r
7 /***************** IMPORTANT!!!!!!!!!!!***********************************************************
\r
9 If you use this software, YOU HAVE TO REFERENCE (at least) 3 papers, the citations [1]
\r
12 /****************************************************************************************************/
\r
14 /******************************************************************************/
\r
15 /* For description and example usage see GC_README.TXT */
\r
16 /******************************************************************************/
\r
19 This software library implements the Graph Cuts Energy Minimization methods
\r
23 [1] Efficient Approximate Energy Minimization via Graph Cuts
\r
24 Yuri Boykov, Olga Veksler, Ramin Zabih,
\r
25 IEEE transactions on PAMI, vol. 20, no. 12, p. 1222-1239, November 2001.
\r
29 This software can be used only for research purposes, you should cite
\r
30 the aforementioned paper in any resulting publication.
\r
31 If you wish to use this software (or the algorithms described in the aforementioned paper)
\r
32 for commercial purposes, you should be aware that there is a US patent:
\r
34 R. Zabih, Y. Boykov, O. Veksler,
\r
35 "System and method for fast approximate energy minimization via graph cuts ",
\r
36 United Stated Patent 6,744,923, June 1, 2004
\r
40 /* Together with the library implemented by O. Veksler, we provide, with the permission of the
\r
41 V. Kolmogorov and Y. Boykov the following libraries:
\r
44 1) energy.h, which was developed by Vladimir Kolmogorov and implements binary energy minimization
\r
45 technique described in
\r
47 [2] What Energy Functions can be Minimized via Graph Cuts?
\r
48 Vladimir Kolmogorov and Ramin Zabih.
\r
49 To appear in IEEE Transactions on Pattern Analysis and Machine Intelligence (PAMI).
\r
50 Earlier version appeared in European Conference on Computer Vision (ECCV), May 2002.
\r
53 We use "energy.h" to implement the binary energy minimization step
\r
54 for the alpha-expansion and swap algorithms. The graph construction provided by "energy.h" is
\r
55 more efficient (and slightly more general) than the original graph construction for the
\r
56 alpha-expansion algorithm in the paper cited as [1]
\r
59 This software can be used only for research purposes. IF YOU USE THIS SOFTWARE,
\r
60 YOU SHOULD CITE THE AFOREMENTIONED PAPER [2] IN ANY RESULTING PUBLICATION.
\r
64 2) graph.h, block.h, maxflow.cpp
\r
66 This software library implements the maxflow algorithm
\r
69 [3] An Experimental Comparison of Min-Cut/Max-Flow Algorithms
\r
70 for Energy Minimization in Vision.
\r
71 Yuri Boykov and Vladimir Kolmogorov.
\r
72 In IEEE Transactions on Pattern Analysis and Machine Intelligence (PAMI),
\r
75 This algorithm was developed by Yuri Boykov and Vladimir Kolmogorov
\r
76 at Siemens Corporate Research. To make it available for public use,
\r
77 it was later reimplemented by Vladimir Kolmogorov based on open publications.
\r
79 If you use this software for research purposes, you should cite
\r
80 the aforementioned paper in any resulting publication.
\r
83 /* These 4 files (energy.h,graph.h, block.h, maxflow.cpp) are included in the curent library with permission of
\r
84 Vladimir Kolmogorov and Yuri Boykov. They are included in a separate subdirectory named NotVekslerCode.
\r
85 The can also be downloaded independently from Vladimir Kolmogorov's
\r
86 website:http://research.microsoft.com/~vnk/
\r
87 Please Note that Vladimir's website is likely to move in the future */
\r
90 /****************************************************************************************************/
\r
93 This program is free software; you can redistribute it and/or modify
\r
94 it under the terms of the GNU General Public License as published by
\r
95 the Free Software Foundation; either version 2 of the License, or
\r
96 (at your option) any later version.
\r
98 This program is distributed in the hope that it will be useful,
\r
99 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
100 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
101 GNU General Public License for more details.
\r
103 You should have received a copy of the GNU General Public License
\r
104 along with this program; if not, write to the Free Software
\r
105 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
110 #ifndef __GCOPTIMIZATION_H__
\r
111 #define __GCOPTIMIZATION_H__
\r
113 #include "LinkedBlockList.h"
\r
114 #include "bagon_assert.h" // <assert.h>
\r
116 #include "energy.h"
\r
117 #define m_datacost(pix,lab) (m_datacost[(pix)*m_num_labels+(lab)] )
\r
118 #define m_smoothcost(lab1,lab2) (m_smoothcost[(lab1)+(lab2)*m_num_labels] )
\r
119 #define SET_INDIVIDUALLY 0
\r
120 #define SET_ALL_AT_ONCE 1
\r
124 #include "GraphCut.h"
\r
128 class GCoptimization
\r
132 ///////////////////////////////////////////////////////////////////////////////////////
\r
133 // First define needed data types //
\r
134 ///////////////////////////////////////////////////////////////////////////////////////
\r
135 /* Type for the total energy calculation. Change it in Graph.h */
\r
136 typedef Graph::flowtype EnergyType;
\r
138 /* Type for the individual terms in the energy function.Change it in Graph.h */
\r
139 typedef Graph::captype EnergyTermType;
\r
141 /* Type of label. Can be set to char, short, int, long */
\r
142 typedef int LabelType;
\r
144 /* Type for pixel. Can be set to short, int, long */
\r
145 typedef int PixelType;
\r
148 /* The following 4 definitions are functions used to set data and smoothness terms */
\r
149 typedef EnergyTermType (*smoothFnPix)(PixelType pix1, PixelType pix2, LabelType lab1, LabelType lab2);
\r
150 typedef EnergyTermType (*smoothFnCoord)(PixelType x, PixelType y, LabelType l1, LabelType l2);
\r
151 typedef EnergyTermType (*dataFnPix)(PixelType pix, LabelType lab);
\r
152 typedef EnergyTermType (*dataFnCoord)(PixelType x, PixelType y, LabelType l);
\r
155 ///////////////////////////////////////////////////////////////////////////////////////
\r
156 // Next define constructors //
\r
157 ///////////////////////////////////////////////////////////////////////////////////////
\r
160 /* Use this constructor only for grid of size width by height. If you use this constructor, */
\r
161 /* 4 connected grid neigbhorhood structure is assumed. num_labels is the number of labels, */
\r
162 /* Labels are assumed to be in {0, 1, 2,....num_labels-1} */
\r
163 /* dataSetup and smoothSetup can take only 2 values, namely SET_INDIVIDUALLY (0) and */
\r
164 /* SET_ALL_AT_ONCE(1) */
\r
165 /* In case dataSetup = SET_INDIVIDUALLY, to set the data cost you must use the */
\r
166 /* member function setDataCost(pixel,label,cost). If dataSetup = SET_ALL_AT_ONCE, */
\r
167 /* to set the data costs you must use any of the setData() functions. */
\r
168 /* In case smoothSetup = SET_INDIVIDUALLY, to set the smooth cost you must use the */
\r
169 /* member function setSmoothCost(label1,label2,cost). If smoothSetup = SET_ALL_AT_ONCE, */
\r
170 /* to set the smoothness costs you must use any of the setSmoothness() functions. */
\r
171 GCoptimization(PixelType width,PixelType height,int num_labels,int dataSetup, int smoothSetup);
\r
174 /* This is the constructor for non-grid graphs. Everything is the same as in the constructor */
\r
175 /* above, but neighborhood structure must be specified by any of the two setNeighbors() */
\r
177 GCoptimization(PixelType num_pixels,int num_labels,int dataSetup, int smoothSetup);
\r
179 /* This constructor is the same as the first constructor, but array m_answer for */
\r
180 /* storage of the labels is passed. This will save space, as the answer will not have to be */
\r
181 /* stored twice, once in the current class, and once in the class which calls this optimization */
\r
183 GCoptimization(LabelType *m_answer,PixelType num_pixels,int nLabels,int dataSetup, int smoothSetup);
\r
185 /* This constructor is the same as the second constructor, but array m_answer for */
\r
186 /* storage of the labels is passed. This will save space, as the answer will not have to be */
\r
187 /* stored twice, once in the current class, and once in the class which calls this optimization */
\r
189 GCoptimization(LabelType *m_answer,PixelType width,PixelType height,int num_labels,int dataSetup, int smoothSetup);
\r
191 /* Peforms expansion algorithm. Runs the number of iterations specified by max_num_iterations */
\r
192 /* Returns the total energy of labeling */
\r
193 EnergyType expansion(int max_num_iterations);
\r
195 /* Peforms expansion algorithm. Runs it until convergence */
\r
196 /* Returns the total energy of labeling */
\r
197 EnergyType expansion();
\r
199 /* Peforms one iteration (one pass over all labels) of expansion algorithm.*/
\r
200 EnergyType oneExpansionIteration();
\r
202 /* Peforms expansion on one label, specified by the input parameter alpha_label */
\r
203 EnergyType alpha_expansion(LabelType alpha_label);
\r
205 /* Peforms expansion on label alpha_label only for pixels specified by *pixels. */
\r
206 /* num is the size of array pixels */
\r
207 EnergyType alpha_expansion(LabelType alpha_label, PixelType *pixels, int num);
\r
209 /* Peforms swap algorithm. Runs it the specified number of iterations */
\r
210 EnergyType swap(int max_num_iterations);
\r
212 /* Peforms swap algorithm. Runs it until convergence */
\r
215 /* Peforms one iteration (one pass over all labels) of swap algorithm.*/
\r
216 EnergyType oneSwapIteration();
\r
218 /* Peforms swap on a pair of labels, specified by the input parameters alpha_label, beta_label */
\r
219 EnergyType alpha_beta_swap(LabelType alpha_label, LabelType beta_label);
\r
223 /* Returns current label assigned to input pixel */
\r
224 inline LabelType whatLabel(PixelType pixel){assert(pixel >= 0 && pixel < m_num_pixels);return(m_labeling[pixel]);};
\r
226 /* Sets data cost of pixel to label */
\r
227 inline void setDataCost(PixelType pixel, LabelType label, EnergyTermType cost){
\r
228 assert(m_dataInput == SET_INDIVIDUALLY);
\r
229 assert(pixel >= 0 && pixel < m_num_pixels && label >= 0 && label < m_num_labels );
\r
230 m_datacost(pixel,label) = cost;};
\r
232 /* Sets smooth cost for label1, label2 to cost. */
\r
233 inline void setSmoothCost(LabelType label1, LabelType label2, EnergyTermType cost){
\r
234 assert(m_smoothInput == SET_INDIVIDUALLY);
\r
235 assert(label1 >= 0 && label1 < m_num_labels && label2 >= 0 && label2 < m_num_labels );
\r
236 m_smoothcost(label1,label2) = cost;};
\r
238 /* Makes pixel1 and pixel2 neighbors of each other. Can be called only 1 time for each */
\r
239 /* unordered pair of pixels. Parameter weight can be used to set spacially varying terms */
\r
240 /* If the desired penalty for neighboring pixels pixel1 and pixel2 is */
\r
241 /* V(label1,label2) = weight*SmoothnessPenalty(label1,label2), then */
\r
242 /* member function setLabel should be called as: setLabel(pixel1,pixel2,weight) */
\r
243 void setNeighbors(PixelType pixel1, PixelType pixel2, EnergyTermType weight);
\r
245 /* If the desired penalty for neighboring pixels pixel1 and pixel2 is */
\r
246 /* V(label1,label2) = SmoothnessPenalty(label1,label2), then */
\r
247 /* member function setLabel should be called as: setLabel(pixel1,pixel2) */
\r
248 /* Also use this function to set up the neighborhood structure when energy terms are specified */
\r
249 /* by a function pointer */
\r
250 /* Again, this function can only be called one time for each distinct pair of pixels */
\r
251 void setNeighbors(PixelType pixel1, PixelType pixel2);
\r
253 /* This function can be used to change the label of any pixel at any time */
\r
254 inline void setLabel(PixelType pixel, LabelType label){
\r
255 assert(label >= 0 && label < m_num_labels && pixel >= 0 && pixel < m_num_pixels);m_labeling[pixel] = label;};
\r
258 /* Set all labels at once according to user's input */
\r
259 /* labels is an array of size num_pixels (or width*height in grid graph) */
\r
260 void SetAllLabels(const LabelType* labels);
\r
262 /* copy the internal labels array into a given external array */
\r
263 void ExportLabels(LabelType* labels);
\r
265 /* get number of pixels in graph */
\r
266 inline PixelType GetNumPixels() { return m_num_pixels; };
\r
268 /* get width, if not a grid returns one thus height = num_of_pixels / width */
\r
269 inline PixelType GetWidth() {
\r
274 /* get number of possible labels */
\r
275 inline LabelType GetNumLabels() { return m_num_labels; };
\r
278 /* Returns Data Energy of current labeling */
\r
279 EnergyType giveDataEnergy();
\r
281 /* Returns Smooth Energy of current labeling */
\r
282 EnergyType giveSmoothEnergy();
\r
284 /* By default, the labels are visited in random order for both the swap and alpha-expansion moves */
\r
285 /* Use this function with boolean argument 0 to fix the order to be not random */
\r
286 /* Use this function with argumnet 1 to fix the order back to random */
\r
287 void setLabelOrder(bool RANDOM_LABEL_ORDER);
\r
290 /* This function is used to set the data term, and it can be used only if dataSetup = SET_ALL_AT_ONCE */
\r
291 /* DataCost is an array s.t. the data cost for pixel p and label l is stored at */
\r
292 /* DataCost[pixel*num_labels+l]. If the current neighborhood system is a grid, then */
\r
293 /* the data term for label l and pixel with coordinates (x,y) is stored at */
\r
294 /* DataCost[(x+y*width)*num_labels + l]. Thus the size of array DataCost is num_pixels*num_labels */
\r
295 void setData(EnergyTermType *DataCost);
\r
297 /* This function is used to set the data term, and it can be used only if dataSetup = SET_ALL_AT_ONCE */
\r
298 /* dataFn is a pointer to a function f(Pixel p, Label l), s.t. the data cost for pixel p to have */
\r
299 /* label l is given by f(p,l) */
\r
300 void setData(dataFnPix dataFn);
\r
303 /* This function is used to set the data term, and it can be used only if dataSetup = SET_ALL_AT_ONCE */
\r
304 /* and only for the grid graph */
\r
305 /* dataFn is a pointer to a function f(x, y, l), s.t. the data cost for pixel with coordinates (x,y) */
\r
306 /* to have label l is given by f(x,y,l) */
\r
307 void setData(dataFnCoord dataFn);
\r
310 /* This function is used to set the smoothness term, and it can be used only if */
\r
311 /* smoothSetup = SET_ALL_AT_ONCE */
\r
312 /* V is an array of costs, such that V(label1,label2) is stored at V[label1+num_labels*label2] */
\r
313 /* If graph is a grid, then using this function only if the smooth costs are not spacially varying */
\r
314 /* that is the smoothness penalty V depends only on labels, but not on pixels */
\r
315 void setSmoothness(EnergyTermType* V);
\r
317 /* This function is used to set the smoothness term, and it can be used only if */
\r
318 /* smoothSetup = SET_ALL_AT_ONCE AND the graph is a grid */
\r
319 /* V is an array of costs, such that V(label1,label2) is stored at V[label1+num_labels*label2] */
\r
320 /* hCue() is used to store the spacially varying coefficient w_pq. */
\r
321 /* if p has coordinates (x,y) and q has coordinates (x+1,y) then w_pq = hCue[x+y*width] */
\r
322 /* The smoothness penalty, therefore, is */
\r
323 /* V_pq(label1,label2) = V[label1+num_labels*label2]*hCue[x+y*width] */
\r
324 /* vCue() is used to store the spacially varying coefficient w_pq. */
\r
325 /* if p has coordinates (x,y) and q has coordinates (x,y+1) then w_pq = vCue[x+y*width] */
\r
326 /* The smoothness penalty, therefore, is */
\r
327 /* V_pq(label1,label2) = V[label1+num_labels*label2]*vCue[x+y*width] */
\r
328 void setSmoothness(EnergyTermType* V,EnergyTermType* hCue, EnergyTermType* vCue);
\r
330 /* This function is used to set the smoothness term, and it can be used only if */
\r
331 /* smoothSetup = SET_ALL_AT_ONCE */
\r
332 /* cost is a function f(pix1,pix2,label1,label2) such that smoothness penalty for neigboring pixels */
\r
333 /* pix1 and pix2 to have labels, respectively, label1 and label2 is f(pix1,pix2,label1,label2) */
\r
334 void setSmoothness(smoothFnCoord cost);
\r
336 /* This function is used to set the smoothness term, and it can be used only if */
\r
337 /* smoothSetup = SET_ALL_AT_ONCE AND the graph is a grid */
\r
338 /* horz_cost is a function f(x,y,label1,label2) such that smoothness penalty for neigboring pixels */
\r
339 /* (x,y) and (x+1,y) to have labels, respectively, label1 and label2 is f(x,y,label1,label2) */
\r
340 /* vert_cost is a function f(x,y,label1,label2) such that smoothness penalty for neigboring pixels */
\r
341 /* (x,y) and (x,y+1) to have labels, respectively, label1 and label2 is f(x,y,label1,label2) */
\r
342 void setSmoothness(smoothFnCoord horz_cost, smoothFnCoord vert_cost);
\r
345 /* validate class function */
\r
346 bool IsClassValid() { return class_sig == VALID_CLASS_SIGNITURE; };
\r
347 bool GetTruncateState() { return truncate_flag; };
\r
348 void SetTruncateState(bool tf) { truncate_flag = tf; };
\r
352 typedef struct NeighborStruct{
\r
354 EnergyTermType weight;
\r
363 } representationType;
\r
365 int class_sig; /* bagon: signiture value to verify class is ok */
\r
366 bool truncate_flag; /* bagon: flag for allowing truncation in expansion */
\r
371 PixelType m_num_pixels;
\r
372 LabelType *m_labeling;
\r
374 representationType m_dataType;
\r
375 representationType m_smoothType;
\r
376 bool m_random_label_order;
\r
378 bool m_varying_weights; // this boolean flag is used only for grid graphs
\r
381 bool m_deleteLabeling;
\r
383 EnergyTermType *m_datacost;
\r
384 EnergyTermType *m_smoothcost;
\r
385 EnergyTermType *m_vertWeights;
\r
386 EnergyTermType *m_horizWeights;
\r
387 LinkedBlockList *m_neighbors;
\r
389 LabelType *m_labelTable;
\r
390 PixelType *m_lookupPixVar;
\r
392 EnergyTermType m_weight;
\r
394 /* Pointers to function for energy terms */
\r
395 dataFnPix m_dataFnPix;
\r
396 dataFnCoord m_dataFnCoord;
\r
398 smoothFnCoord m_horz_cost,m_vert_cost;
\r
399 smoothFnPix m_smoothFnPix;
\r
402 EnergyType start_expansion(int max_iterations);
\r
403 EnergyType start_swap(int max_iterations);
\r
404 EnergyType compute_energy();
\r
406 void commonGridInitialization( PixelType width, PixelType height, int nLabels);
\r
407 void commonNonGridInitialization(PixelType num_pixels, int num_labels);
\r
408 void commonInitialization(int dataSetup, int smoothSetup);
\r
410 void scramble_label_table();
\r
411 void perform_alpha_expansion(LabelType label);
\r
412 void perform_alpha_beta_swap(LabelType alpha_label, LabelType beta_label);
\r
414 EnergyType giveDataEnergyArray();
\r
415 EnergyType giveDataEnergyFnPix();
\r
416 EnergyType giveDataEnergyFnCoord();
\r
418 EnergyType giveSmoothEnergy_G_ARRAY_VW();
\r
419 EnergyType giveSmoothEnergy_G_ARRAY();
\r
420 EnergyType giveSmoothEnergy_G_FnPix();
\r
421 EnergyType giveSmoothEnergy_G_FnCoord();
\r
422 EnergyType giveSmoothEnergy_NG_ARRAY();
\r
423 EnergyType giveSmoothEnergy_NG_FnPix();
\r
425 void add_t_links_ARRAY(Energy *e,Energy::Var *variables,int size,LabelType alpha_label);
\r
426 void add_t_links_FnPix(Energy *e,Energy::Var *variables,int size,LabelType alpha_label);
\r
427 void add_t_links_FnCoord(Energy *e,Energy::Var *variables,int size,LabelType alpha_label);
\r
429 void add_t_links_ARRAY_swap(Energy *e,Energy::Var *variables,int size,LabelType alpha_label,LabelType beta_label,PixelType *pixels);
\r
430 void add_t_links_FnPix_swap(Energy *e,Energy::Var *variables,int size,LabelType alpha_label,LabelType beta_label,PixelType *pixels);
\r
431 void add_t_links_FnCoord_swap(Energy *e,Energy::Var *variables,int size,LabelType alpha_label,LabelType beta_label,PixelType *pixels);
\r
433 void set_up_expansion_energy_G_ARRAY_VW(int size, LabelType alpha_label,Energy* e, Energy::Var *variables);
\r
434 void set_up_expansion_energy_G_ARRAY(int size, LabelType alpha_label,Energy* e, Energy::Var *variables);
\r
435 void set_up_expansion_energy_G_FnPix(int size, LabelType alpha_label,Energy* e, Energy::Var *variables);
\r
436 void set_up_expansion_energy_G_FnCoord(int size, LabelType alpha_label,Energy* e, Energy::Var *variables);
\r
437 void set_up_expansion_energy_NG_ARRAY(int size, LabelType alpha_label,Energy* e, Energy::Var *variables);
\r
438 void set_up_expansion_energy_NG_FnPix(int size, LabelType alpha_label,Energy* e, Energy::Var *variables);
\r
439 void set_up_expansion_energy_G_ARRAY_VW_pix(int size, LabelType alpha_label,Energy *e,
\r
440 Energy::Var *variables, PixelType *pixels, int num );
\r
441 void set_up_expansion_energy_G_ARRAY_pix(int size, LabelType alpha_label,Energy *e,
\r
442 Energy::Var *variables, PixelType *pixels, int num );
\r
445 void set_up_swap_energy_G_ARRAY_VW(int size, LabelType alpha_label,LabelType beta_label,PixelType *pixels,Energy* e, Energy::Var *variables);
\r
446 void set_up_swap_energy_G_ARRAY(int size, LabelType alpha_label,LabelType beta_label,PixelType *pixels,Energy* e, Energy::Var *variables);
\r
447 void set_up_swap_energy_G_FnPix(int size, LabelType alpha_label,LabelType beta_label,PixelType *pixels,Energy* e, Energy::Var *variables);
\r
448 void set_up_swap_energy_G_FnCoord(int size, LabelType alpha_label,LabelType beta_label,PixelType *pixels,Energy* e, Energy::Var *variables);
\r
449 void set_up_swap_energy_NG_ARRAY(int size, LabelType alpha_label,LabelType beta_label,PixelType *pixels,Energy* e, Energy::Var *variables);
\r
450 void set_up_swap_energy_NG_FnPix(int size, LabelType alpha_label,LabelType beta_label,PixelType *pixels,Energy* e, Energy::Var *variables);
\r
453 void initialize_memory();
\r
454 void terminateOnError(bool error_condition,const char *message);
\r