use cv::reduce() instead of this custom code