![](/img/trans.png)
[英]Convert color image to Black and White (not grayscale) depending on color conditions in java
[英]How to convert an image from color to black and white (grayscale) in Java
我只是想把這個彩色圖像轉換為黑白,但我不知道該怎么做。 我只知道如何獲得像素。 你能幫助我嗎?
private BufferedImage image;
private int[][]rgbValue;
public void setRgbValue(BufferedImage image){
int width = image.getWidth();
int height = image.getHeight();
rgbValue = new int[width*height][3];
int counter = 0;
for(int i=0 ; i<width ; i++){
for(int j=0 ; j<height ; j++){
int color = image.getRGB(i, j);
int red = (color & 0x00ff0000) >> 16;
int green = (color & 0x0000ff00) >> 8;
int blue = (color & 0x000000ff);
rgbValue[counter][0] = red;
rgbValue[counter][1] = green;
rgbValue[counter][2] = blue;
counter++;
}
}
}
如何將其與此代碼結合使用?
temp = red;
red = green;
green = blue;
blue = temp;
temp = 0;
rgb[i] = ((red << 16)) + ((green << 8 )) + (blue );
if(rgb[i] <= 0x670000){
rgb[i] = 0x000000;
} else {
rgb[i] = 0xffffff;
}
使用Java2D ColorConvertOp.filter(..)將BufferedImage的顏色轉換為指定的ColorSpace 。
BufferedImage bi = null; //Your BufferedImage goes here. null for compiler
ColorConvertOp op =
new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
op.filter(bi, bi);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.