目的:
在opencv工程里面使用otsu分割灰度图像类似于matlab里的graythresh, opencv里面提供了otsu threshold的源代码“icvGetThreshVal_Otsu”,但是我们却不能直接调用它,只能把它拷贝出来使用(这就是开源的好处),下面代码是我在opencv工程应用当中应用otsu(大津法)的例子
//Input:
//pTile_pic: 灰度图像,*IplImage
//output:
//im_th: pTile_pic 的otsu阈值
//otsu threshold:
int size = 256;
float range[] = {0, 255};
float *pRange[] = {&range[0]};
CvHistogram *p_imhist = cvCreateHist(1, &size, CV_HIST_ARRAY, pRange);
cvCalcHist(&pTile_pic, p_imhist);
double im_th = icvGetThreshVal_Otsu(p_imhist);
//opencv Source code:
static double
icvGetThreshVal_Otsu( const CvHistogram* hist )
{
double max_val = 0;
CV_FUNCNAME( "icvGetThreshVal_Otsu" );
__BEGIN__;
int i, count;
const float* h;
double sum = 0, mu = 0;
bool uniform = false;
double low = 0, hig