I will take the same answer as Janos said. Use the entropy. If the entropy is zero then you have one dominant gray value and the variance is equal to zeros. An for an 8-bit image if the entropy is equal to 8 then this is a perfect heterogeneous image since each gray value have equal probability of occurrence.
It depends on the application. That is, how you decide if an image is complex or not. If you classify a noise image as a complex one and a homogeneous one as a not-complex one, you may use metrics that we use in steganalysis.
For the steganalysis applications these methods have been proposed.
1- QINGZHONG LIU et al., Image complexity and feature mining for steganalysis of least significant bit matching steganography, 2008
2- QINGZHONG LIU et al., Neighboring Joint Density-Based JPEG Steganalysis, 2011
You can use HOG/PHOG based measure to evaluate image complexity. See the paper linked below where authors defined appropriate complexity measure (Mco). This approach allows easily detect a uniform backgrounds.
Conference Paper PHOG-Derived Aesthetic Measures Applied to Color Photographs...
For more complicated backgrounds like repeated patterns, textures you can use a similarity value based on mutual information e.g. compute it between selected parts of the image.
These answers all assume that noise images, e.g. with each pixel selected uniformly at random, are highly complex. If you don't consider noise images to be random, try this method based on minimum description length: