我想将彩色图像转换为灰度图像。首先,我获取彩色图像的数据并更改此数据,但是当我想从这些数据创建加里图像时,我遇到了这样的错误...
getData() 方法:
public double[] getData(BufferedImage a){
double[] data2=new double[h*w];
int red=0,green=0,blue=0;
int counter=0;
for(int w1=0;w1<w;w1++){
for(int h1=0;h1<h;h1++){
int rgb=a.getRGB(w1,h1);
red=(rgb)>> 16;
green=(rgb)>>8;
blue=(rgb);
data2[counter]=(red+green+blue)/3;
counter++;
}
}
return data2;
}
createimage() 方法:
void createImage(){
double[] data1=getData(colorImage);
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
WritableRaster raster = image.getRaster();
int counter = 0 ;
for(int i = 0; i<h; i++){
for(int j = 0; j<w; j++){
raster.setSample(i, j, 0, data1[counter]);
counter++;
}
}
}
最佳答案
正如我所见,setSample 调用中的 i 和 j 必须交换。 (或 BufferedImage 中的 w 和 h)
关于java - 将彩色图像转换为灰度图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8114857/