The Co-segmentation problem  is where the objective is to segment a similar object from a pair of images. The background in the two images may be arbitrary; therefore, simultaneous segmentation of both images must be performed with a requirement that the appearance of the two sets of foreground pixels in the respective images are consistent. Existing approaches [1, 2] cast this problem as a Markov Random Field (MRF) based segmentation of the image pair with a regularized difference of the two histograms – assuming a Gaussian prior on the foreground appearance  or by calculating the sum of squared differences . Both are interesting formulations but lead to difﬁcult optimization problems, due to the presence of the second (histogram difference) term. The model proposed here bypasses measurement of the histogram differences in a direct fashion; we show that this enables obtaining efﬁcient solutions to the underlying optimization model. Our new algorithm is similar to the existing methods in spirit, but differs substantially in that it can be solved to optimality in polynomial time using a maximum ﬂow procedure on an appropriately constructed graph.
References:  C. Rother, T. Minka, A. Blake, and V. Kolmogorov. Cosegmentation of image pairs by histogram matching – incorporating a global constraint into MRFs. In Proc. of Conf. on Computer Vision and Pattern Recognition, 2006.
 L. Mukherjee, V. Singh, and C. R. Dyer. Half-integrality based algorithms for cosegmentation of images. In Proc. of
Conf. on Computer Vision and Pattern Recognition, 2009.
./segment -conf seg.conf.imagename imagefilename1 imagefilename2
seg.conf.stone provided with this distribution is setup to run on stone1_128_small.png and stone2_128_small.png, the histogram files (histfile1.raw and histfile2.raw) are created accordingly. Run as
./segment -conf seg.conf.stone ../pairimages/stone1_small_128.png ../pairimages/stone2_small_128.png
Before you can run this code on new images you will need the following files:
histsizes.dat (written by the matlab files under histogram_creation_code)
histfile1.raw (histogram of the 1st image, written by the matlab files under histogram_creation_code)
histfile2.raw (histogram of the 2nd image, written by the matlab files under histogram_creation_code)
ses.conf.imagename (this file is the configuration file, contains the path for the histfiles, value of the regularizer lambda, variables called epsilon and numLambdas that are always set to 1, these are internal parameters of the parametric max-flow), the sigma values for smoothness and datacost, the relative influence of the BG (called inflBG) and the number of fg and bg labels and finally these labels (one per line, first specify fg labels in sequence and then bg labels in sequence). These labels are used to set up the data cost for MRF. This configuration file must have seg.conf in the name. See the provided file for an example of what values it should have. To run a simple MRF, (not coseg) set lambda=0, inflBG=1.
The output is written in the directory output, two file names are created, mask_im1_seg.png and mask_im2_seg.png, which are the segmentation masks of the two images.
To see the mask as a regular binary image in matlab do the following.
A sample histogram generation code is provided with this distribution.