1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
   |  
void Bin_Otsu3( void *_imgsrc, void *_imgdst, uint32 _largeur, uint32 _hauteur )
{    
 
	uint16		histogram[256];
	uint16		omega[256]; 
	uint32		myu[256];   
 
	double	max_sigma;
	double	sigma;          //inter-class variance 
 
	uint8	i, threshold; 	// threshold for binarization 
	uint16	n;
	double	k; 				
 
 
	uint8	*psrc, *pdst, *pend;
 
	pend    = (uint8*)_imgsrc + _largeur * _hauteur;
	n		= _largeur * _hauteur; 	// n = 88 *72 = 6336 pixels 
 
 
	// Histogram generation 
 
	for (i = 0; i < 256; i++ )	
		histogram[ i ] = 0;
 
	for (psrc = (uint8*)_imgsrc; psrc < pend; psrc++)
		histogram[ *psrc ]++;
 
 
 
	// On calcule les deux premiers moments 
	// myu=moy et omega= ecart-type			
 
	omega[0] = histogram[0];		
	myu[0]   = 0;					
 
	for (i = 1; i < 256; i++) 
	{
		omega[i] = omega[i-1] + histogram[i]; 
 
		myu[i]   = myu[i-1] + i * histogram[i];
	}
 
 
	// sigma maximization
 
	threshold = 0;
	max_sigma = 0.0;
 
	for (i = 0; i < 256; i++) 
	{
		if ((omega[i] == 0) || (omega[i] == n))
			sigma = 0.0;
		else
		{
			k = myu[255] * omega[i] - n * myu[i];	
 
 
			sigma =  (k * k)/ (n * n * omega[i] * (n - omega[i]));	
		}
 
		if (sigma > max_sigma) 
		{
			max_sigma = sigma;
			threshold = i;
		}
	}
 
	// binarization with calculated threshold 
 
	for (psrc = (uint8*)_imgsrc, pdst = (uint8*)_imgdst; psrc < pend; psrc++, pdst++)
	{
		*pdst = (*psrc < threshold) ? 0 : 255;
	}
} | 
Partager