这周单位要做一个人脸美化的项目,查资料遇到这位大牛的博客,地址如下:点击打开链接
我的代码也是在他的基础上进行修改的,但是他对图像的RGB三个通道平等调节,为了适应我的需求,我改成了针对三个通道分别调节。废话不多说,开始上源码
void ImageAdjust(Mat& src, Mat& dst, vector<double> low_in, vector<double> high_in, vector<double> low_out, vector<double> high_out, vector<double> gamma) { vector<double> low; vector<double> high; vector<double> bottom; vector<double> top; vector<double> err_in; vector<double> err_out; int N = low_in.size(); for (int i=0; i<N; i++) { low.push_back(low_in[i]*255); high.push_back(high_in[i]*255); bottom.push_back(low_out[i]*255); top.push_back(high_out[i]*255); err_in.push_back(high[i] - low[i]); err_out.push_back(top[i] - bottom[i]); } int x,y; vector<double> val; // intensity transform for( y = 0; y < src.rows; y++) { for (x = 0; x < src.cols; x++) { for (int i=0; i<N; i++) { double val = (src.at<Vec3b>(y, x)[i]); val = pow((val-low[i])/err_in[i], gamma[i])*err_out[i]+bottom[i]; // ☆ if(val > 255) val = 255; if(val < 0) val = 0; dst.at<Vec3b>(y, x)[i] = val; } } } }
测试主函数:
int _tmain(int argc, _TCHAR* argv[]) { Mat src = imread("test.png",1); Mat dst = Mat(src.size(), src.type()); if (!src.empty()) { double a[] = {0.0, 0.0, 0.0}; double b[] = {1.0, 1.0, 1.0}; double c[] = {0.0, 0.0, 0.0}; double d[] = {1.0, 1.0, 1.0}; double e[] = {1.0, 1.0, 1.0}; vector<double> low_in(a, a+3); vector<double> high_in(b, b+3); vector<double> low_out(c, c+3); vector<double> high_out(d, d+3); vector<double> gamma(e, e+3); ImageAdjust(src, dst, low_in, high_in, low_out, high_out, gamma ); imshow("src", src); imshow("dst", dst); waitKey(0); } return 0; }
原文:http://blog.csdn.net/huo065000/article/details/26725959