Our library is available in C++ and Matlab. It has been tested on Windows.

Prerequisites:
-Make sure your CPU supports SSE4.
-Download MinGW-w64 at http://mingw-w64.sourceforge.net/download.php#mingw-builds. Then install mingw-builds. Make sure that the path of MinGW-w64 is added to the path in the environment variable.

Energy function to minimize:
E(x)=\sum_{i,j}{d[i][j][x[i][j]] //data terms
+wr[i][j]*s[x[i][j]][x[i][j+1]]  //smoothness terms on rows
+wc[i][j]*s[x[i][j]][x[i+1][j]]} //smoothness terms on columns 
where s[k1][k2]=min(rho(k1-k2),T). We assume that "rho" is convex in our implementation. 


C++:

Run "compile.bat" to compile "example.cpp". Then run "example.exe" for a demonstration.

To use the library, you need to declare "mrfGrid test(m,n,l,intersection)". Remember that arrays are 0-based in C++ and make sure that all terms have type "int".
-"m" is the number of rows.
-"n" is the number of columns.
-"l" is the number of labels.
-"intersection" is the intersection function. It has the form "int intersection(k1,k2,c,weight)" and it returns the greatest integer "i" such that rho(i-k1)*weight<=rho(i-k2)+c. If "i" is negative or non-existant, you can return any negative integer; if "i" is at least "l" or infinite, you can return any positive integer greater or equal to "l". The L1 penalty rho(k)=abs(k) is the default smoothness term, you can simply set "intersection" to "NULL". L2 and Charbonnier penalties are also implemented in "example.cpp". Implement your intersection function if you want to use other functions. You can implement binary search or write an analytical solution for "intersection".

In addition, you need to fill in four arrays in test.
-test.d[i][j][k]: the data term for node (i,j) with label k.
-test.s[i][j]: the smoothness term between label i and j.
-test.wr[i][j]: the weight between (i,j) and (i,j+1).
-test.wc[i][j]: the weight between (i,j) and (i+1,j).

To get the solution, run "int **sol=test.BCD()". "sol[i][j]" gives the label at (i,j). "test.energy(sol)" gives you the corresponding energy.


Matlab:

Modify the path of Matlab in "mex_file.bat" correspondingly. Replace "D:/Matlab_2012b" with your Matlab path.

Run "example.m" for demonstration.

To get the solution, run "[res energy]=mrfBCD(data,smooth,wrow,wcol)". Make sure that arrays are 1-based and that ”data", "smooth", "wrow" and "wcol" have the data type "int32"!
-"data(i,j,k)" is the data for node (i,j) with label k.
-"smooth(i,j)" is the smoothness term between label i and j, same as min(rho(i-j),T).
-"wrow(i,j)" is the weight between (i,j) and (i,j+1). 
-"wcol(i,j)" is the weight between (i,j) and (i+1,j).
-"res(i,j)" gives the label at (i,j).
-"energy" gives the energy.

In addition, modify "intersection" correctly in "mrfBCD.cpp". "intersection" represents the intersection function. It has the form "int intersection(k1,k2,c,weight)" and it returns the greatest integer "i" such that rho(i-k1)*weight<=rho(i-k2)+c. If "i" is negative or non-exist, you can return any negative integer; if "i" is at leat "l" or infinite, you can return any positive integer greater or equal to "l". The L1 penalty rho(k)=abs(k) is the default smoothness term, you can simply set "intersection" to "NULL". L2 and Charbonnier penalties are also implemented in "mrfBCD.cpp". Implement your intersection function if you use other functions. You can implement binary search or write an analytical solution for "intersection".

Note that this is a Matlab wrapper for the C++ library. It is slightly slower.

Email: cqf@stanford.edu
