otsu算法详解

参考:https://blog.csdn.net/leonardohaig/article/details/120269341

简介

OTSU算法称为最大类间方差法,也称为大津法,由大津再1979年提出,被认为是图像分割中阈值选取的最佳算法,在图像处理中得到广泛应用。它是根据图像的灰度特性,将图像分割成前景区域(object)和背景区域(background),其基本思想是根据分割的阈值计算一个类间方差,将类间方差最大时对应的阈值作为最佳阈值。

类间方差推导

设图像尺寸是MxN,其二值化的的阈值为k,该阈值将图像分成背景和前景两个区域,其中背景区域的像素数量是 N 0 N_0 N0,前景区域的像素数量为 N 1 N_1 N1。背景像素点的占比设为 w 0 w_0 w0,灰度均值为 u 0 u_0 u0,前景像素点的占比设为 w 1 w_1 w1,灰度均值为 u 1 u_1 u1。整幅图像的灰度均值为 u u u,则:

具体推导请看参考

理想情况下,对于同一类,其类内方差是很小的,同时对于背景和前景的类间方差是很大的。因此只要遍历找到使得类间方差最大的阈值T即为所求。

算法步骤与实现

步骤:

  1. 根据图像的尺寸确定图像总的像素点数量MxN
  2. 遍历图像灰度级0-255中的每一个灰度值L,假设L将图像分为背景(Background)和目标(Object)两部分,然后计算类间方差
  3. 遍历过程中使得类间方差最大的阈值T即为所求

代码

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
 
/*
	功能:大津法阈值分割
	输入:gray img
	输出:thresh
*/
int thresholdOTSU(const Mat& img)
{
	CV_Assert(img.type() == CV_8UC1);
	int thresh = -1;
	
	int pixelNum = img.rows * img.cols;

	// 图像直方图统计
	int pixelCount[256] = { 0 };
	Size size(img.size());
	if (img.isContinuous()) {
		size.width *= size.height;
		size.height = 1;
	}
	for (int i = 0; i < size.height; i++) {
		const uchar* sdata = img.ptr(i);
		for (int j = 0; j < size.width; j++) {
			pixelCount[sdata[j]] += 1;
		}
	}

	// 穷举0-255的灰度阈值,找到使得类间方差最大的阈值k
	const float Ppixelnum = 1.0f / (img.rows * img.cols);
	float sigma_max = -FLT_MAX;
	for (int k = 0; k < 256; k++) {
		int backNum = 0, objectNum = 0;
		float backSum = 0, objectSum = 0;
		for (int i = 0; i <= k; i++) {
			backNum += pixelCount[i];
			backSum += i * pixelCount[i];
		}
		for (int i = k + 1; i < 256; i++) {
			objectNum += pixelCount[i];
			objectSum += i * pixelCount[i];
		}

		//求类间方差
		const float w0 = backNum * Ppixelnum;
		const float w1 = objectNum * Ppixelnum;
		const float u0 = backSum / backNum;
		const float u1 = objectSum / objectNum;
		float sigma = w0 * w1 * pow(u0 - u1, 2);
		if (sigma > sigma_max) {
			sigma_max = sigma;
			thresh = k;
		}
	}

	return thresh;
}

void ShowHist(const cv::Mat& image, cv::Mat& hist_img, int scale = 2)
{
	int bins = 256;
	int hist_size[] = { bins };
	float range[] = { 0, 255 };
	const float* ranges[] = { range };
	Mat hist;
	int channels[] = { 0 };

	cv::calcHist(&image, 1, channels, cv::Mat(), // do not use mask
		hist, 1, hist_size, ranges,
		true, // the histogram is uniform
		false);

	double max_val;
	cv::minMaxLoc(hist, nullptr, &max_val, nullptr, nullptr);
	const int hist_height = image.rows;
	hist_img = cv::Mat::zeros(hist_height, bins * scale, CV_8UC3);
	for (int i = 0; i < bins; i++)
	{
		float bin_val = hist.at<float>(i);
		int intensity = cvRound(bin_val * hist_height / max_val);  //要绘制的高度
		cv::rectangle(hist_img, cv::Point(i * scale, hist_height - 1),
			cv::Point((i + 1) * scale - 1, hist_height - intensity),
			CV_RGB(255, 255, 255));
	}
}

