Function for segmentation and clipping