cv::accumulate function might help you. Also check accumulateSquare, accumulateProduct and accumulateWeighted. With accumulated image you can easily calculate statistics as Michael Burdinov says.