您的位置:首页 > 运维架构

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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: