数字图像处理均衡化灰度拉伸C++实现

/// <summary>
/// 均衡化灰度拉伸
/// </summary>
/// <param name="lpdst">输出图像数据</param>
/// <param name="lpsrc">输入图像数据</param>
/// <param name="width">图像宽度</param>
/// <param name="height">图像高度</param>
void equalize_gray(unsigned char *lpdst, const unsigned char *lpsrc, int width, int height)
{
	RLIB_RENAME(width, width_x_height);
	width_x_height *= height;

	int k = 0;
	int counts[256] = { 0 };
	// 保存灰度值个数
	for (; k < width_x_height; ++k) {
		++counts[lpsrc[k * 4]];
	}

	// 灰度映射表
	unsigned char gray_map[256];
	double calc_result = 255.0f / width_x_height;

	gray_map[0] = static_cast<unsigned char>(counts[0] * calc_result);

	int m;
	int temp;
	RLIB_RENAME(height, temp_2);
	for (k = 1; k < RLIB_COUNTOF(gray_map); ++k) {
		for (m = 0, temp = 0, temp_2 = 0; m <= k; m += 2) {
			temp   += counts[m];
			temp_2 += counts[m + 1];
		}
		gray_map[k] = static_cast<unsigned char>((temp + temp_2) * calc_result);

		for (m = 0, ++k, temp = 0, temp_2 = 0; m < k; m += 2) {
			temp   += counts[m];
			temp_2 += counts[m + 1];
		}

		gray_map[k] = static_cast<unsigned char>((temp + temp_2 + counts[k]) * calc_result);
	}

	// 查找映射表
	for (k = 0; k < width_x_height; ++k) {
		temp = k * 4;
		memset(&lpdst[temp], gray_map[x], 4);
		//lpdst[k * 4] = lpdst[k * 4 + 1] = lpdst[k * 4 + 2] = gray_map[x];
		lpdst[temp + 3] = 255;
	}
}

你可能感兴趣的:(C++,图像处理,gray)