图像二值化——OTSU大津法

最大类间方差法是由日本学者大津(Nobuyuki Otsu)于1979年提出的,是一种自适应的阈值确定的方法,又叫大津法,简称OTSU。它是按图像的灰度特性,将图像分成背景和目标两部分,或者说,是寻找一个阈值为K,将图像的颜色分为1,2.....K和K+1.....256两部分。

如何确定这个阈值K?算法分类的原理是让背景和目标之间的类间方差最大,因为背景和目标之间的类间方差越大,说明构成图像的2部分的差别越大,错分的可能性越小。下面进行公式推导:

首先是符号说明:对于图像I(x,y),前景(即目标)和背景的分割阈值记作T,属于前景的像素点数占整幅图像的比例记为ω0,其平均灰度μ0;背景像素点数占整幅图像的比例为ω1,其平均灰度为μ1。图像的总平均灰度记为μ,类间方差记为g。假设图像的背景较暗,并且图像的大小为M×N,图像中像素的灰度值小于阈值T的像素个数记作N0,像素灰度大于阈值T的像素个数记作N1。

\omega _{1}=\frac{N_{1}}{M\times N}

\omega _{2}=\frac{N_{2}}{M\times N}

N_{1}+N_{2}=M\times N

\omega _{1}+\omega _{2}=1

图像的总平均灰度为:\mu =\mu _{1}\times \omega _{1}+\mu _{2}\times \omega _{2}   (1)

前景和背景图象的方差:g =\omega _{1}\times \left ( \mu-\mu _{1} \right )^{2}+\omega _{2}\times \left ( \mu-\mu _{2} \right )^{2}  (2)

将(1)代入(2)得:g =\omega _{1}\times \omega _{2}\times \left ( \mu_{1}-\mu _{2} \right )^{2}

采用遍历的方法得到寻找到类间方差最大值,对应的阈值,即为所求。

int Otsu(const IplImage *frame) //大津法求阈值
{
#define GrayScale 256    //frame灰度级
    int width = frame->width;
    int height = frame->height;
    int pixelCount[GrayScale]={0};
    float pixelPro[GrayScale]={0};
    int i, j, pixelSum = width * height, threshold = 0;
    uchar* data = (uchar*)frame->imageData;
 
    //统计每个灰度级中像素的个数
    for(i = 0; i < height; i++)
    {
        for(j = 0;j < width;j++)
        {
            pixelCount[(int)data[i * width + j]]++;
        }
    }
 
    //计算每个灰度级的像素数目占整幅图像的比例
    for(i = 0; i < GrayScale; i++)
    {
        pixelPro[i] = (float)pixelCount[i] / pixelSum;
    }
 
    //遍历灰度级[0,255],寻找合适的threshold
    float w0, w1, u0tmp, u1tmp, u0, u1, deltaTmp, deltaMax = 0;
    for(i = 0; i < GrayScale; i++)
    {
        w0 = w1 = u0tmp = u1tmp = u0 = u1 = deltaTmp = 0;
        for(j = 0; j < GrayScale; j++)
        {
            if(j <= i)   //背景部分
            {
                w0 += pixelPro[j];
                u0tmp += j * pixelPro[j];
            }
            else   //前景部分
            {
                w1 += pixelPro[j];
                u1tmp += j * pixelPro[j];
            }
        }
        u0 = u0tmp / w0;
        u1 = u1tmp / w1;
        deltaTmp = (float)(w0 *w1* pow((u0 - u1), 2)) ;
        if(deltaTmp > deltaMax)
        {
            deltaMax = deltaTmp;
            threshold = i;
        }
    }
    return threshold;
}

opencv提供的阈值化方法有threshold和adaptiveThreshold,以及OTSU方法。 

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
 
int main(int argc, char* argv[])
{
	Mat img = imread(argv[1], -1);
	if (img.empty())
	{
		cout <<"Error: Could not load image" <<endl;
		return 0;
	}
 
	Mat gray;
	cvtColor(img, gray, CV_BGR2GRAY);
 
	Mat dst;
	threshold(gray, dst, 0, 255, CV_THRESH_OTSU);
 
	imshow("src", img);
	imshow("gray", gray);
	imshow("dst", dst);
	waitKey(0);
 
	return 0;
}

参考:

【1】https://baike.baidu.com/item/otsu/16252828?fr=aladdin

【2】Otsu N. A threshold selection method from gray-level histogram. IEEE Trans,1979;SMC-9;62-66

【3】https://blog.csdn.net/momo026/article/details/84026075

  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
大津法二值化是一种常用的图像处理算法,它能有效地将图像转换为二值图像。在MATLAB中,可以通过使用Otsu算法来实现大津法二值化。这个算法基于图像的灰度直方图,通过计算某个灰度值作为阈值时的类间方差最大化来确定最佳阈值。引用提供了一个MATLAB代码示例,可以通过输入图像路径来实现大津法二值化。在该代码中,使用了hist函数来获取每个像素值的数量,并根据这些数量计算出最佳阈值。实验结果表明,在处理特定类型的图像时,手动计算的阈值与MATLAB自带算法计算的阈值几乎一致。因此,可以使用MATLAB中的大津法二值化方法来有效地对图像进行二值化处理。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [MATLAB小技巧(3)otsu二值化分割算法](https://blog.csdn.net/sinat_34897952/article/details/124287380)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [matlab大津法二值化代码-imagebinarization:有效地对图像进行二值化](https://download.csdn.net/download/weixin_38708841/19076881)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [[matlab数字图像处理10]对一副图像进行二值化,ostu算法等](https://blog.csdn.net/qq_46535765/article/details/126062684)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值