图像处理算法之图像暗角特效

       基本所有图像美化app都有该特效,有的app叫晕影效果,有的app叫暗角效果,其实就是以图像为中心,提高图像四角的亮度或者降低亮度。为图像四角提高亮度的不多,更多的还是给图像增加暗角效果,以突出图像主体。 尤其是很多lomo滤镜,基本都会加一个暗角。那么暗角是怎么实现的呢,看似很简单,但真做到ps般色调过渡自然、平滑,看着舒服,还是需要一番计算与调试的。算法总体上没有复杂原理与计算,看代码基本就明白了。
       下面是相关示例代码(亮角: strength范围为[0.0f, 1.0f], 暗角: strength范围为[-1.0f, 0.0f]):
void ImageVignetting(BMPINFO *pSrcBitmap, float strength)
{
	int width = pSrcBitmap->lWidth;
	int height = pSrcBitmap->lHeight;
	float radio = 0.0;
	float maxLen = (float)MAX(width, height);
	float cx = maxLen * 0.5f;
	float cy = maxLen * 0.5f;
	float maxDist = cx*cx + cy*cy;
	uchar* pSrcData = pSrcBitmap->pPlane[0];

	if (strength > 0.0f)
	{
		// 亮角
		for (int i = 0; i < height; i++)
		{
			for (int j = 0; j < width; j++, pSrcData += 4)
			{
				float nx = (float)maxLen * j / width;
				float ny = (float)maxLen * i / height;
				float curDist = (nx - cx)*(nx - cx) + (ny - cy)*(ny - cy);
				radio = curDist / maxDist;
				// radio *= radio;
				radio *= strength;
				pSrcData[BLUE] = (uchar)CLAMP0255(pSrcData[BLUE]*(1.0f - radio) + SCREEN_XY(pSrcData[BLUE], 230)*radio);
				pSrcData[GREEN] = (uchar)CLAMP0255(pSrcData[GREEN]*(1.0f - radio) + SCREEN_XY(pSrcData[GREEN], 230)*radio);
				pSrcData[RED] = (uchar)CLAMP0255(pSrcData[RED]*(1.0f - radio) + SCREEN_XY(pSrcData[RED], 230)*radio);
			}
		}
	}
	else
	{
		// 暗角
		for (int i = 0; i < height; i++)
		{
			for (int j = 0; j < width; j++, pSrcData += 4)
			{
				float nx = (float)maxLen * j / width;
				float ny = (float)maxLen * i / height;
				float curDist = (nx - cx)*(nx - cx) + (ny - cy)*(ny - cy);
				radio = curDist / maxDist;
				// radio *= radio;
				radio *= strength;
				radio = 1.0f + radio;
				pSrcData[BLUE] = (uchar)CLAMP0255(pSrcData[BLUE]*radio);
				pSrcData[GREEN] = (uchar)CLAMP0255(pSrcData[GREEN]*radio);
				pSrcData[RED] = (uchar)CLAMP0255(pSrcData[RED]*radio);
			}
		}
	}
}
        下面是效果图:
                                            
                                            




  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值