2016-09-24 5 views
2

私はBufferedImageをjavaでフィルタリングしたいですか?フィルタリングrgbイメージは、Javaで一度だけ動作します

private void changeRGB(BufferedImage image, int colour) { 
    int width = image.getWidth(); 
    int height = image.getHeight(); 
    int newColor = 0; 

    for(int y = 0; y < height; y++){ 
     for(int x = 0; x < width; x++){ 
      Color color = new Color(image.getRGB(x,y)); 

      int red = color.getRed(); 
      int green = color.getGreen(); 
      int blue = color.getBlue(); 

      if (colour == 1) { 
       newColor = new Color(red, 0, 0).getRGB(); 
      } else if (colour == 2) { 
       newColor = new Color(0, green, 0).getRGB(); 
      } else if (colour == 3) { 
       newColor = new Color(0, 0, blue).getRGB(); 
      } 

      image.setRGB(x, y, newColor); 
     } 
    } 
    icon = new ImageIcon(image); 
    this.lblFilteredImage.setIcon(icon); 
} 

と私が押した場合、例えば:
現在、私はこのコードの部分を持っています

choice = 2; changeRGB(img, choice);

このコードは、時間を動作し、私はこれらのボタンのいずれかを押すと、2回目は、それが

+0

'color'と' colour'変数は同じスコープにありますか?これらの変数名の1つを間違いなく変更する必要があります...同じ画像に複数回適用すると、色を抽出するので、既に抽出した同じチャンネルを抽出するか、画像が黒くなるため、画像も変化しません以前は0に設定されていました... – fabian

+0

しかし、イメージが黒くならないように、どうすれば問題を解決できますか? – mafioso

+0

2つの画像を使用します。一方はソースを、もう一方はフィルタリングしたデータを使用します。異なるフィルタを適用することは、これらのフィルタでは意味がありません。だから私は一度にこれらのフィルタの1つだけを適用したいと思います。 – fabian

答えて

1

もう動作しません:、赤、緑、青のボタン私は、このようにそれを行いますBufferedImageに格納されている「ソース」イメージを変更しているようですが、新しいBufferedImageを使用して結果を保存し、ソースイメージを破棄しないようにします。

この方法で試すことができるので、「ソース」画像は変更されません。

private void changeRGB(BufferedImage image,int colour) 
{ 
    int width=image.getWidth(); 
    int height=image.getHeight(); 
    int newColor=0; 
    BufferedImage tempBuffer=new BufferedImage(image.getWidth(), image.getHeight(), image.getType()); 
    for(int y=0;y<height;y++) 
    { 
     for(int x=0;x<width;x++) 
     { 
      Color color=new Color(image.getRGB(x, y)); 

      int red=color.getRed(); 
      int green=color.getGreen(); 
      int blue=color.getBlue(); 

      if(colour==1) 
      { 
       newColor=new Color(red, 0, 0).getRGB(); 
      } 
      else if(colour==2) 
      { 
       newColor=new Color(0, green, 0).getRGB(); 
      } 
      else if(colour==3) 
      { 
       newColor=new Color(0, 0, blue).getRGB(); 
      } 

      tempBuffer.setRGB(x, y, newColor); 
     } 
    } 
    icon=new ImageIcon(tempBuffer); 
    this.lblFilteredImage.setIcon(icon); 
} 
0

私はアレックスが正しいと思います。 BufferedImageは変更可能ですので、​​は「オリジナル」BufferedImageを変更します。
CloneBufferedImageとクローンを処理します。

+0

フィードバックをいただければ幸いです。 – c0der

関連する問題