Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将图像转换为二维数组,然后在处理后从中获取图像_Java_Image - Fatal编程技术网

Java 将图像转换为二维数组,然后在处理后从中获取图像

Java 将图像转换为二维数组,然后在处理后从中获取图像,java,image,Java,Image,我正在学习图像处理技术,并且有一些家庭作业。 在我的作业中,其中要求我覆盖一个RBG到灰色的图像。 我已经将图像转换为2D矩阵,做了一些事情,当我再次从2D矩阵转换到图像时,会发生一些错误。 这是我的代码: private static SampleModel samM; public static int[][] imageToArrayPixel(File file) { try { BufferedImage img = ImageIO.read(file);

我正在学习图像处理技术,并且有一些家庭作业。 在我的作业中,其中要求我覆盖一个RBG到灰色的图像。 我已经将图像转换为2D矩阵,做了一些事情,当我再次从2D矩阵转换到图像时,会发生一些错误。 这是我的代码:

private static SampleModel samM;
public static int[][] imageToArrayPixel(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);
                System.out.print("  " + pixels[x][y]);
            }
            System.out.println("");
        }

        samM = raster.getSampleModel();

        return pixels;

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

public static java.awt.Image getImage(int pixels[][]) {
    int w = pixels.length;
    int h = pixels[0].length;

    WritableRaster raster = Raster.createWritableRaster(samM, new Point(0, 0));


    for (int i = 0; i < w; i++) {
        for (int j = 0; j < pixels[i].length; j++) {
            if (pixels[i][j] > 128) {
                raster.setSample(i, j, 1, 255);
            } else {
                raster.setSample(i, j, 1, 0);
            }
        }
    }

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

    File output = new File("check.jpg");
    try {
        ImageIO.write(image, "jpg", output);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return image;
}

public static java.awt.Image getImageWithRBG(Pixel pixels[][]) {
    int w = pixels.length;
    int h = pixels[0].length;

    WritableRaster raster = Raster.createWritableRaster(samM, new Point(0, 0));
    int[] pixelValue = new int[3];

    for (int i = 0; i < w; i++) {
        for (int j = 0; j < h; j++) {
            pixelValue[0] = pixels[i][j].red;
            pixelValue[1] = pixels[i][j].blue;
            pixelValue[2] = pixels[i][j].green;
            raster.setPixel(j, i, pixelValue);
        }
    }

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

    File output = new File("check.jpg");
    try {
        ImageIO.write(image, "jpg", output);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return image;
}

public static void main(String[] args) throws IOException {
    int pixel[][] = imageToArrayPixel(new File("C:\\Users\\KimEricko\\Pictures\\1402373904964_500.jpg"));

    getImage(pixel);

}
私有静态SampleModel-samM;
公共静态int[][]图像到arraypixel(文件){
试一试{
BuffereImage img=ImageIO.read(文件);
光栅=img.getData();
int w=graster.getWidth(),h=graster.getHeight();
整数像素[][]=新整数[w][h];
对于(int x=0;x128){
光栅设置样本(i,j,1255);
}否则{
光栅设置样本(i,j,1,0);
}
}
}
BuffereImage=新的BuffereImage(w,h,BuffereImage.TYPE_BYTE_GRAY);
image.setData(光栅);
文件输出=新文件(“check.jpg”);
试一试{
写入(图像,“jpg”,输出);
}捕获(例外e){
e、 printStackTrace();
}
返回图像;
}
public static java.awt.Image getImageWithRBG(像素[][]){
int w=像素。长度;
int h=像素[0]。长度;
WritableRaster raster=光栅。createWritableRaster(samM,新点(0,0));
int[]像素值=新的int[3];
对于(int i=0;i
这是我用来掩饰的形象: 这是我在修复后收到的照片:

我不明白为什么复原后的照片只有原始照片的1/3。
我能做些什么来修复这个问题?

在我看来,getImageWithRBG中有一个bug,就是 光栅设置像素(j,i,像素值); 应该是 光栅设置像素(i,j,像素值)

setPixel和setSample具有相似的输入:x和y


我不知道是否还有其他问题,这只是我注意到的第一件事。

谢谢您的评论!