2016-08-02 176 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没有工作,但另一件事。非常感谢。 –