Analyzing image contrast?

I’m looking to find a way to analyze the contrast of an image. Based on that I then want to increase the contrast if it’s under a certain threshold. I have the code for increasing the contrast (using ofxContrast) but am lacking a way to analyze how high the contrast of an image is.
Any ideas?

Could you do something like get the average brightness of the brightest 10% of the pixels and the darkest 10% and compare them?