接口设计
static BOOL histeq(CImageDataset &imgIn, CImageDataset &imgOut);
运行结果
源代码
void CImageProcessingEx::getHist(const CImageDataset& img,int band,std::vector<int>& hist)
{
int value;
int N_row = img.m_ysize;
int N_col = img.m_xsize;
const double* imgData = img.m_data;
for(int row=0; row<N_row; ++row)
{
for(int col=0; col<N_col; ++col)
{
int index = band * N_row * N_col + row * N_col + col;
value=(UINT8)imgData[index];
hist[value]++;
}
}
}
BOOL CImageProcessingEx::histeq(CImageDataset &imgIn, CImageDataset &imgOut)
{
const int LEVEL = 256;
int k, row, col;
std::vector<int> hist(LEVEL);
std::vector<double> accuHist(LEVEL);
if(imgIn.empty())
{
return FALSE;
}
init(imgIn);
if(FALSE == imgOut.create(n_col,n_row,n_band))
{
return FALSE;
}
const double *input_data=imgIn.m_data;
double *output_data = imgOut.m_data;
int img_area = n_col * n_row;
for (int band = 0; band < n_band; band++)
{
getHist(imgIn, band,hist);
accuHist[0] = hist[0] / img_area;
for (k = 1; k < LEVEL - 1; k++)
{
accuHist[k] = accuHist[k - 1] + double(hist[k]) / img_area;
}
accuHist[255] = 1;
int index, gray_value;
for (row = 0; row < n_row; ++row)
{
for (col = 0; col < n_col; ++col)
{
index = coo2ind(band,row,col);
gray_value = (int)input_data[index];
output_data[index] = (LEVEL - 1) * accuHist[gray_value];
}
}
}
return TRUE;
}