OpenCV实现PS2018中的SmartBlur美颜算法

//不处理边界

void smartBlur(Mat &Img, Mat &OutImg, int Radius, int Threshold) {


	OutImg.create(Img.size(),CV_8UC3);
	for (int i = Radius; i < Img.rows - Radius; i++) {


		uchar *curPtr = Img.ptr<uchar>(i);
		uchar *outPtr = OutImg.ptr<uchar>(i);
		for (int j = Radius; j < Img.cols - Radius; j++) {

			int curValue = curPtr[j * 3];
			int curValue2 = curPtr[j * 3 + 1];
			int curValue3 = curPtr[j * 3 + 2];

			int low = curValue - Threshold;
			int low2 = curValue2 - Threshold;
			int low3 = curValue3 - Threshold;

			int high = curValue + Threshold;
			int high2 = curValue2 + Threshold;
			int high3 = curValue3 + Threshold;

			int Index1 = 0;
			int Index2 = 0;
			int Index3 = 0;
			int cvx1 = 0;
			int cvx2 = 0;
			int cvx3 = 0;
			for (int r = -Radius; r <= Radius; r++) {
				uchar *tPtr = Img.ptr<uchar>(i + r);
				for (int c = -Radius; c <= Radius; c++) {
					int r = tPtr[(j + c) * 3];
					int g = tPtr[(j + c) * 3 + 1];
					int b = tPtr[(j + c) * 3 + 2];
					if (r > low&&r < high) {
						cvx1 += r;
						Index1++;
					}
					if (g > low2&&g < high2) {
						cvx2 += g;
						Index2++;
					}
					if (b > low3&&b < high3) {
						cvx3 += b;
						Index3++;
					}
				}
			}
			outPtr[j * 3] = cvx1 / Index1;
			outPtr[j * 3+1] = cvx2 / Index2;
			outPtr[j * 3+2] = cvx3 / Index3;
		}
	}

	

}


效果图对比
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值