I have a grayscale image I and a logical mask of its foreground FG. I would like to do the operation roughly described as,
In other words, similar to bwdist, I need to minimize the distance of each background pixel P to the foreground. However, I do not want the value of the minimum distance to be assigned to P. Instead, I want it assigned the value of the closest foreground pixel that it found. If multiple foreground pixels are equidistant from P, then the average value among them can be used.
Are there any existing tools that will do this operation efficiently?