#include<cv.h> #include<opencv2\opencv.hpp> #include<highgui\highgui.hpp> #include<iostream> using namespace std; using namespace cv; void claBitmaps(Mat &img, Mat &tb, Mat &eb){ Mat tmp = img.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 cout << "meddate"<<meddate << endl; threshold(img, tb, meddate, 255, THRESH_BINARY); imshow("tb", tb); waitKey(0); } int main(){ Mat gray_img, tb,eb; Mat ori_img = imread("2.bmp"); cvtColor(ori_img, gray_img, CV_BGR2GRAY); imshow("gray_img", gray_img); claBitmaps(gray_img, tb, eb); /*Firstly,create a MTB image in using median of gray image*/ return 0; }