现在的位置: 首页 > 综合 > 正文

学习OpenCV:滤镜系列(15)——羽化(模糊边缘)

2013年02月12日 ⁄ 综合 ⁄ 共 1430字 ⁄ 字号 评论关闭

==============================================

版权所有:小熊不去实验室CSDN博客

==============================================


在PHOTOSHOP里,羽化就是使你选定范围的图边缘达到朦胧的效果。 

羽化值越大,朦胧范围越宽,羽化值越小,朦胧范围越窄。可根据你想留下图的大小来调节。
算法分析:
1、通过对rgb值增加额外的V值实现朦胧效果
2、通过控制V值的大小实现范围控制。
3、V  = 255 * 当前点Point距中点距离的平方s1 / (顶点距中点的距离平方 *mSize)s2;
4、s1 有根据 ratio 修正 dx dy值。


#include <math.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#define MAXSIZE (32768)
using namespace cv;
using namespace std;



float mSize = 0.5;

int main()
{
	Mat src = imread("D:/img/arrow04.jpg",1);
	imshow("src",src);
	int width=src.cols;
	int heigh=src.rows;
	int centerX=width>>1;
	int centerY=heigh>>1;
	
	int maxV=centerX*centerX+centerY*centerY;
	int minV=(int)(maxV*(1-mSize));
	int diff= maxV -minV;
	float ratio = width >heigh ? (float)heigh/(float)width : (float)width/(float)heigh;
	
	Mat img;
	src.copyTo(img);

	Scalar avg=mean(src);
	Mat dst(img.size(),CV_8UC3);
	Mat mask1u[3];
	float tmp,r;
	for (int y=0;y<heigh;y++)
	{
		uchar* imgP=img.ptr<uchar>(y);
		uchar* dstP=dst.ptr<uchar>(y);
		for (int x=0;x<width;x++)
		{
			int b=imgP[3*x];
			int g=imgP[3*x+1];
			int r=imgP[3*x+2];

			float dx=centerX-x;
			float dy=centerY-y;
			
			if(width > heigh)
				 dx= (dx*ratio);
			else
				dy = (dy*ratio);

			int dstSq = dx*dx + dy*dy;

			float v = ((float) dstSq / diff)*255;

			r = (int)(r +v);
			g = (int)(g +v);
			b = (int)(b +v);
			r = (r>255 ? 255 : (r<0? 0 : r));
			g = (g>255 ? 255 : (g<0? 0 : g));
			b = (b>255 ? 255 : (b<0? 0 : b));

			dstP[3*x] = (uchar)b;
			dstP[3*x+1] = (uchar)g;
			dstP[3*x+2] = (uchar)r;
		}
	}
	imshow("羽化",dst);

	waitKey();
	imwrite("D:/img/羽化.jpg",dst);

}

原图:


羽化:
Reference:http://www.cnblogs.com/lipeil/archive/2012/09/21/2696519.html

抱歉!评论已关闭.