自适应阈值化方法

1.均值方法

为每一点单独计算阈值,块内均值作为阈值

2.高斯方法

为每一点单独计算阈值,块内加权均值作为阈值

3.otsu

otsu法(最大类间方差法,有时也称之为大津算法)使用的是聚类的思想,把图像的灰度数按灰度级分成2个部分,使得两个部分之间的灰度值差异最大,每个部分之间的灰度差异最小,通过方差的计算来寻找一个合适的灰度级别 来划分。  所以 可以在二值化的时候 采用otsu算法来自动选取阈值进行二值化。otsu算法被认为是图像分割中阈值选取的最佳算法,计算简单,不受图像亮度和对比度的影响。因此,使类间方差最大的分割意味着错分概率最小。


设t为设定的阈值。

wo: 分开后  前景像素点数占图像的比例

uo:  分开后  前景像素点的平均灰度

w1:分开后  被景像素点数占图像的比例

u1:  分开后  被景像素点的平均灰度

u=w0*u0 + w1*u1 :图像总平均灰度


从L个灰度级遍历t,使得t为某个值的时候,前景和背景的方差最大, 则 这个 t  值便是我们要求得的阈值。

其中,方差的计算公式如下:

g=wo * (uo - u) * (uo - u) + w1 * (u1 - u) * (u1 - u)

[             此公式计算量较大,可以采用:      g = wo * w1 * (uo - u1) * (uo - u1)                ]



 

==================

  1. #include "stdafx.h"   
  2. #include "stdio.h"   
  3. #include "cv.h"   
  4. #include "highgui.h"   
  5. #include "Math.h"   
  6.   
  7. int Otsu(IplImage* src);  
  8.   
  9. int _tmain(int argc, _TCHAR* argv[])  
  10. {  
  11.     IplImage* img = cvLoadImage("c:\\aSa.jpg",0);  
  12.     IplImage* dst = cvCreateImage(cvGetSize(img), 8, 1);  
  13.     int threshold = Otsu(img);  
  14.     printf("threshold = %d\n", threshold);  
  15.     cvThreshold(img, dst, threshold, 255, CV_THRESH_BINARY);  
  16.   
  17.     cvNamedWindow( "img", 1 );  
  18.     cvShowImage("img", dst);  
  19.   
  20.   
  21.     cvWaitKey(-1);  
  22.   
  23.     cvReleaseImage(&img);  
  24.     cvReleaseImage(&dst);  
  25.       
  26.     cvDestroyWindow( "dst" );  
  27.     return 0;  
  28. }  
  29.   
  30. int Otsu(IplImage* src)    
  31. {    
  32.     int height=src->height;    
  33.     int width=src->width;        
  34.   
  35.     //histogram     
  36.     float histogram[256] = {0};    
  37.     for(int i=0; i < height; i++)  
  38.     {    
  39.         unsigned char* p=(unsigned char*)src->imageData + src->widthStep * i;    
  40.         for(int j = 0; j < width; j++)   
  41.         {    
  42.             histogram[*p++]++;    
  43.         }    
  44.     }    
  45.     //normalize histogram     
  46.     int size = height * width;    
  47.     for(int i = 0; i < 256; i++)  
  48.     {    
  49.         histogram[i] = histogram[i] / size;    
  50.     }    
  51.   
  52.     //average pixel value     
  53.     float avgValue=0;    
  54.     for(int i=0; i < 256; i++)  
  55.     {    
  56.         avgValue += i * histogram[i];  //整幅图像的平均灰度  
  57.     }     
  58.   
  59.     int threshold;      
  60.     float maxVariance=0;    
  61.     float w = 0, u = 0;    
  62.     for(int i = 0; i < 256; i++)   
  63.     {    
  64.         w += histogram[i];  //假设当前灰度i为阈值, 0~i 灰度的像素(假设像素值在此范围的像素叫做前景像素) 所占整幅图像的比例  
  65.         u += i * histogram[i];  // 灰度i 之前的像素(0~i)的平均灰度值: 前景像素的平均灰度值  
  66.   
  67.         float t = avgValue * w - u;    
  68.         float variance = t * t / (w * (1 - w) );    
  69.         if(variance > maxVariance)   
  70.         {    
  71.             maxVariance = variance;    
  72.             threshold = i;    
  73.         }    
  74.     }    
  75.   
  76.     return threshold;    
  77. }   
#include "stdafx.h"
#include "stdio.h"
#include "cv.h"
#include "highgui.h"
#include "Math.h"

int Otsu(IplImage* src);

int _tmain(int argc, _TCHAR* argv[])
{
	IplImage* img = cvLoadImage("c:\\aSa.jpg",0);
	IplImage* dst = cvCreateImage(cvGetSize(img), 8, 1);
	int threshold = Otsu(img);
	printf("threshold = %d\n", threshold);
	cvThreshold(img, dst, threshold, 255, CV_THRESH_BINARY);

	cvNamedWindow( "img", 1 );
	cvShowImage("img", dst);


	cvWaitKey(-1);

	cvReleaseImage(&img);
	cvReleaseImage(&dst);
	
	cvDestroyWindow( "dst" );
	return 0;
}

int Otsu(IplImage* src)  
{  
	int height=src->height;  
	int width=src->width;      

	//histogram  
	float histogram[256] = {0};  
	for(int i=0; i < height; i++)
	{  
		unsigned char* p=(unsigned char*)src->imageData + src->widthStep * i;  
		for(int j = 0; j < width; j++) 
		{  
			histogram[*p++]++;  
		}  
	}  
	//normalize histogram  
	int size = height * width;  
	for(int i = 0; i < 256; i++)
	{  
		histogram[i] = histogram[i] / size;  
	}  

	//average pixel value  
	float avgValue=0;  
	for(int i=0; i < 256; i++)
	{  
		avgValue += i * histogram[i];  //整幅图像的平均灰度
	}   

	int threshold;    
	float maxVariance=0;  
	float w = 0, u = 0;  
	for(int i = 0; i < 256; i++) 
	{  
		w += histogram[i];  //假设当前灰度i为阈值, 0~i 灰度的像素(假设像素值在此范围的像素叫做前景像素) 所占整幅图像的比例
		u += i * histogram[i];  // 灰度i 之前的像素(0~i)的平均灰度值: 前景像素的平均灰度值

		float t = avgValue * w - u;  
		float variance = t * t / (w * (1 - w) );  
		if(variance > maxVariance) 
		{  
			maxVariance = variance;  
			threshold = i;  
		}  
	}  

	return threshold;  
} 



 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值