A method which seems to work was suggested here: Is it the right way to create mask for phase unwrapping?