Median of sequence of cv::Mat's


I have this cute routine which gives me median of sequence of images:

struct imageStackEntry {
cv::Mat colorImage;

void saveStackedMedian(std::vector& imageStack) {
Mat tmp;

for (int i = 0; i < imageStack.size(); i++) {
	for (int j = i + 1; j < imageStack.size(); j++) {
		min(imageStack[i].colorImage, imageStack[j].colorImage, imageStack[i].colorImage);
		max(imageStack[j].colorImage, tmp, imageStack[j].colorImage);
Mat result = imageStack[imageStack.size() / 2].colorImage;
imwrite("median.png", result);


Is it possible to make this routine faster using CUDA?

I cannot find CUDA equivalent version of min() and max() which works on Mat’s. Do they exists?

Thanks for any help!