ヒストグラムの等化:画像の拡張


void HistogramEqualization(unsigned char *pGrey,int nWidth, int nHeight)
{
	unsigned char* pData,pInResult[256];
	int			   pHist[256],i,j,nMulti;
	float		   pIntensity[256];
	pData = (unsigned char*)malloc(nWidth*nHeight*sizeof(unsigned char));
	memcpy(pData, pGrey, nWidth*nHeight*sizeof(unsigned char));
	memset(pHist, 0, sizeof(int)*256);
	nMulti = nWidth * nHeight;

	//                 
	for (i=0; i<nMulti; i++)
	{
		pHist[pData[i]]++;
	}
	//         ,          ,         
	memset(pIntensity, 0, sizeof(float)*256);
	pIntensity[0] = (float)pHist[0] / (float)nMulti;
	for (i=1; i<256; i++)
	{
		pIntensity[i] = (float)pHist[i] / (float)nMulti;
		pIntensity[i] += pIntensity[i-1];
	}
	//          

	for (i=0; i<256; i++)
	{
		pInResult[i] = (int)(255*pIntensity[i] + 0.5);
	}

	//         ,      ,           
	for(i=0; i<nMulti; i++)
	{
		pGrey[i] = pInResult[pData[i]];
	}

	free(pData);

}