OpenCV给图像添加柏林噪声
2013-05-06 21:55
477 查看
float persistence = 0.55f; int Number_Of_Octaves = 3; /* 一个噪声发生器 */ float Noise1(int x, int y) { int n = x + y * 57; n = (n<<13) ^ n; return ( 1.0f - ( (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0f); } /* 一个光滑噪声发生器 */ float SmoothNoise_1(int x, int y) { float corners = ( Noise1(x-1, y-1)+Noise1(x+1, y-1)+Noise1(x-1, y+1)+Noise1(x+1, y+1) ) / 16.0f; float sides = ( Noise1(x-1, y) +Noise1(x+1, y) +Noise1(x, y-1) +Noise1(x, y+1) ) / 8.0f; float center = Noise1(x, y) / 4.0f; return corners + sides + center; } /* 插值函数 */ float Cosine_Interpolate(float a, float b, float x) { double ft = x * 3.1415927; double f = (1 - cos(ft)) * 0.5f; return (float)(a*(1-f) + b*f); } /* 插值噪声发生器 */ float InterpolatedNoise_1(float x, float y) { int integer_X = (int)floor(x+0.5); float fractional_X = x - integer_X; int integer_Y = (int)floor(y+0.5); float fractional_Y = y - integer_Y; float v1 = SmoothNoise_1(integer_X, integer_Y); float v2 = SmoothNoise_1(integer_X + 1, integer_Y); float v3 = SmoothNoise_1(integer_X, integer_Y + 1); float v4 = SmoothNoise_1(integer_X + 1, integer_Y + 1); float i1 = Cosine_Interpolate(v1 , v2 , fractional_X); float i2 = Cosine_Interpolate(v3 , v4 , fractional_X); return Cosine_Interpolate(i1 , i2 , fractional_Y); } /* 最终的PERLIN NOISE */ float PerlinNoise_2D(float x, float y) { float total = 0.0f; float p = persistence; int n = Number_Of_Octaves - 1; int ii; for(ii=0;ii<=n;ii++) { float frequency = (float) pow((float)2,ii); float amplitude = (float) pow(p,ii); total = total + InterpolatedNoise_1(x * frequency, y * frequency) * amplitude; } return total; } float myperlin( int x, int y ) { int ii, jj; float ynoise=0; for(ii=0; ii< x; ii++) for(jj=0; jj< y; jj++) { ynoise=PerlinNoise_2D((float)ii, (float)jj); ynoise= (float)((1+ynoise)); } return ynoise; }
IplImage* img = cvCreateImage(cvGetSize(GetDocument()->Image), IPL_DEPTH_32F, 0); // my code start here // extract image parameters IplImage* image1 = 0; uchar *data; image1 = cvCloneImage(GetDocument()->Image); int height = image1->height; int width = image1->width; int channels = image1->nChannels; int step = image1->widthStep; data = (uchar *)image1->imageData; //cvNamedWindow("src", CV_WINDOW_AUTOSIZE); //cvShowImage("src",image1); // add noise //CvSize size=cvGetSize(image1); IplImage *noise, *image2; CvScalar s; int i, j, k; float x1; // Initial noise and noised image noise= cvCreateImage(cvGetSize(image1), image1->depth, channels ); image2=cvCloneImage(image1); // Input noise parameters DLG1 dlg; if( dlg.DoModal() == IDOK ) { mid_int = dlg.m_mydata1; float deviate=(float)mid_int; // parameters for(i=0; i<height; i++) for(j=0; j<width; j++) { x1 = myperlin(i, j)*deviate; for(k=0; k<channels; k++) { s.val[k]=x1; } cvSet2D(noise, i, j, s); } cvAdd(image1, noise, image2); cvNamedWindow("Perlin_noised pic", CV_WINDOW_AUTOSIZE); cvShowImage("Perlin_noised pic",image2); //cvSaveImage("Perlin_noised.bmp",image2); global_image = cvCloneImage(image2); cvReleaseImage(&img); cvReleaseImage(&noise); cvReleaseImage(&image1); cvReleaseImage(&image2);
相关文章推荐
- 【OpenCV】给图像添加噪声
- Opencv 图像添加噪声
- 通过opencv给图像添加椒盐噪声
- 图像添加噪声【OpenCV学习笔记1】
- opencv学习笔记(1)----图像读写;添加噪声
- [转]图像去噪的OpenCV添加噪声和去噪
- 使用opencv给图像添加高斯噪声
- Episode Eight:opencv2.4.9实现向图像添加噪声和对图像滤波去噪
- 图像去噪的OPenCV添加噪声和去噪
- 用OpenCV的RNG类为图像添加高斯噪声
- opencv 降低图像亮度 + 添加噪声处理
- OpenCV特征点检测匹配图像-----添加包围盒
- Opencv 图像叠加 添加水印
- python opencv3 —— 常用工具、辅助函数、绘图函数(图像添加文本、矩形等几何形状)
- 图像添加高斯噪声、胡椒噪声、盐噪声和椒盐噪声
- OpenCV结合freetype在图像中添加中文
- opencv 初学添加slider模 4000 糊图像以及鼠标交互
- 图像基本噪声添加函数
- OpenCV特征点检测匹配图像-----添加包围盒
- (25)Air Band OpenCV2.4.13_为图像添加边界