Opencv去除高光

借鉴了yuyu[1]的代码,进行补全和小部分修改

#include   
#include

using namespace std;
using namespace cv;

int main(int argc, char** argv)
{

	Mat src = imread("xxx.jpg");
	if (src.empty())
	{
		cout << "fail to read image" << endl;
		return -1;
	}
	Mat dst=src.clone();
	
	int channels = src.channels();
	int row = src.rows;
	int col = src.cols*channels;

	int i = 0, j = 0;
	unsigned char R, G, B, MaxC;
	double alpha, beta, alpha_r, alpha_g, alpha_b, beta_r, beta_g, beta_b, temp = 0, realbeta = 0, minalpha = 0;
	double gama, gama_r, gama_g, gama_b;
	unsigned char* srcData;
	unsigned char* dstData;
	for (i = 0; i < row; i++)
	{
		srcData = (unsigned char*)src.ptr<uchar>(i);
		dstData = (unsigned char*)dst.ptr<uchar>(i);
		for (j = 0; j < col; j++)
		{
			
			R = srcData[j];
			G = srcData[j + 1];
			B = srcData[j + 2];

			alpha_r = (double)R / (double)(R + G + B);
			alpha_g = (double)G / (double)(R + G + B);
			alpha_b = (double)B / (double)(R + G + B);
			alpha = max(max(alpha_r, alpha_g), alpha_b);
			MaxC = max(max(R, G), B);// compute the maximum of the rgb channels
			minalpha = min(min(alpha_r, alpha_g), alpha_b);
			beta_r = 1 - (alpha - alpha_r) / (3 * alpha - 1);
			beta_g = 1 - (alpha - alpha_g) / (3 * alpha - 1);
			beta_b = 1 - (alpha - alpha_b) / (3 * alpha - 1);
			beta = max(max(beta_r, beta_g), beta_b);//将beta当做漫反射系数,则有
													// gama is used to approximiate the beta
			gama_r = (alpha_r - minalpha) / (1 - 3 * minalpha);
			gama_g = (alpha_g - minalpha) / (1 - 3 * minalpha);
			gama_b = (alpha_b - minalpha) / (1 - 3 * minalpha);
			gama = max(max(gama_r, gama_g), gama_b);

			temp = (gama*(R + G + B) - MaxC) / (3 * gama - 1);

			//beta=(alpha-minalpha)/(1-3*minalpha)+0.08;
			//temp=(beta*(R+G+B)-MaxC)/(3* beta -1);

			dstData[j] = R - (unsigned char)(temp + 0.5);
			dstData[j + 1] = G - (unsigned char)(temp + 0.5);
			dstData[j + 2] = B - (unsigned char)(temp + 0.5);
		}
	}
	imshow("DST",dst);
	imwrite("dst.jpg",dst);
	waitKey();
	return 0;
}

Opencv去除高光_第1张图片
Opencv去除高光_第2张图片
Opencv去除高光_第3张图片
Opencv去除高光_第4张图片
Opencv去除高光_第5张图片
参考文献
[1]:http://blog.sina.com.cn/s/blog_6388b0f8010137hy.html
[2]:Yang Q, Wang S, Ahuja N. Real-time specular highlight removal using bilateral filtering[C]//European conference on computer vision. Springer, Berlin, Heidelberg, 2010: 87-100.

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