Connected components of sets of finite perimeter and applications to image processing