Mat image;//The original image
Mat tmp = image.reshape(1, 1);//make matrix new number of channels and new number of rows. here Put data: 1 row, all cols
Mat sorted; //after sorted data
cv::sort(tmp, sorted, CV_SORT_ASCENDING);
int meddate = sorted.at<uchar>(sorted.cols / 2);//find median data in median of cols