Java 将双整数[]图像缩小为较小的大小

Java 将双整数[]图像缩小为较小的大小,java,image,image-processing,scaletransform,Java,Image,Image Processing,Scaletransform,因此,在我的代码中,我将图像表示为1和0的双int[][]数组。我希望能够将图像缩小为更小的int[][]数组。这是我试图做的一个例子: 0000000000 0000000000 00000 0000110000 00100 0000110000 => 00100 0000110000 01110 0000110000 00000 0011111100 00000 0000000000 0000000000 000

因此,在我的代码中,我将图像表示为1和0的双int[][]数组。我希望能够将图像缩小为更小的int[][]数组。这是我试图做的一个例子:

0000000000
0000000000       00000 
0000110000       00100   
0000110000   =>  00100
0000110000       01110
0000110000       00000
0011111100       00000
0000000000
0000000000
0000000000
有没有图书馆可以为我做这样的事?或者关于如何编写代码来为我做到这一点的任何想法。这将是我正在寻找的方法原型:

int[][] reduceImage(int[][] image, double scaleOfReduction) {
  // somehow make image reduced
  // how to implement this efficiently???
  return reducedImage;
}

下面是一个简单的代码片段,它可以实现您的目的

int[][] reduceImage(int[][] image, int scale) {

    int[][] reducedImage = new int[image.length/scale][image[0].length/scale];

    for (int i=0;i<reducedImage.length;i++) {
        for (int j=0;j<reducedImage[0].length;j++) {
            int total = 0;
            for (int x=0;x<scale;x++) {
                for (int y=0;y<scale;y++) {
                    total += image[i*scale+x][j*scale+y];
                }
            }
            reducedImage[i][j] = total>(scale*scale/2) ? 1 : 0;
        }
    }

    return reducedImage;
}
然后我们在新图像中迭代每个像素:

for (int i=0;i<reducedImage.length;i++) {
    for (int j=0;j<reducedImage[0].length;j++) {
return reducedImage;
最后,我们返回新图像:

for (int i=0;i<reducedImage.length;i++) {
    for (int j=0;j<reducedImage[0].length;j++) {
return reducedImage;

这可能不是收缩图像的最佳方法,但它非常简单且易于理解。

对于您的示例,scaleOfReduction的值是多少?不幸的是,如果原始图像正方形内的所有像素都是1,则结果中只有1。因此,对于大多数图像,这将给出大部分零。可能不是期望的结果。@DavidWallace我假设使用整数的真实图像的颜色深度不会为1:/I我错了吗?问题是我将图像表示为1和0的双整数[][]数组,现在好多了。请注意,行reducedImage[i][j]=total>scale*scale/2?1 : 0; 从您的解释来看,与大型代码块不太匹配。
return reducedImage;