1. A method of generating an image enhancement function for enhancing an input image comprising a plurality of pixels to form an enhanced output image comprising a plurality of pixels, the method comprising: receiving a reference image comprising a plurality of pixels; receiving an enhanced image comprising a corresponding plurality of pixels, wherein the enhanced image is derived from the reference image; calculating a plurality of lookup tables, wherein each lookup table maps a first plurality of pixel values to a second plurality of pixel values; and generating the image enhancement function based on the lookup tables and which, when applied to the input image, forms the output image, wherein the relationship between brightness of a pixel of the output image and brightness of a pixel of the input image depends at least in part on lookup tables of pixels that have a brightness quantization level in common with the pixel of the input image, wherein the image enhancement function, when applied to the reference image, generates an approximation of the enhanced image, wherein the lookup tables of the brightness quantization level are weighted at least in part based on spatial location and at least in part based on a likelihood that the brightness quantization level is represented at a given pixel location.