本系列代码托管于:https://github.com/chintsan-code/opencv4-tutorials

本篇使用的项目为:threshold

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, const char** argv) {
	Mat src = imread("../sample/master.jpg");
	if (src.empty()) {
		cout << "could not load image..." << endl;
		return -1;
	}
	namedWindow("input", WINDOW_AUTOSIZE);
	imshow("input", src);
	Mat gray, binary;
	cvtColor(src, gray, COLOR_BGR2GRAY);
	imshow("gray", gray);
	Scalar m = mean(gray);
	printf("means : %.2f\n", m[0]);
	threshold(gray, binary, m[0], 255, THRESH_BINARY);
	imshow("binary", binary);

	double t1 = threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_OTSU);
	imshow("otsu binary", binary);

	double t2 = threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_TRIANGLE);
	imshow("triangle binary", binary);

	printf("otsu theshold : %.2f, triangle threshold : %.2f \n", t1, t2);

	waitKey(0);
	destroyAllWindows();
	return 0;
}

图像二值化分割,最重要的就是计算出阈值threshold

均值法

opencv4入门笔记(26):全局阈值分割-萤火

全局阈值

  • OTSU:最大类间方差法。利用直方图
opencv4入门笔记(26):全局阈值分割-萤火
  • Triangle:三角法。对单峰直方图图像效果会好一些。医学、生物X光图像
opencv4入门笔记(26):全局阈值分割-萤火