2016-10-30 2 views
-1

私は画像処理技術を学び、いくつかの宿題をしています。 私の宿題では、グレーのイメージにRBGをカバーするように頼んだ。 イメージを2Dマトリックスに変換しました。何かやっていますが、2Dマトリックスからイメージまでカバーすると、何らかの問題が起こります。 この私のコード: before と、ここで私は、復元後に受信した写真である:私はひそかに使用イメージを2D配列に変換し、プロセスの後にgetImageを返します。

private static SampleModel samM; 
public static int[][] imageToArrayPixel(File file) { 
    try { 
     BufferedImage img = ImageIO.read(file); 
     Raster raster = img.getData(); 
     int w = raster.getWidth(), h = raster.getHeight(); 
     int pixels[][] = new int[w][h]; 
     for (int x = 0; x < w; x++) { 
      for (int y = 0; y < h; y++) { 
       pixels[x][y] = raster.getSample(x, y, 0); 
       System.out.print(" " + pixels[x][y]); 
      } 
      System.out.println(""); 
     } 

     samM = raster.getSampleModel(); 

     return pixels; 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

public static java.awt.Image getImage(int pixels[][]) { 
    int w = pixels.length; 
    int h = pixels[0].length; 

    WritableRaster raster = Raster.createWritableRaster(samM, new Point(0, 0)); 


    for (int i = 0; i < w; i++) { 
     for (int j = 0; j < pixels[i].length; j++) { 
      if (pixels[i][j] > 128) { 
       raster.setSample(i, j, 1, 255); 
      } else { 
       raster.setSample(i, j, 1, 0); 
      } 
     } 
    } 

    BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY); 
    image.setData(raster); 

    File output = new File("check.jpg"); 
    try { 
     ImageIO.write(image, "jpg", output); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return image; 
} 

public static java.awt.Image getImageWithRBG(Pixel pixels[][]) { 
    int w = pixels.length; 
    int h = pixels[0].length; 

    WritableRaster raster = Raster.createWritableRaster(samM, new Point(0, 0)); 
    int[] pixelValue = new int[3]; 

    for (int i = 0; i < w; i++) { 
     for (int j = 0; j < h; j++) { 
      pixelValue[0] = pixels[i][j].red; 
      pixelValue[1] = pixels[i][j].blue; 
      pixelValue[2] = pixels[i][j].green; 
      raster.setPixel(j, i, pixelValue); 
     } 
    } 

    BufferedImage image = new BufferedImage(h, w, BufferedImage.TYPE_CUSTOM); 
    image.setData(raster); 

    File output = new File("check.jpg"); 
    try { 
     ImageIO.write(image, "jpg", output); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return image; 
} 

public static void main(String[] args) throws IOException { 
    int pixel[][] = imageToArrayPixel(new File("C:\\Users\\KimEricko\\Pictures\\1402373904964_500.jpg")); 

    getImage(pixel); 

} 

この私のイメージ after

なぜ絵の後、私は理解していません復元には元の写真の1/3しか含まれていません。 これを解決するにはどうすればよいですか?

答えて

0

getImageWithRBGにバグがあるように私に見える、その raster.setPixel(j、i、pixelValue); は である必要があります。raster.setPixel(i、j、pixelValue);

とsetPixelとsetSampleは、同様の入力を持っているが:他の問題がある場合、私は知らないのy

、それは私が気づいただけで最初のものですその後、xは。

+0

ありがとうございました! –

関連する問題