OpenCV实现验证otsu算法

转自:http://blog.csdn.net/augusdi/article/details/9012043

 

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) ]

由于otsu算法是对图像的灰度级进行聚类,so 在执行otsu算法之前,需要计算该图像的灰度直方图。


按照上面的解释参考代码如下:

  1. #include <stdio.h> 
  2. #include <cv.h> 
  3. #include <highgui.h> 
  4. #include <math.h> 
  5.  
  6. #pragma comment(lib, "cv.lib") 
  7. #pragma comment(lib, "cxcore.lib") 
  8. #pragma comment(lib, "highgui.lib") 
  9. int Otsu(IplImage* src); 
  10.  
  11. int _tmain(int argc, _TCHAR* argv[]) 
  12.     IplImage* img = cvLoadImage("c:\\aSa.jpg",0); 
  13.     IplImage* dst = cvCreateImage(cvGetSize(img), 8, 1); 
  14.     int threshold = Otsu(img); 
  15.  
  16.     cvThreshold(img, dst, threshold, 255, CV_THRESH_BINARY); 
  17.  
  18.  
  19.     cvNamedWindow( "img", 1 ); 
  20.     cvShowImage("img", dst); 
  21.  
  22.  
  23.     cvWaitKey(-1); 
  24.  
  25.     cvReleaseImage(&img); 
  26.     cvReleaseImage(&dst); 
  27.  
  28.     cvDestroyWindow( "dst" ); 
  29.     return 0; 
  30.  
  31. int Otsu(IplImage* src)   
  32. {   
  33.     int height=src->height;   
  34.     int width=src->width;       
  35.     long size = height * width;  
  36.  
  37.     //histogram   
  38.     float histogram[256] = {0};   
  39.     for(int m=0; m < height; m++) 
  40.     {   
  41.         unsigned char* p=(unsignedchar*)src->imageData + src->widthStep * m;   
  42.         for(int n = 0; n < width; n++)  
  43.         {   
  44.             histogram[int(*p++)]++;   
  45.         }   
  46.     }   
  47.  
  48.     int threshold;     
  49.     long sum0 = 0, sum1 = 0;//存储前景的灰度总和和背景灰度总和 
  50.     long cnt0 = 0, cnt1 = 0; //前景的总个数和背景的总个数 
  51.     double w0 = 0, w1 = 0;//前景和背景所占整幅图像的比例 
  52.     double u0 = 0, u1 = 0;  //前景和背景的平均灰度 
  53.     double variance = 0;//最大类间方差 
  54.     int i, j; 
  55.     double u = 0; 
  56.     double maxVariance = 0; 
  57.     for(i = 1; i < 256; i++)//一次遍历每个像素 
  58.     {   
  59.         sum0 = 0; 
  60.         sum1 = 0;  
  61.         cnt0 = 0; 
  62.         cnt1 = 0; 
  63.         w0 = 0; 
  64.         w1 = 0; 
  65.         for(j = 0; j < i; j++) 
  66.         { 
  67.             cnt0 += histogram[j]; 
  68.             sum0 += j * histogram[j]; 
  69.         } 
  70.  
  71.         u0 = (double)sum0 /  cnt0;  
  72.         w0 = (double)cnt0 / size; 
  73.  
  74.         for(j = i ; j <= 255; j++) 
  75.         { 
  76.             cnt1 += histogram[j]; 
  77.             sum1 += j * histogram[j]; 
  78.         } 
  79.  
  80.         u1 = (double)sum1 / cnt1; 
  81.         w1 = 1 - w0; // (double)cnt1 / size; 
  82.  
  83.         u = u0 * w0 + u1 * w1; //图像的平均灰度 
  84.         printf("u = %f\n", u); 
  85.         //variance =  w0 * pow((u0 - u), 2) + w1 * pow((u1 - u), 2); 
  86.         variance =  w0 * w1 *  (u0 - u1) * (u0 - u1); 
  87.         if(variance > maxVariance)  
  88.         {   
  89.             maxVariance = variance;   
  90.             threshold = i;   
  91.         }  
  92.     }   
  93.  
  94.     printf("threshold = %d\n", threshold); 
  95.     return threshold;   
  96. }   
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
#include <math.h>

#pragma comment(lib, "cv.lib")
#pragma comment(lib, "cxcore.lib")
#pragma comment(lib, "highgui.lib")
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);

	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;      
	long size = height * width; 

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

	int threshold;    
	long sum0 = 0, sum1 = 0; //存储前景的灰度总和和背景灰度总和
	long cnt0 = 0, cnt1 = 0; //前景的总个数和背景的总个数
	double w0 = 0, w1 = 0; //前景和背景所占整幅图像的比例
	double u0 = 0, u1 = 0;  //前景和背景的平均灰度
	double variance = 0; //最大类间方差
	int i, j;
	double u = 0;
	double maxVariance = 0;
	for(i = 1; i < 256; i++) //一次遍历每个像素
	{  
		sum0 = 0;
		sum1 = 0; 
		cnt0 = 0;
		cnt1 = 0;
		w0 = 0;
	    w1 = 0;
		for(j = 0; j < i; j++)
		{
			cnt0 += histogram[j];
			sum0 += j * histogram[j];
		}

		u0 = (double)sum0 /  cnt0; 
		w0 = (double)cnt0 / size;

		for(j = i ; j <= 255; j++)
		{
			cnt1 += histogram[j];
			sum1 += j * histogram[j];
		}

		u1 = (double)sum1 / cnt1;
		w1 = 1 - w0; // (double)cnt1 / size;

		u = u0 * w0 + u1 * w1; //图像的平均灰度
		printf("u = %f\n", u);
		//variance =  w0 * pow((u0 - u), 2) + w1 * pow((u1 - u), 2);
		variance =  w0 * w1 *  (u0 - u1) * (u0 - u1);
		if(variance > maxVariance) 
		{  
			maxVariance = variance;  
			threshold = i;  
		} 
	}  

	printf("threshold = %d\n", threshold);
	return threshold;  
}  

把w1写成w0 ··害我debug 了好久~~总是不认真,脑袋浑浑噩噩的···这都看不出来。。。。


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

对了,之前搜集的一个otsu的算法,代码如下(由于时间太久了,不知道出处了。。。膜拜大牛哈)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值