Calculate the average colour of an area in an image

imagemagick

Solution

I would crop to the area you are interested in then resize it to 1 pixel. Then get the value of that pixel.

Problem

The goal is to lay a caption on top of an image and set the text colour to one that will contrast with the background it is positioned on top of. To that end, I wish to calculate the average colour of the area inside the red rectangles in the following images:

Original source

Related problems