http://blog.csdn.net/xw20084898/article/details/17564957 主要取自这里,修改为Mat类。
单阈值图像分割算法,有手动阀值,自适应阈值,最大熵法,基本全局阈值法,OTSU阈值。
#include<opencv2/opencv.hpp>
#include<opencv2/imgproc.hpp>
#include<opencv2/highgui.hpp>
#include<vector>
using namespace cv;
using namespace std;
void maxEntroy(Mat src,int&Threshold);
int BasicGlobalThreshold(int *pg, int x, int y);
int ThresholdOtsu(Mat src);
int main() {
Mat src,gray,dst1,dst2,dst3,dst4, dst5,dst6;
src = imread("trees.png");
cvtColor(src, gray, CV_BGR2GRAY);
imshow("原图", src);
imshow("灰度图", gray);
///手动设置阈值
threshold(gray, dst1,100, 255, CV_THRESH_BINARY);
namedWindow("手动阈值",CV_WINDOW_AUTOSIZE);
imshow("手动阈值", dst1);
///自适应阈值计算图像领域平均灰度
double max_value = 255;
int adpative_m