如何利用遗传算法降低噪声图像的误差?

How to reduce the error of the noise image using genetic algorithm?

The question says, given a noise image (lena_noise) and original image (lena_original), the programmer is asked to design and implement a program that minimize the noise by given mathematical formula using Genetic algorithm.

我的问题是我的程序运行不好,真的很糟糕。这意味着当我从 lena_noise 开始时,程序应该会在一段时间后给我原始图像,但不幸的是它给出的图像比 lena_noise 差。所以我认为我的噪音程序有问题。所以,我希望找到一个关键点来找到如何使用遗传算法降低噪声?

 private int[][] Performing_Noise_into_grayscale_image(Storing_Images SI)
{
    this.Generate_New_Random_for_3_parameters();
    int [][] nose = new int[SI.heigth][SI.width];
    for (int row = 0; row < SI.heigth; row++) 
    {
        for (int col = 0; col < SI.width; col++) 
        {
            double no =  this.NoiseAmp*sin((2*Math.PI*this.NoiseFreqCol*row) + (2*Math.PI*this.NoiseFreqCol*col));
            int value = SI.Array[row][col];

            int alph = (value >> 24) & 0xff;
            int red = (value >> 16) & 0xff;
            int green = (value >> 8) & 0xff;
            int blue = value & 0xff;
            int avg = (red+green+blue)/3;

            alph = ((int)no) | (alph << 24);
            red = ((int) no) | (avg << 16);
            green = ((int) no) | (avg << 8);
            blue = ((int) no) | avg;

            int value2 = 0;
            value2 = (alph<<24) | (red<<16) | (green<<8) | blue;
            nose[row][col] = value2; 
        }
    }
    return nose;
}

函数 Generate_New_Random_for_3_parameters() 它只是一个函数,它为 3 个变量提供 0-30 之间的随机双数。这3个变量是数学公式中使用的(NoiseAmp, NoiseFreqCol, NoiseFreqCol)。

Storing_Images 有三个属性,它们是 (int [][] Array, int height, int width)

注意图片是灰度图,所以取平均值。


Brief about the program

程序有四个参数,如下:Crossover_probability = 0.3,mutation_probability = 0.05,number_of_population = 4,number_of_generations = 10。如果你对这些参数不熟悉,再看看Introduction to Genetic Algorithm。因此,程序从接收 lena_noise 开始,并在其上应用所有人口的数学公式(每个人口都有自己的随机双数数学公式),然后我们 select 最好的一个, 也就是与 lena_original 相比误差较小的那个。那么最好的就是能够为下一代生存的那个。在下一代上,我们对所有种群应用数学公式,然后我们 select 与 lena_original 图像相比,误差尽可能小的最好的。等等


Fitness函数如下,

public Storing_Images[] Fitness()
{
    Storing_Images errorSSI [] = new Storing_Images[4];
    for (int i = 0; i < (this.Childs.length) ; i++)
    {
        //choose the best one among 4
        //the best one is the one that have the minimum error from the orginal image.
        int [][] error = IP.Compare_OriginalImage_with_given_Image(Childs[i].Array);
        errorSSI[i] = new Storing_Images(error,512,512);
    }
   int value=0;
   int Sum [] = new int[4];
   for (int i = 0; i < (this.Childs.length) ; i++)
   {
       for (int row = 0; row < IP.width; row++) 
       {
          for (int col = 0; col < IP.height; col++) 
          {
              int val = errorSSI[i].Array[row][col];
              if ( val < 0 )
                  val = val * -1;
              value = value + val;
          }
       }
       double x = (value/(IP.width * IP.height));
       Sum[i] = (int) x;
       value =0;

   }
   int Mini=Sum[0];
   int posi_of_mini= 0;
  // int pos2=0;
   for (int k = 0; k < Sum.length; k++)
   {
       if ( Sum[k] < Mini )
       {
         //  pos2 = Mini;
           Mini = Sum[k];
           posi_of_mini = k;
       }
   }
   this.Childs[0] = Childs[posi_of_mini];
   this.Childs[1] = Childs[posi_of_mini];
   this.Childs[2] = Childs[posi_of_mini];
   this.Childs[3] = Childs[posi_of_mini];

   return Childs;

}

关于 lena_original 和人口之一之间的比较功能。

 public int [][] Compare_OriginalImage_with_given_Image(int [][] current) 
{
  int [][] error = new int [this.height][this.width];

  for (int row = 0; row < height; row++) 
  {
     for (int col = 0; col < width; col++) 
     {
         int value1 = this.Orginal[row][col];
         int value2 = current[row][col];
         //I need to put absolute value for either value 1 and value2
         if (value1 < 0)
             value1 = value1 *-1;
         if(value2 < 0)
             value2 = value2 * -1;
         //int min = Math.min(value1, value2);
         int er = value2 - value1;
         error[row][col] = er;

     }
  }
  return error;

}

Reference.

*与我的问题类似的问题,但没有关于答案的详细信息来自此页面 Image processing using genetic algorithm

*How to convert a color image into grayscale image in Java 这几页告诉我们如何处理灰度图像并使用它。


随时提出有关问题的问题。此外,欢迎任何评论、提示等。谢谢

试试这个来创建灰度图像:

public static void makeGray(BufferedImage img)
{
    for (int x = 0; x < img.getWidth(); x++) {
        for (int y = 0; y < img.getHeight(); y++) {
            int rgb = img.getRGB(x, y);
            int r = (rgb >> 16) & 0xFF;
            int g = (rgb >> 8) & 0xFF;
            int b = (rgb & 0xFF);

            int grayLevel = (r + g + b) / 3;
            int gray = (grayLevel << 16) + (grayLevel << 8) + grayLevel; 
            img.setRGB(x, y, gray);
        }
    }
}

现在,您需要一种方法来组合人口中的 2 个元素。 怎么样都无所谓,所以为了示例,我将图像平均分割:

for (int x = 0; x < img1.getWidth() / 2; x++) 
    for (int y = 0; y < img1.getHeight(); y++) 
        // build 1st half of the result image 

for (int x = img2.getWidth() / 2; x < img2.getWidth(); x++) 
    for (int y = 0; y < img2.getHeight(); y++) 
        // build 2nd half of the result image

您还需要考虑突变率,它可以改变结果图像上的随机元素。 (稍后再做)

GA是一种全局优化算法。用于图像去噪不是很方便。即使你找到了合适的公式,它也只适用于这对图像。