2016-08-02 188 views
1

我正在嘗試更改圖像的顏色。因此,我使用以下代碼更改java中的圖像顏色

public class Picture { 

String img_name; 
BufferedImage buf_img; 
int width; 
int height; 

public Picture(String name) { 
    this.img_name = name; 

    try { 
     buf_img = ImageIO.read(new File(img_name)); 
    } catch (IOException ex) { 
     Logger.getLogger(Picture.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 

public Picture(int w, int h) { 
    this.width = w; 
    this.height = h; 
    buf_img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); 
} 

public int width() { 
    width = buf_img.getWidth(); 
    return width; 
} 

public int height() { 
    height = buf_img.getHeight(); 
    return height; 
} 

public Color get(int col, int row) { 
    Color color = new Color(buf_img.getRGB(col, row)); 
    return color; 
} 

public void set(int col, int row, Color color) { 
    buf_img.setRGB(col, row, color.getRGB()); 
} 

public void show() { 
    try { 

     File saveAs = new File("D:\\temp\\" + new Random().nextInt() + ".png"); 
     ImageIO.write(buf_img, "png", saveAs); 

     Desktop.getDesktop().open(saveAs); 
    } catch (IOException ex) { 
     Logger.getLogger(Picture.class.getName()).log(Level.SEVERE, null, ex); 
    } 
    } 

} 

public class ColorSeparation { 

public static void main(String[] args) { 

    // read in the picture specified by command-line argument 
    Picture picture = new Picture("D:\\qwe1.jpg"); 
    int width = picture.width(); 
    int height = picture.height(); 

    // create three empy pictures of the same dimension 
    Picture pictureR = new Picture(width, height); 


    // separate colors 
    for (int col = 0; col < width; col++) { 
     for (int row = 0; row < height; row++) { 
      Color color = picture.get(col, row); 
      int r = color.getRed(); 
      int g = color.getGreen(); 
      int b = color.getBlue(); 
      pictureR.set(col, row, new Color(r, 0, 0)); 

     } 
    } 

    // display picture in its own window 
    pictureR.show(); 

    } 

} 

它按預期工作。 input Image

Output image

現在我想設置整個圖像的顏色爲rgb 255,153,51。我試圖設置 pictureR.set(col, row, new Color(255, 153, 51))。但結果輸出如下圖resulting image

我怎樣才能得到正確的圖像?請幫忙。

+4

這看起來很像,你必須「設置整個圖像的顏色,255,153,51 RGB」我的圖像。 –

+0

發佈'圖片'類。更好的是,發佈一個[mcve] – Reimeus

+0

@Reimeus我現在已經更新了這個問題 –

回答

3

你最初的例子是誤導你。第一個例子中的代碼是設置不同的紅色陰影(從原始紅色通道拉出),創建一個「紅色」圖像,而不是像您想象的那樣「着色」圖像。

int r = color.getRed(); 
pictureR.set(col, row, new Color(r, 0, 0)); 

你的第二個例子是設置爲每一個像素一個固定的顏色,所以你得到一個統一的橙色。

pictureR.set(col, row, new Color(255, 153, 51)) 

您需要通過改變所有三個通道使圖像着色,就像您最初改變紅色值一樣。有關使用合成的示例,請參閱this question,這與您現在使用的角度不同。

根據您的示例代碼,最簡單的實現方式是計算每個像素的relative luminence(實際上它是灰度值),並使用它來調整您設置的「橙色」值。對於luminence標準的權重是

L = 0.2126*R + 0.7152*G + 0.0722*B 

所以像

pictureR.set(col, row, new Color(255 * L/255, 153 * L/255, 51 * L/255)); 
+0

顏色座標超出了線程「main」中Exception的限制java.lang.IllegalArgumentException:預期範圍外的顏色參數:Red Green藍色 –

+0

@swapnilgandhi正確,L在0-255的範圍內,因此將其與像素值混合超過RGB值範圍(也是0-255)。我已經更新了我的答案,在設置顏色通道的同時將L轉換爲百分比(除以255)。您可能希望在解決方案中使用不均勻的權重(如「L」公式中的權重),具體取決於您想要的結果。 – brichins

+0

Luminence沒有工作,但另一件事。非常感謝。 –