学习OpenCV:滤镜系列(15)——羽化(模糊边缘)
==============================================
版权所有:小熊不去实验室CSDN博客
==============================================
#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);
}
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号