Split code in segment_image.hpp into functions