【问题标题】:How to demosaic a bayer RAW image?如何去马赛克拜耳 RAW 图像?
【发布时间】:2018-08-05 03:57:37
【问题描述】:

我有一个专有的拜耳 RAW 图像,我需要在 Android 上对其进行去马赛克以显示图像。我正在逐字节读取文件头以检索分辨率信息(imageWidth/imageHeight)。在标头之后是来自字节 2000 及以下的像素数据。 如何在 Android 上对这个拜耳像素数据进行去马赛克以显示图像?

这是我读取字节数据并获取分辨率的代码:

File file = new File(path);
int size = (int) file.length();
byte[] bytes = new byte[size];
try {
    BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
    buf.read(bytes, 0, bytes.length);
    buf.close();
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

int x1 = bytes[1056];
int x2 = bytes[1057];
int x3 = bytes[1058];
int x4 = bytes[1059];
int imageWidth = (x1 << 24) + (x2 << 16) + (x3 << 8) + x4;

int y1 = bytes[1056];
int y2 = bytes[1057];
int y3 = bytes[1058];
int y4 = bytes[1059];
int imageHeigth = (y1 << 24) + (y2 << 16) + (y3 << 8) + y4;

// image data bytes[2000] and following, one pixel per byte
//
// bayer format:
//
// BGBGBGBG....
// GRGRGRGR....
// BGBGBGBG....
// ............
// ............

【问题讨论】:

    标签: android demosaicing


    【解决方案1】:

    您可以使用第三方 libaray OpenCV 对拜耳 RAW 图像进行去马赛克处理。 它有一个函数Imgproc.demosaicing(),可以将原始拜耳图像转换为所需格式

    你可以查找这个可以在android中使用的OpenCV的cpp。

    https://github.com/opencv/opencv/blob/master/modules/imgproc/src/demosaicing.cpp

    希望这会对你有所帮助。

    【讨论】:

      【解决方案2】:

      回答这个问题有点困难:有很多不同的 debayer/demosaic 算法。最新的非常复杂。

      您的主要问题是基于 Java 的 Android。真的没有很好的去拜耳库。

      这在很大程度上取决于您要使用哪种算法。以下是您可以根据 Imaging 库 ImageJ 在您的应用程序中实现的一些功能:

      public class Debayer_Image implements PlugInFilter {
      
      ImagePlus imp;
      ImageProcessor ip;
      int width;
      int height;
      static boolean normalize = false;
      static boolean equalize = false;
      static boolean stackHist = false;
      static boolean showColour = false;
      static boolean median = false;
      static boolean gauss = false;
      static int med_radius = 2;
      static int gauss_radius = 2;
      
      public int setup(String arg, ImagePlus imp) {
          IJ.register(Debayer_Image.class);
          if (IJ.versionLessThan("1.32c"))
              return DONE;
          imp.unlock();   
          this.imp = imp;
          return DOES_16;
      }
      
      public void run(ImageProcessor ip) {
          width = imp.getWidth();
          height = imp.getHeight();
          ImageStack rgb = new ImageStack(width, height, imp.getProcessor().getColorModel());
          String[] orders = {"R-G-R-G", "B-G-B-G", "G-R-G-R", "G-B-G-B"};
          String[] algorithms = {"Replication", "Bilinear", "Smooth Hue", "Adaptive Smooth Hue"}; 
      
          int row_order = /*TODO SET YOUR ROW ORDER HERE*/
          int algorithm = /*TODO SET YOUR ALGORITHM HERE*/
          /* TODO SET VALUES FOR normalize, equalize, ... */
      
          options = "saturated=0.5";
          if (normalize) options = options + " normalize";
          if (equalize) options = options + " equalize";
          options = options + " normalize_all";
          if (stackHist) options = options + " use"; 
      
          /* TODO ONLY USE ONE CALL */
          rgb = replicate_decode(row_order) 
              = average_decode(row_order);
              = smooth_decode(row_order);
              = adaptive_decode(row_order);
      
          if (median) IJ.run("Median...", "radius="+med_radius+" stack");
          if (gauss) IJ.run("Median...", "radius="+gauss_radius+" stack");
          if (normalize || equalize) IJ.run("Enhance Contrast", options);
      
          if (showColour) IJ.run("Convert Stack to RGB");
      
      }
      
      ImageStack replicate_decode(int row_order) { //Replication algorithm
          ip = imp.getProcessor();
          width = imp.getWidth();
          height = imp.getHeight();
          int one = 0;
          ImageStack rgb = new ImageStack(width, height, imp.getProcessor().getColorModel());
          ImageProcessor r = new ShortProcessor(width,height);
          ImageProcessor g = new ShortProcessor(width,height);
          ImageProcessor b = new ShortProcessor(width,height);
          //Short[] pixels = ip.getPixels();
      
      
          if (row_order == 0 || row_order == 1) {
              for (int y=0; y<height; y+=2) {
                  for (int x=0; x<width; x+=2) {
                      one = ip.getPixel(x,y);
                      b.putPixel(x,y,one);
                      b.putPixel(x+1,y,one);
                      b.putPixel(x,y+1,one);
                      b.putPixel(x+1,y+1,one);
                  }
              }
      
              for (int y=1; y<height; y+=2) {
                  for (int x=1; x<width; x+=2) {
                      one = ip.getPixel(x,y);
                      r.putPixel(x,y,one);
                      r.putPixel(x+1,y,one);
                      r.putPixel(x,y+1,one);
                      r.putPixel(x+1,y+1,one);
                  }
              }
      
              for (int y=0; y<height; y+=2) {
                  for (int x=1; x<width; x+=2) {
                      one = ip.getPixel(x,y);
                      g.putPixel(x,y,one);
                      g.putPixel(x+1,y,one);
                  }
              }   
      
              for (int y=1; y<height; y+=2) {
                  for (int x=0; x<width; x+=2) {
                      one = ip.getPixel(x,y);
                      g.putPixel(x,y,one);
                      g.putPixel(x+1,y,one);
                  }
              }   
      
              if (row_order == 0) {
                  rgb.addSlice("red",b);  
                  rgb.addSlice("green",g);
                  rgb.addSlice("blue",r); 
              }
              else if (row_order == 1) {
                  rgb.addSlice("red",r);  
                  rgb.addSlice("green",g);
                  rgb.addSlice("blue",b);         
              }
          }
      
          else if (row_order == 2 || row_order == 3) {
              for (int y=1; y<height; y+=2) {
                  for (int x=0; x<width; x+=2) {
                      one = ip.getPixel(x,y);
                      b.putPixel(x,y,one);
                      b.putPixel(x+1,y,one);
                      b.putPixel(x,y+1,one);
                      b.putPixel(x+1,y+1,one);
                  }
              }
      
              for (int y=0; y<height; y+=2) {
                  for (int x=1; x<width; x+=2) {
                      one = ip.getPixel(x,y);
                      r.putPixel(x,y,one);
                      r.putPixel(x+1,y,one);
                      r.putPixel(x,y+1,one);
                      r.putPixel(x+1,y+1,one);
                  }
              }
      
              for (int y=0; y<height; y+=2) {
                  for (int x=0; x<width; x+=2) {
                      one = ip.getPixel(x,y);
                      g.putPixel(x,y,one);
                      g.putPixel(x+1,y,one);
                  }
              }   
      
              for (int y=1; y<height; y+=2) {
                  for (int x=1; x<width; x+=2) {
                      one = ip.getPixel(x,y);
                      g.putPixel(x,y,one);
                      g.putPixel(x+1,y,one);
                  }
              }   
      
              if (row_order == 2) {
                  rgb.addSlice("red",b);  
                  rgb.addSlice("green",g);
                  rgb.addSlice("blue",r); 
              }
              else if (row_order == 3) {
                  rgb.addSlice("red",r);  
                  rgb.addSlice("green",g);
                  rgb.addSlice("blue",b);         
              }
          }
      
          return rgb;
      
      
      }
      
      ImageStack average_decode(int row_order) {          //Bilinear algorithm
          ip = imp.getProcessor();
          width = imp.getWidth();
          height = imp.getHeight();
          int one = 0;
          int two = 0;
          int three = 0;
          int four = 0;
          ImageStack rgb = new ImageStack(width, height, imp.getProcessor().getColorModel());
          ImageProcessor r = new ShortProcessor(width,height);
          ImageProcessor g = new ShortProcessor(width,height);
          ImageProcessor b = new ShortProcessor(width,height);
          //Short[] pixels = ip.getPixels();
      
      
          if (row_order == 0 || row_order == 1) {
              for (int y=0; y<height; y+=2) {
                  for (int x=0; x<width; x+=2) {
                      one = ip.getPixel(x,y);
                      two = ip.getPixel(x+2,y);
                      three = ip.getPixel(x,y+2);
                      four = ip.getPixel(x+2,y+2);
      
                      b.putPixel(x,y,one);
                      b.putPixel(x+1,y,(one+two)/2);
                      b.putPixel(x,y+1,(one+three)/2);
                      b.putPixel(x+1,y+1,(one+two+three+four)/4);
                  }
              }
      
              for (int y=1; y<height; y+=2) {
                  for (int x=1; x<width; x+=2) {
                      one = ip.getPixel(x,y);
                      two = ip.getPixel(x+2,y);
                      three = ip.getPixel(x,y+2);
                      four = ip.getPixel(x+2,y+2);
      
                      r.putPixel(x,y,one);
                      r.putPixel(x+1,y,(one+two)/2);
                      r.putPixel(x,y+1,(one+three)/2);
                      r.putPixel(x+1,y+1,(one+two+three+four)/4);
                  }
              }
      
              for (int y=0; y<height; y+=2) {
                  for (int x=1; x<width; x+=2) {
                      one = ip.getPixel(x,y);
                      two = ip.getPixel(x+2,y);
                      three = ip.getPixel(x+1,y+1);
                      four = ip.getPixel(x+1,y-1);
      
                      g.putPixel(x,y,one);
                      g.putPixel(x+1,y,(one+two+three+four)/4);
                  }
              }   
      
              for (int y=1; y<height; y+=2) {
                  for (int x=0; x<width; x+=2) {
                      one = ip.getPixel(x,y);
                      two = ip.getPixel(x+2,y);
                      three = ip.getPixel(x+1,y+1);
                      four = ip.getPixel(x+1,y-1);
      
                      g.putPixel(x,y,one);
                      g.putPixel(x+1,y,(one+two+three+four)/4);
                  }
              }   
      
              if (row_order == 0) {
                  rgb.addSlice("red",b);  
                  rgb.addSlice("green",g);
                  rgb.addSlice("blue",r); 
              }
              else if (row_order == 1) {
                  rgb.addSlice("red",r);  
                  rgb.addSlice("green",g);
                  rgb.addSlice("blue",b);         
              }
          }
      
          else if (row_order == 2 || row_order == 3) {
              for (int y=1; y<height; y+=2) {
                  for (int x=0; x<width; x+=2) {
                      one = ip.getPixel(x,y);
                      two = ip.getPixel(x+2,y);
                      three = ip.getPixel(x,y+2);
                      four = ip.getPixel(x+2,y+2);
      
                      b.putPixel(x,y,one);
                      b.putPixel(x+1,y,(one+two)/2);
                      b.putPixel(x,y+1,(one+three)/2);
                      b.putPixel(x+1,y+1,(one+two+three+four)/4);
                  }
              }
      
              for (int y=0; y<height; y+=2) {
                  for (int x=1; x<width; x+=2) {
                      one = ip.getPixel(x,y);
                      two = ip.getPixel(x+2,y);
                      three = ip.getPixel(x,y+2);
                      four = ip.getPixel(x+2,y+2);
      
                      r.putPixel(x,y,one);
                      r.putPixel(x+1,y,(one+two)/2);
                      r.putPixel(x,y+1,(one+three)/2);
                      r.putPixel(x+1,y+1,(one+two+three+four)/4);             
                  }
              }
      
              for (int y=0; y<height; y+=2) {
                  for (int x=0; x<width; x+=2) {
                      one = ip.getPixel(x,y);
                      two = ip.getPixel(x+2,y);
                      three = ip.getPixel(x+1,y+1);
                      four = ip.getPixel(x+1,y-1);
      
                      g.putPixel(x,y,one);
                      g.putPixel(x+1,y,(one+two+three+four)/4);
                  }
              }   
      
              for (int y=1; y<height; y+=2) {
                  for (int x=1; x<width; x+=2) {
                      one = ip.getPixel(x,y);
                      two = ip.getPixel(x+2,y);
                      three = ip.getPixel(x+1,y+1);
                      four = ip.getPixel(x+1,y-1);
      
                      g.putPixel(x,y,one);
                      g.putPixel(x+1,y,(one+two+three+four)/4);
                  }
              }   
      
              if (row_order == 2) {
                  rgb.addSlice("red",b);  
                  rgb.addSlice("green",g);
                  rgb.addSlice("blue",r); 
              }
              else if (row_order == 3) {
                  rgb.addSlice("red",r);  
                  rgb.addSlice("green",g);
                  rgb.addSlice("blue",b);         
              }
          }
      
          return rgb;
      
      
      }
      
      ImageStack smooth_decode(int row_order) {           //Smooth Hue algorithm
          ip = imp.getProcessor();
          width = imp.getWidth();
          height = imp.getHeight();
          double G1 = 0;
          double G2 = 0;
          double G3 = 0;
          double G4 = 0;
          double G5 = 0;
          double G6 = 0;
          double G7 = 0;
          double G8 = 0;
          double G9 = 0;
          double B1 = 0;
          double B2 = 0;
          double B3 = 0;
          double B4 = 0;
          double R1 = 0;
          double R2 = 0;
          double R3 = 0;
          double R4 = 0;
          ImageStack rgb = new ImageStack(width, height, imp.getProcessor().getColorModel());
          ImageProcessor r = new ShortProcessor(width,height);
          ImageProcessor g = new ShortProcessor(width,height);
          ImageProcessor b = new ShortProcessor(width,height);
          //Short[] pixels = ip.getPixels();
      
      
          if (row_order == 0 || row_order == 1) {
              //Solve for green pixels first
              for (int y=0; y<height; y+=2) {
                  for (int x=1; x<width; x+=2) {
                      G1 = ip.getPixel(x,y);
                      G2 = ip.getPixel(x+2,y);
                      G3 = ip.getPixel(x+1,y+1);
                      G4 = ip.getPixel(x+1,y-1);
      
                      g.putPixel(x,y,(int)G1);
                      if (y==0) g.putPixel(x+1,y,(int)((G1+G2+G3)/3));
                      else g.putPixel(x+1,y,(int)((G1+G2+G3+G4)/4));
                      if (x==1) g.putPixel(x-1,y,(int)((G1+G4+ip.getPixel(x-1,y+1))/3));
                  }
              }   
      
              for (int x=0; x<width; x+=2) {  
                  for (int y=1; y<height; y+=2) {
      
                      G1 = ip.getPixel(x,y);
                      G2 = ip.getPixel(x+2,y);
                      G3 = ip.getPixel(x+1,y+1);
                      G4 = ip.getPixel(x+1,y-1);
      
                      g.putPixel(x,y,(int)G1);
                      if (x==0) g.putPixel(x+1,y,(int)((G1+G2+G3)/3));
                      else g.putPixel(x+1,y,(int)((G1+G2+G3+G4)/4));
                  }
              }   
      
              g.putPixel(0,0,(int)((ip.getPixel(0,1)+ip.getPixel(1,0))/2));
      
      
              for (int y=0; y<height; y+=2) {
                  for (int x=0; x<width; x+=2) {
                      B1 = ip.getPixel(x,y);
                      B2 = ip.getPixel(x+2,y);
                      B3 = ip.getPixel(x,y+2);
                      B4 = ip.getPixel(x+2,y+2);
                      G1 = g.getPixel(x,y);
                      G2 = g.getPixel(x+2,y);
                      G3 = g.getPixel(x,y+2);
                      G4 = g.getPixel(x+2,y+2);
                      G5 = g.getPixel(x+1,y);
                      G6 = g.getPixel(x,y+1);
                      G9 = g.getPixel(x+1,y+1);
                      if(G1==0) G1=1;
                      if(G2==0) G2=1;
                      if(G3==0) G3=1;
                      if(G4==0) G4=1;
      
                      b.putPixel(x,y,(int)(B1));
                      b.putPixel(x+1,y,(int)((G5/2 * ((B1/G1) + (B2/G2)) )) );
                      b.putPixel(x,y+1,(int)(( G6/2 * ((B1/G1) + (B3/G3)) )) );
                      b.putPixel(x+1,y+1, (int)((G9/4 *  ((B1/G1) + (B3/G3) + (B2/G2) + (B4/G4)) )) );
      
                  }
              }
      
              for (int y=1; y<height; y+=2) {
                  for (int x=1; x<width; x+=2) {
                      R1 = ip.getPixel(x,y);
                      R2 = ip.getPixel(x+2,y);
                      R3 = ip.getPixel(x,y+2);
                      R4 = ip.getPixel(x+2,y+2);
                      G1 = g.getPixel(x,y);
                      G2 = g.getPixel(x+2,y);
                      G3 = g.getPixel(x,y+2);
                      G4 = g.getPixel(x+2,y+2);
                      G5 = g.getPixel(x+1,y);
                      G6 = g.getPixel(x,y+1);
                      G9 = g.getPixel(x+1,y+1);
                      if(G1==0) G1=1;
                      if(G2==0) G2=1;
                      if(G3==0) G3=1;
                      if(G4==0) G4=1;
      
                      r.putPixel(x,y,(int)(R1));
                      r.putPixel(x+1,y,(int)((G5/2 * ((R1/G1) + (R2/G2) )) ));
                      r.putPixel(x,y+1,(int)(( G6/2 * ((R1/G1) + (R3/G3) )) ));
                      r.putPixel(x+1,y+1, (int)((G9/4 *  ((R1/G1) + (R3/G3) + (R2/G2) + (R4/G4)) ) ));
                  }
              }
      
      
              if (row_order == 0) {
                  rgb.addSlice("red",b);  
                  rgb.addSlice("green",g);
                  rgb.addSlice("blue",r); 
              }
              else if (row_order == 1) {
                  rgb.addSlice("red",r);  
                  rgb.addSlice("green",g);
                  rgb.addSlice("blue",b);         
              }
          }
      
          else if (row_order == 2 || row_order == 3) {
      
              for (int y=0; y<height; y+=2) {
                  for (int x=0; x<width; x+=2) {
                      G1 = ip.getPixel(x,y);
                      G2 = ip.getPixel(x+2,y);
                      G3 = ip.getPixel(x+1,y+1);
                      G4 = ip.getPixel(x+1,y-1);
      
                      g.putPixel(x,y,(int)G1);
                      if (y==0) g.putPixel(x+1,y,(int)((G1+G2+G3)/3));
                      else g.putPixel(x+1,y,(int)((G1+G2+G3+G4)/4));
                      if (x==1) g.putPixel(x-1,y,(int)((G1+G4+ip.getPixel(x-1,y+1))/3));
                  }
              }   
      
              for (int y=1; y<height; y+=2) {
                  for (int x=1; x<width; x+=2) {
                      G1 = ip.getPixel(x,y);
                      G2 = ip.getPixel(x+2,y);
                      G3 = ip.getPixel(x+1,y+1);
                      G4 = ip.getPixel(x+1,y-1);
      
                      g.putPixel(x,y,(int)G1);
                      if (x==0) g.putPixel(x+1,y,(int)((G1+G2+G3)/3));
                      else g.putPixel(x+1,y,(int)((G1+G2+G3+G4)/4));
                  }
              }
      
              g.putPixel(0,0,(int)((ip.getPixel(0,1)+ip.getPixel(1,0))/2));
      
              for (int y=1; y<height; y+=2) {
                  for (int x=0; x<width; x+=2) {
                      B1 = ip.getPixel(x,y);
                      B2 = ip.getPixel(x+2,y);
                      B3 = ip.getPixel(x,y+2);
                      B4 = ip.getPixel(x+2,y+2);
                      G1 = g.getPixel(x,y);
                      G2 = g.getPixel(x+2,y);
                      G3 = g.getPixel(x,y+2);
                      G4 = g.getPixel(x+2,y+2);
                      G5 = g.getPixel(x+1,y);
                      G6 = g.getPixel(x,y+1);
                      G9 = g.getPixel(x+1,y+1);
                      if(G1==0) G1=1;
                      if(G2==0) G2=1;
                      if(G3==0) G3=1;
                      if(G4==0) G4=1;
      
                      b.putPixel(x,y,(int)(B1));
                      b.putPixel(x+1,y,(int)((G5/2 * ((B1/G1) + (B2/G2)) )) );
                      b.putPixel(x,y+1,(int)(( G6/2 * ((B1/G1) + (B3/G3)) )) );
                      b.putPixel(x+1,y+1, (int)((G9/4 *  ((B1/G1) + (B3/G3) + (B2/G2) + (B4/G4)) )) );        
      
                  }
              }
      
              for (int y=0; y<height; y+=2) {
                  for (int x=1; x<width; x+=2) {
                      R1 = ip.getPixel(x,y);
                      R2 = ip.getPixel(x+2,y);
                      R3 = ip.getPixel(x,y+2);
                      R4 = ip.getPixel(x+2,y+2);
                      G1 = g.getPixel(x,y);
                      G2 = g.getPixel(x+2,y);
                      G3 = g.getPixel(x,y+2);
                      G4 = g.getPixel(x+2,y+2);
                      G5 = g.getPixel(x+1,y);
                      G6 = g.getPixel(x,y+1);
                      G9 = g.getPixel(x+1,y+1);
                      if(G1==0) G1=1;
                      if(G2==0) G2=1;
                      if(G3==0) G3=1;
                      if(G4==0) G4=1;
      
                      r.putPixel(x,y,(int)(R1));
                      r.putPixel(x+1,y,(int)((G5/2 * ((R1/G1) + (R2/G2) )) ));
                      r.putPixel(x,y+1,(int)(( G6/2 * ((R1/G1) + (R3/G3) )) ));
                      r.putPixel(x+1,y+1, (int)((G9/4 *  ((R1/G1) + (R3/G3) + (R2/G2) + (R4/G4)) ) ));        
                  }
              }
      
      
      
              if (row_order == 2) {
                  rgb.addSlice("red",b);  
                  rgb.addSlice("green",g);
                  rgb.addSlice("blue",r); 
              }
              else if (row_order == 3) {
                  rgb.addSlice("red",r);  
                  rgb.addSlice("green",g);
                  rgb.addSlice("blue",b);         
              }
          }
      
          return rgb;
      
      
      }
      
      ImageStack adaptive_decode(int row_order) {         //Adaptive Smooth Hue algorithm (Edge detecting)
          ip = imp.getProcessor();
          width = imp.getWidth();
          height = imp.getHeight();
          double G1 = 0;
          double G2 = 0;
          double G3 = 0;
          double G4 = 0;
          double G5 = 0;
          double G6 = 0;
          double G7 = 0;
          double G8 = 0;
          double G9 = 0;
          double B1 = 0;
          double B2 = 0;
          double B3 = 0;
          double B4 = 0;
          double B5 = 0;
          double R1 = 0;
          double R2 = 0;
          double R3 = 0;
          double R4 = 0;
          double R5 = 0;
          double N = 0;
          double S = 0;
          double E = 0;
          double W = 0;
          ImageStack rgb = new ImageStack(width, height, imp.getProcessor().getColorModel());
          ImageProcessor r = new ShortProcessor(width,height);
          ImageProcessor g = new ShortProcessor(width,height);
          ImageProcessor b = new ShortProcessor(width,height);
          //Short[] pixels = ip.getPixels();
      
      
          if (row_order == 0 || row_order == 1) {
              //Solve for green pixels first
              for (int y=0; y<height; y+=2) {
                  for (int x=1; x<width; x+=2) {
                      G1 = ip.getPixel(x,y);
                      G2 = ip.getPixel(x+2,y);
                      G3 = ip.getPixel(x+1,y+1);
                      G4 = ip.getPixel(x+1,y-1);
                      R1 = ip.getPixel(x-1,y);
                      R2 = ip.getPixel(x+3,y);
                      R3 = ip.getPixel(x+1,y+2);
                      R4 = ip.getPixel(x+1,y-2);
                      R5 = ip.getPixel(x+1,y+1);
      
                      N = Math.abs(R4-R5)*2 + Math.abs(G4-G3);
                      S = Math.abs(R5-R3)*2 + Math.abs(G4-G3);
                      E = Math.abs(R5-R2)*2 + Math.abs(G1-G2);
                      W = Math.abs(R1-R5)*2 + Math.abs(G1-G2);
      
                      if(N<S && N<E && N<W) {
                          g.putPixel(x+1,y,(int)((G4*3 + R5 + G3 - R4)/4));
                      }
      
                      else if(S<N && S<E && S<W) {
                          g.putPixel(x+1,y,(int)((G3*3 + R5 + G4 - R3)/4));
                      }
      
                      else if(W<N && W<E && W<S) {
                          g.putPixel(x+1,y,(int)((G1*3 + R5 + G2 - R1)/4));
                      }
      
                      else if(E<N && E<S && E<W) {
                          g.putPixel(x+1,y,(int)((G2*3 + R5 + G1 - R2)/4));
                      }
      
                      g.putPixel(x,y,(int)G1);
      
                      if (y==0) g.putPixel(x+1,y,(int)((G1+G2+G3)/3));
                      else g.putPixel(x+1,y,(int)((G1+G2+G3+G4)/4));
                      if (x==1) g.putPixel(x-1,y,(int)((G1+G4+ip.getPixel(x-1,y+1))/3));
                  }
              }   
      
              for (int x=0; x<width; x+=2) {  
                  for (int y=1; y<height; y+=2) {
                      G1 = ip.getPixel(x,y);
                      G2 = ip.getPixel(x+2,y);
                      G3 = ip.getPixel(x+1,y+1);
                      G4 = ip.getPixel(x+1,y-1);
                      R1 = ip.getPixel(x-1,y);
                      R2 = ip.getPixel(x+3,y);
                      R3 = ip.getPixel(x+1,y+2);
                      R4 = ip.getPixel(x+1,y-2);
                      R5 = ip.getPixel(x+1,y+1);
      
                      N = Math.abs(R4-R5)*2 + Math.abs(G4-G3);
                      S = Math.abs(R5-R3)*2 + Math.abs(G4-G3);
                      E = Math.abs(R5-R2)*2 + Math.abs(G1-G2);
                      W = Math.abs(R1-R5)*2 + Math.abs(G1-G2);
      
                      if(N<S && N<E && N<W) {
                          g.putPixel(x+1,y,(int)((G4*3 + R5 + G3 - R4)/4));
                      }
      
                      else if(S<N && S<E && S<W) {
                          g.putPixel(x+1,y,(int)((G3*3 + R5 + G4 - R3)/4));
                      }
      
                      else if(W<N && W<E && W<S) {
                          g.putPixel(x+1,y,(int)((G1*3 + R5 + G2 - R1)/4));
                      }
      
                      else if(E<N && E<S && E<W) {
                          g.putPixel(x+1,y,(int)((G2*3 + R5 + G1 - R2)/4));
                      }
      
                      g.putPixel(x,y,(int)G1);
                      if (x==0) g.putPixel(x+1,y,(int)((G1+G2+G3)/3));
                      else g.putPixel(x+1,y,(int)((G1+G2+G3+G4)/4));
                  }
              }   
      
              g.putPixel(0,0,(int)((ip.getPixel(0,1)+ip.getPixel(1,0))/2));
      
      
              for (int y=0; y<height; y+=2) {
                  for (int x=0; x<width; x+=2) {
                      B1 = ip.getPixel(x,y);
                      B2 = ip.getPixel(x+2,y);
                      B3 = ip.getPixel(x,y+2);
                      B4 = ip.getPixel(x+2,y+2);
                      G1 = g.getPixel(x,y);
                      G2 = g.getPixel(x+2,y);
                      G3 = g.getPixel(x,y+2);
                      G4 = g.getPixel(x+2,y+2);
                      G5 = g.getPixel(x+1,y);
                      G6 = g.getPixel(x,y+1);
                      G9 = g.getPixel(x+1,y+1);
                      if(G1==0) G1=1;
                      if(G2==0) G2=1;
                      if(G3==0) G3=1;
                      if(G4==0) G4=1;
      
                      b.putPixel(x,y,(int)(B1));
                      b.putPixel(x+1,y,(int)((G5/2 * ((B1/G1) + (B2/G2)) )) );
                      b.putPixel(x,y+1,(int)(( G6/2 * ((B1/G1) + (B3/G3)) )) );
                      b.putPixel(x+1,y+1, (int)((G9/4 *  ((B1/G1) + (B3/G3) + (B2/G2) + (B4/G4)) )) );
      
                  }
              }
      
              for (int y=1; y<height; y+=2) {
                  for (int x=1; x<width; x+=2) {
                      R1 = ip.getPixel(x,y);
                      R2 = ip.getPixel(x+2,y);
                      R3 = ip.getPixel(x,y+2);
                      R4 = ip.getPixel(x+2,y+2);
                      G1 = g.getPixel(x,y);
                      G2 = g.getPixel(x+2,y);
                      G3 = g.getPixel(x,y+2);
                      G4 = g.getPixel(x+2,y+2);
                      G5 = g.getPixel(x+1,y);
                      G6 = g.getPixel(x,y+1);
                      G9 = g.getPixel(x+1,y+1);
                      if(G1==0) G1=1;
                      if(G2==0) G2=1;
                      if(G3==0) G3=1;
                      if(G4==0) G4=1;
      
                      r.putPixel(x,y,(int)(R1));
                      r.putPixel(x+1,y,(int)((G5/2 * ((R1/G1) + (R2/G2) )) ));
                      r.putPixel(x,y+1,(int)(( G6/2 * ((R1/G1) + (R3/G3) )) ));
                      r.putPixel(x+1,y+1, (int)((G9/4 *  ((R1/G1) + (R3/G3) + (R2/G2) + (R4/G4)) ) ));
                  }
              }
      
      
              if (row_order == 0) {
                  rgb.addSlice("red",b);  
                  rgb.addSlice("green",g);
                  rgb.addSlice("blue",r); 
              }
              else if (row_order == 1) {
                  rgb.addSlice("red",r);  
                  rgb.addSlice("green",g);
                  rgb.addSlice("blue",b);         
              }
          }
      
          else if (row_order == 2 || row_order == 3) {
      
              for (int y=0; y<height; y+=2) {
                  for (int x=0; x<width; x+=2) {
                      G1 = ip.getPixel(x,y);
                      G2 = ip.getPixel(x+2,y);
                      G3 = ip.getPixel(x+1,y+1);
                      G4 = ip.getPixel(x+1,y-1);
                      R1 = ip.getPixel(x-1,y);
                      R2 = ip.getPixel(x+3,y);
                      R3 = ip.getPixel(x+1,y+2);
                      R4 = ip.getPixel(x+1,y-2);
                      R5 = ip.getPixel(x+1,y+1);
      
                      N = Math.abs(R4-R5)*2 + Math.abs(G4-G3);
                      S = Math.abs(R5-R3)*2 + Math.abs(G4-G3);
                      E = Math.abs(R5-R2)*2 + Math.abs(G1-G2);
                      W = Math.abs(R1-R5)*2 + Math.abs(G1-G2);
      
                      if(N<S && N<E && N<W) {
                          g.putPixel(x+1,y,(int)((G4*3 + R5 + G3 - R4)/4));
                      }
      
                      else if(S<N && S<E && S<W) {
                          g.putPixel(x+1,y,(int)((G3*3 + R5 + G4 - R3)/4));
                      }
      
                      else if(W<N && W<E && W<S) {
                          g.putPixel(x+1,y,(int)((G1*3 + R5 + G2 - R1)/4));
                      }
      
                      else if(E<N && E<S && E<W) {
                          g.putPixel(x+1,y,(int)((G2*3 + R5 + G1 - R2)/4));
                      }
      
                      g.putPixel(x,y,(int)G1);
                      if (y==0) g.putPixel(x+1,y,(int)((G1+G2+G3)/3));
                      else g.putPixel(x+1,y,(int)((G1+G2+G3+G4)/4));
                      if (x==1) g.putPixel(x-1,y,(int)((G1+G4+ip.getPixel(x-1,y+1))/3));
                  }
              }   
      
              for (int y=1; y<height; y+=2) {
                  for (int x=1; x<width; x+=2) {
                      G1 = ip.getPixel(x,y);
                      G2 = ip.getPixel(x+2,y);
                      G3 = ip.getPixel(x+1,y+1);
                      G4 = ip.getPixel(x+1,y-1);
                      R1 = ip.getPixel(x-1,y);
                      R2 = ip.getPixel(x+3,y);
                      R3 = ip.getPixel(x+1,y+2);
                      R4 = ip.getPixel(x+1,y-2);
                      R5 = ip.getPixel(x+1,y+1);
      
                      N = Math.abs(R4-R5)*2 + Math.abs(G4-G3);
                      S = Math.abs(R5-R3)*2 + Math.abs(G4-G3);
                      E = Math.abs(R5-R2)*2 + Math.abs(G1-G2);
                      W = Math.abs(R1-R5)*2 + Math.abs(G1-G2);
      
                      if(N<S && N<E && N<W) {
                          g.putPixel(x+1,y,(int)((G4*3 + R5 + G3 - R4)/4));
                      }
      
                      else if(S<N && S<E && S<W) {
                          g.putPixel(x+1,y,(int)((G3*3 + R5 + G4 - R3)/4));
                      }
      
                      else if(W<N && W<E && W<S) {
                          g.putPixel(x+1,y,(int)((G1*3 + R5 + G2 - R1)/4));
                      }
      
                      else if(E<N && E<S && E<W) {
                          g.putPixel(x+1,y,(int)((G2*3 + R5 + G1 - R2)/4));
                      }
      
                      g.putPixel(x,y,(int)G1);
                      if (x==0) g.putPixel(x+1,y,(int)((G1+G2+G3)/3));
                      else g.putPixel(x+1,y,(int)((G1+G2+G3+G4)/4));
                  }
              }
      
              g.putPixel(0,0,(int)((ip.getPixel(0,1)+ip.getPixel(1,0))/2));
      
              for (int y=1; y<height; y+=2) {
                  for (int x=0; x<width; x+=2) {
                      B1 = ip.getPixel(x,y);
                      B2 = ip.getPixel(x+2,y);
                      B3 = ip.getPixel(x,y+2);
                      B4 = ip.getPixel(x+2,y+2);
                      G1 = g.getPixel(x,y);
                      G2 = g.getPixel(x+2,y);
                      G3 = g.getPixel(x,y+2);
                      G4 = g.getPixel(x+2,y+2);
                      G5 = g.getPixel(x+1,y);
                      G6 = g.getPixel(x,y+1);
                      G9 = g.getPixel(x+1,y+1);
                      if(G1==0) G1=1;
                      if(G2==0) G2=1;
                      if(G3==0) G3=1;
                      if(G4==0) G4=1;
      
                      b.putPixel(x,y,(int)(B1));
                      b.putPixel(x+1,y,(int)((G5/2 * ((B1/G1) + (B2/G2)) )) );
                      b.putPixel(x,y+1,(int)(( G6/2 * ((B1/G1) + (B3/G3)) )) );
                      b.putPixel(x+1,y+1, (int)((G9/4 *  ((B1/G1) + (B3/G3) + (B2/G2) + (B4/G4)) )) );        
      
                  }
              }
      
              for (int y=0; y<height; y+=2) {
                  for (int x=1; x<width; x+=2) {
                      R1 = ip.getPixel(x,y);
                      R2 = ip.getPixel(x+2,y);
                      R3 = ip.getPixel(x,y+2);
                      R4 = ip.getPixel(x+2,y+2);
                      G1 = g.getPixel(x,y);
                      G2 = g.getPixel(x+2,y);
                      G3 = g.getPixel(x,y+2);
                      G4 = g.getPixel(x+2,y+2);
                      G5 = g.getPixel(x+1,y);
                      G6 = g.getPixel(x,y+1);
                      G9 = g.getPixel(x+1,y+1);
                      if(G1==0) G1=1;
                      if(G2==0) G2=1;
                      if(G3==0) G3=1;
                      if(G4==0) G4=1;
      
                      r.putPixel(x,y,(int)(R1));
                      r.putPixel(x+1,y,(int)((G5/2 * ((R1/G1) + (R2/G2) )) ));
                      r.putPixel(x,y+1,(int)(( G6/2 * ((R1/G1) + (R3/G3) )) ));
                      r.putPixel(x+1,y+1, (int)((G9/4 *  ((R1/G1) + (R3/G3) + (R2/G2) + (R4/G4)) ) ));        
                  }
              }
      
      
      
              if (row_order == 2) {
                  rgb.addSlice("red",b);  
                  rgb.addSlice("green",g);
                  rgb.addSlice("blue",r); 
              }
              else if (row_order == 3) {
                  rgb.addSlice("red",r);  
                  rgb.addSlice("green",g);
                  rgb.addSlice("blue",b);         
              }
          }
      
          return rgb;
      
      
      }
      

      基于PHYS 2070的脚本

      【讨论】:

        猜你喜欢
        • 2016-04-11
        • 2021-08-07
        • 2017-10-26
        • 2020-12-26
        • 1970-01-01
        • 2021-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多