2016-07-06 3 views
-2

画像をピクセルマトリクスに配置するコードがあります。この画像を4つの部分に分割し、これらの部分に対して4つの異なる画像ファイルを取得する必要があります。 次に、画像処理をしてから、それらの部分を1つの画像にまとめて結合する必要があります。 これを達成するのを手伝ってください。 注:画像は色がついていますので、4等分して1つに戻したいと思います。変更は必要ありません.4つの強度行列を得るコードはここにあります。しかし、私は何をすべきかわかりませんそれは必要ありません。ピクセルデータをJavaの画像に戻す

import java.awt.image.BufferedImage; 
import java.awt.image.DataBufferByte; 
import java.io.File; 
import java.io.IOException; 
import javax.imageio.ImageIO; 

    class Optimization 
{ 
    public static void main(String[] args) throws IOException 
    { 

     BufferedImage hugeImage = ImageIO.read(new File("comp.jpg")); 
     final byte[] pixels = ((DataBufferByte) hugeImage.getRaster().getDataBuffer()).getData(); 
    int width = hugeImage.getWidth(); 
    int height = hugeImage.getHeight(); 
     if(width%2!=0) 
      width=width-1; 
     if(height%2!=0) 
     height=height-1; 
     //System.out.print(width+" "+height); 
    int intensity[][]=new int[width][height]; 
    int b1[][]=new int[width/2][height/2]; 
    int b2[][]=new int[width/2][height/2]; 
    int b3[][]=new int[width/2][height/2]; 
    int b4[][]=new int[width/2][height/2]; 
    int x1=0,y1=0,x2=0,y2=0,x3=0,x4=0,y3=0,y4=0; 
     final int pixelLength = 3; 
     for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength) 
      { 
      int a1,a2,a3; 
      a3= ((int) pixels[pixel] & 0xff); // blue 
      a2= (((int) pixels[pixel + 1] & 0xff)); // green 
      a1= (((int) pixels[pixel + 2] & 0xff)); // red 
      int i=(a1+a2+a3)/3; 
      intensity[col][row]=i; 
      if((col<=width/2-1)&&(row<=height/2-1)) 
      { 
       b1[x1][y1]=i; 
       x1++; 
       if(col==width/2-1) 
       { 
        x1=0; 
        y1++; 
       } 
      } 
      if((col<width)&&(row<=height/2-1)&&(col>width/2-1)) 
      { 
       b2[x2][y2]=i; 
       x2++; 
       if(col==width-1) 
       { 
        x2=0; 
        y2++; 
       } 
      } 
      if((col<width/2)&&(row<height)&&(row>=height/2)) 
      { 
       b3[x3][y3]=i; 
       x3++; 
       if(col==width/2-1) 
       { 
        x3=0; 
        y3++; 
       } 
      } 
      if((col>width/2-1)&&(row>height/2-1)) 
      { 
       b4[x4][y4]=i; 
       x4++; 
       if(col==width-1) 
       { 
        x4=0; 
        y4++; 
       } 
      } 
      col++; 
      if (col == width) 
      { 
       col = 0; 
       row++; 

      } 

    } 
    for(int m=0;m<height/2;m++) 
    { 
     for(int n=0;n<width/2;n++) 
     { 
      System.out.print(b1[n][m]+" "); 
     } 
     System.out.println(); 
    } 



    } 
} 
+0

申し訳ありませんが、あなたがしようとしていることは明らかではありません。いくつかの例を示してください。 –

+0

イメージを4つの部分に分割したいだけです。それだけです。 – Srijan

+1

あなたのコードは機能しますか?それは失敗ですか?デバッガでそのステップを踏んで、期待どおりにどこからずれているかを確認しましたか?それが失敗すると、何が間違った行動ですか?例外はありますか?その場合は、スタックトレースを表示して、コード内で例外の発生場所を教えてください。あなたは私たちを助けなければなりません。 –

答えて

0

java.awt.ImageはあなたにgetSubimage(int x, int y, int w, int h)

を提供し、 "指定した矩形領域で定義されるサブイメージを返します。"あなたはちょうど4つの等しい地域を返し、それです。

+0

それは本当に良い方法です。しかし、処理後にサブ画像のそれらの部分を結合したいのですが? – Srijan

+0

@Srijanあなたはあなたの質問にそれを言います。 – eldo

0

getSubImage(int x, int y, int w, int h)を使用できますが、元の画像と同じラスタを共有する画像が表示されます。つまり、新しいサブ画像を変更すると元の画像も変更されます。問題がなければ、それを使用してください。

エルス

、すでにDataBufferの(良いアイデア)をアクセスしてきたように、ここにあなたが欲しいものを行うための簡単なコードである(私はただのDataBufferのコピーを作成するには、画像の大きさが奇数のとき、それは動作します):

BufferedImage image = ... ; 
BufferedImage q1 = new BufferedImage(image.getWidth()/2, image.getHeight()/2, image.getType()) ; 
BufferedImage q2 = new BufferedImage(image.getWidth()-image.getWidth()/2, image.getHeight()/2, image.getType()) ; 
BufferedImage q3 = new BufferedImage(image.getWidth()/2, image.getHeight()-image.getHeight()/2, image.getType()) ; 
BufferedImage q4 = new BufferedImage(image.getWidth()-image.getWidth()/2, image.getHeight()-image.getHeight()/2, image.getType()) ; 
byte[] bb = ((DataBufferByte)image.getRaster().getDataBuffer()).getData() ; 
byte[] bbq1 = ((DataBufferByte)q1.getRaster().getDataBuffer()).getData() ; 
byte[] bbq2 = ((DataBufferByte)q2.getRaster().getDataBuffer()).getData() ; 
byte[] bbq3 = ((DataBufferByte)q3.getRaster().getDataBuffer()).getData() ; 
byte[] bbq4 = ((DataBufferByte)q4.getRaster().getDataBuffer()).getData() ; 

for (int y=0 ; y < q1.getHeight() ; y++) // Fill Q1 and Q2 
    { 
    System.arraycopy(bb, y*image.getWidth(), bbq1, y*q1.getWidth(), q1.getWidth()) ; 
    System.arraycopy(bb, y*image.getWidth()+q1.getWidth(), bbq2, y*q2.getWidth(), q2.getWidth()) ; 
    } 

for (int y=0 ; y < q3.getHeight() ; y++) // Fill Q3 and Q4 
    { 
    System.arraycopy(bb, (y+q1.getHeight())*image.getWidth(), bbq3, y*q3.getWidth(), q3.getWidth()) ; 
    System.arraycopy(bb, (y+q1.getHeight())*image.getWidth()+q3.getWidth(), bbq4, y*q4.getWidth(), q4.getWidth()) ; 
    } 
+0

元のイメージに影響を与えない別々のバイト配列を作成するには、それが賢明です。 – Srijan

+0

イメージ処理をしてイメージのすべての部分を元に戻す方法を教えてください。 – Srijan

+0

あなたは私が共有したコードの正反対です。 4つの画像の合計に等しい大きな画像を作成した後、Q1/Q2が完全にコピーされるまで、Q1の1行/ 1行、続いて1行/ Q2行を追加します。 Q3/Q4を同じように追加します。 – FiReTiTi

関連する問題