1. A computer-implemented method of generating a mask layer of an image, comprising: performing the following steps by a processor: forming gradient components; using grayscale values of pixels adjacent to maximal gradient points within the gradient components to obtain respective threshold values for the gradient components; for each of the gradient components, using a respective one of said threshold values to threshold all pixels within a bounding box of the respective gradient component to form a respective thresholded component; and adding one or more of the thresholded components or an inverse of the one or more of the thresholded components to the mask layer.