convert color image to grayscale

荒凉一梦 提交于 2019-12-23 03:30:35

问题


I want to convert a color image to grayscale.First I getting the data of color image and change this data but when I want to create a gary image from this data I have a error like this...

getData() maethod:

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() method:

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++;
           }
        }
}

回答1:


as it looks for me, the i and j in the setSample call must be swapped. (or w and h in BufferedImage)



来源:https://stackoverflow.com/questions/8114857/convert-color-image-to-grayscale

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!