Opencv实现图像中值的获取

#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;
}

 

posted @ 2017-11-20 14:33  心沉大海-汇聚成一  阅读(1663)  评论(0)    收藏  举报