Computer-implemented method for generating depth values of a surface region of a surface of a workpiece, comprising the following steps: - receiving a focal image stack, wherein the focal image stack has a plurality of images of the workpiece, wherein the images image the surface region of the surface of the workpiece with focal plane positions, which are different in a depth direction, as an image region of the image, wherein a focal plane position is assigned to each image of the focal image stack and wherein image points of the images are respectively assigned to a corresponding object point on the surface of the workpiece; - determining a respective image sharpness value for a plurality of subregions of the image region of each image of the focal image stack; - adapting a model, which can be used to describe a profile of image sharpness values in each case for a common imaged subregion of the surface region over the images of the image stack, in each case to actual profiles of the determined image sharpness values of those subregions of the image regions that form a stack of image subregions because they correspond to a common imaged subregion of the surface region; - determining a quality measure of a quality of the adaptation of the model to the image sharpness values for the stacks of image subregions; - discarding any present stacks of image subregions for which the determined quality measure does not meet a quality criterion; - determining a respective depth value for those stacks of image subregions for which the determined quality measure does meet a quality criterion, wherein the depth value corresponds to the greatest image sharpness for the respective stack of image subregions and is a measure of a depth position of the common imaged subregion of the surface region. - receiving a focal image stack, wherein the focal image stack has a plurality of images of the workpiece, wherein the images image the surface region of the surface of the workpiece with focal plane positions, which are different in a depth direction, as an image region of the image, wherein a focal plane position is assigned to each image of the focal image stack and wherein image points of the images are respectively assigned to a corresponding object point on the surface of the workpiece; - determining a respective image sharpness value for a plurality of subregions of the image region of each image of the focal image stack; - adapting a model, which can be used to describe a profile of image sharpness values in each case for a common imaged subregion of the surface region over the images of the image stack, in each case to actual profiles of the determined image sharpness values of those subregions of the image regions that form a stack of image subregions because they correspond to a common imaged subregion of the surface region; - determining a quality measure of a quality of the adaptation of the model to the image sharpness values for the stacks of image subregions; - discarding any present stacks of image subregions for which the determined quality measure does not meet a quality criterion; - determining a respective depth value for those stacks of image subregions for which the determined quality measure does meet a quality criterion, wherein the depth value corresponds to the greatest image sharpness for the respective stack of image subregions and is a measure of a depth position of the common imaged subregion of the surface region.