将图像转换为二维数组,然后在 Java 中取回图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15002706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:09:47  来源:igfitidea点击:

Convert an image to a 2D array and then get the image back in Java

javaimage-processing

提问by Wayne Rooney

I want to convert an image to a 2D array of pixels, do some operations on it and then convert the resulting 2D array back to image. But I am always getting a pure black image as a result. I cannot figure out where I am going wrong. Here is what I am doing.All operations need to be done on a 8 bit grayscale image.

我想将图像转换为二维像素数组,对其进行一些操作,然后将生成的二维数组转换回图像。但结果我总是得到纯黑色的图像。我无法弄清楚我哪里出错了。这是我正在做的。所有操作都需要在 8 位灰度图像上完成。

  • Firstly I get the 2D array of pixels.

    public int[][] compute(File file)
    {
    try 
    {
        BufferedImage img= ImageIO.read(file);
        Raster raster=img.getData();
        int w=raster.getWidth(),h=raster.getHeight();
        int pixels[][]=new int[w][h];
        for (int x=0;x<w;x++)
        {
            for(int y=0;y<h;y++)
            {
                pixels[x][y]=raster.getSample(x,y,0);
            }
        }
    
        return pixels;
    
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    return null;
    }
    
  • Then I do some operations on the pixels array

  • Next I convert the array back to the image.

    public java.awt.Image getImage(int pixels[][])
    {
         int w=pixels.length;
         int h=pixels[0].length;
         BufferedImage image=new BufferedImage(w,h,BufferedImage.TYPE_BYTE_GRAY);
         WritableRaster raster=(WritableRaster)image.getData();
         for(int i=0;i<w;i++)
         {
             for(int j=0;j<h;j++)
             {
                 raster.setSample(i,j,0,pixels[i][j]);
             }
         }
    
    File output=new File("check.jpg");
    try {
        ImageIO.write(image,"jpg",output);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    return image;
    }
    
  • 首先我得到像素的二维数组。

    public int[][] compute(File file)
    {
    try 
    {
        BufferedImage img= ImageIO.read(file);
        Raster raster=img.getData();
        int w=raster.getWidth(),h=raster.getHeight();
        int pixels[][]=new int[w][h];
        for (int x=0;x<w;x++)
        {
            for(int y=0;y<h;y++)
            {
                pixels[x][y]=raster.getSample(x,y,0);
            }
        }
    
        return pixels;
    
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    return null;
    }
    
  • 然后我对像素数组做一些操作

  • 接下来我将数组转换回图像。

    public java.awt.Image getImage(int pixels[][])
    {
         int w=pixels.length;
         int h=pixels[0].length;
         BufferedImage image=new BufferedImage(w,h,BufferedImage.TYPE_BYTE_GRAY);
         WritableRaster raster=(WritableRaster)image.getData();
         for(int i=0;i<w;i++)
         {
             for(int j=0;j<h;j++)
             {
                 raster.setSample(i,j,0,pixels[i][j]);
             }
         }
    
    File output=new File("check.jpg");
    try {
        ImageIO.write(image,"jpg",output);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    return image;
    }
    

But I get a complete black image and I am sure that it is not complete black. What should I do to get the correct results?

但是我得到了一个全黑的图像,我确信它不是全黑的。我应该怎么做才能得到正确的结果?

EDIT- After applying efan's solution, when I save the image to a file, suppose the pixel value of (0,0) is 68 then on computing the pixel value from the same file it changes sometimes to 70 sometimes to 71.. There is small distortion to each pixel.But it spoils the image as a whole. Is there any fix ?

编辑- 应用 efan 的解决方案后,当我将图像保存到文件时,假设 (0,0) 的像素值为 68,然后在计算来自同一文件的像素值时,它有时会更改为 70,有时会更改为 71.. 有每个像素的失真很小。但它破坏了整个图像。有什么解决办法吗?

回答by efan

I think the reason image is completely black is that SampleModelfor Rasteris wrong. Here is what i did with your code:

我认为图像完全黑色的原因是SampleModel因为Raster是错误的。这是我对你的代码所做的:

private SampleModel sampleModel;

public int[][] compute(File file)
{
    ...
    sampleModel = raster.getSampleModel();
    ...
}

public java.awt.Image getImage(int pixels[][])
{
    ...
    WritableRaster raster= Raster.createWritableRaster(sampleModel, new Point(0,0));
    for(int i=0;i<w;i++)
    {
        for(int j=0;j<h;j++)
        {
            raster.setSample(i,j,0,pixels[i][j]);
        }
    }

    BufferedImage image=new BufferedImage(w,h,BufferedImage.TYPE_BYTE_GRAY);
    image.setData(raster);
    ...
}

And this worked for me fine. My understanding is that BufferedImage.TYPE_BYTE_GRAYdoes not select exactly what you need. Something different might work better, but i don't know how exactly those types correspond to color/sample model. And if you know which sample model you need you can just use it:

这对我来说很好。我的理解是这BufferedImage.TYPE_BYTE_GRAY并没有完全选择你需要的。不同的东西可能会更好,但我不知道这些类型与颜色/样本模型的对应关系如何。如果你知道你需要哪个样本模型,你可以使用它:

WritableRaster raster= Raster.createWritableRaster(new PixelInterleavedSampleModel(0, w, h, 1, 1920, new int[] {0}), new Point(0,0));