You cannot do it in general case, unless your code is aware of human anatomy.
Imagine an intellectual person from an extraterrestrial civilization who sees a human for the very first time, in pure lighting condition, and that human holds two hands close together (if you have a good imagination, I guess it's easy to imagine, isn't it?
). I'm afraid that creature also would have trouble to recognize two separate limbs.
The problem is inherently not well defined, and this is not your fault. It is inherently difficult, resembles one of those ill-posed problems
. Sorry for not giving you a solution. Just think about it.