optimizate to find the contour index