int main()
{
	Mat img = imread("test_ostu.bmp",IMREAD_GRAYSCALE);
	imshow("img", img);

	//求解直方图
	Mat hist_img;
	int scale = 2;
	ShowHist(img, hist_img, scale);

	//ostu二值化
	Mat ourdst, cvdst;

	auto thresh = thresholdOTSU(img);
	threshold(img, ourdst, thresh, 255, CV_THRESH_BINARY);

	//在直方图上绘制阈值位置
	line(hist_img, Point(thresh * scale, 0), Point(thresh * scale, img.rows - 1), Scalar(255, 0, 0), 2);

	auto thresh_cv = threshold(img, cvdst, 0, 255, THRESH_OTSU);
	cout << thresh << " " << thresh_cv << endl;
	
	imshow("hist", hist_img);
	imshow("ourdst", ourdst);
	imshow("cvdst", cvdst);

	waitKey(0);
	return 0;
}

结果请看参考

在参考的那篇博客中,除了从基本思想上对类间方差进行了推导之外,还从概率论角度说明了最大化类间方差等价于最小化类内方差,同时给出了另一条计算类间方差的公式。此外,还扩展到了多级大津法,也就是运用同样的思想,但可以将图像分割成多个类(大于两个)。

算法评价

  • 应用:是求图像全局阈值的最佳方法。
  • 优点:计算简单快速,不受图像亮度和对比度的影响。
  • 缺点:对图像噪声敏感;当前景和背景大小比例(面积)悬殊时效果不好。
  • 解释:当图像中的前景与背景的面积相差很大时,直方图没有明显的双峰,或者两个峰的大小相差很大,分割效果不佳,或者前景与背景的灰度有较大的重叠时也不能准确的将前景与背景分开。导致这种现象出现的原因是该方法忽略了图像的空间信息,同时该方法将图像的灰度分布作为分割图像的依据,因而对噪声也相当敏感。所以,在实际应用中,总是将其与其他方法结合起来使用,比如高斯滤波。
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Otsu算法是一种图像分割算法,用于将图像分成背景和前景两部分。它基于图像的灰度直方图,通过寻找一个阈值,将图像中的像素分为两个类别。 以下是Otsu算法的代码详解: ```python import numpy as np import cv2 def otsu_threshold(image): # 将图像转换为灰度图 gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 计算灰度直方图 hist = cv2.calcHist([gray], [0], None, [256], [0, 256]) # 归一化直方图 hist_norm = hist.ravel() / hist.max() # 初始化类间方差和最佳阈值 best_threshold = 0 max_variance = 0 # 遍历所有可能的阈值 for threshold in range(256): # 计算背景和前景的像素数目 background_pixels = np.sum(hist_norm[:threshold]) foreground_pixels = np.sum(hist_norm[threshold:]) # 计算背景和前景的平均灰度值 background_mean = np.sum(np.arange(threshold) * hist_norm[:threshold]) / background_pixels foreground_mean = np.sum(np.arange(threshold, 256) * hist_norm[threshold:]) / foreground_pixels # 计算类间方差 variance = background_pixels * foreground_pixels * (background_mean - foreground_mean) ** 2 # 更新最大类间方差和最佳阈值 if variance > max_variance: max_variance = variance best_threshold = threshold # 应用最佳阈值进行二值化 _, binary = cv2.threshold(gray, best_threshold, 255, cv2.THRESH_BINARY) return binary # 读取图像 image = cv2.imread('image.jpg') # 应用Otsu算法进行图像分割 binary_image = otsu_threshold(image) # 显示结果 cv2.imshow('Original Image', image) cv2.imshow('Binary Image', binary_image) cv2.waitKey(0) cv2.destroyAllWindows() ``` 以上代码使用Python和OpenCV库实现了Otsu算法。首先,将彩色图像转换为灰度图像,然后计算灰度直方图并归一化。接下来,遍历所有可能的阈值,并计算背景和前景的像素数目以及平均灰度值。根据类间方差的计算公式,更新最大类间方差和最佳阈值。最后,应用最佳阈值进行二值化,得到分割后的二值图像。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值