The performance of image editing tasks, such as segmentation and colorization, from partial user input, is done by examining minimal distances between image pixels. Known solutions work for discrete approximations of the original shortest path problem. Erroneous editing results due to approximate minimal distances produced by known solutions. The proposed invention uses a tree of image level sets for calculation of minimal distances between image pixels without introducing approximation error. This computes a solution of the original minimal distance problem, while maintaining the same linear algorithm complexity. This is useful for image and video segmentation, matting objects into a different background, and image colorization, to be used in commercial image editing programs or a part of mobile image editing applications.