I check your code, and find the Mat alphamap has not set with initial value since its malloc. It is then directly used in InitializeAlpha. So I think this may be a big negligence.
In the right process, after malloc, the background pixel's alpha should be 0, and foreground pixel 1.