This program was written in Matlab. To run the program, simply run the script run.m. A file selector will pop up, simply choose the image you want to separate out the foreground and background from. Then, a second selector will pop up, simply choose the trimap image, which must be the same size as the image you are segmenting. The program will run for a while and produce an alpha matte with the segmentation, and finally a 3rd file selector will pop up. Select an image you want to compose with. The segmented foreground will be composed with the image you select.
I implemented the paper "A Bayesian Approach to Digital Matting" by Chuang et al. The code is commented, and at the top are parameters that can be changed such as the max number of iterations per pixel (10), the FG/BG thresholds (5%), and the amount alpha must change per iteration (.0001). Alpha is initially set to be the average of the neighboring alpha values (unknown neighbors are .5).