since we are chaining this code anyway, can you replace CV_IMIN with std::min?