Java 如何利用最近邻系统进行图像缩放
该方法以开头,并应返回大小为[img[0].length*factor\u h]*[img.length*factor\u w]Java 如何利用最近邻系统进行图像缩放,java,image,Java,Image,该方法以开头,并应返回大小为[img[0].length*factor\u h]*[img.length*factor\u w] public static int[][] scaleup(double factor_j,double factor_w,int [][]img)}//the method int[][] answer= new int[img.length][img[0].length]; for (int i = 0; i < img.length; i+
public static int[][] scaleup(double factor_j,double factor_w,int [][]img)}//the method
int[][] answer= new int[img.length][img[0].length];
for (int i = 0; i < img.length; i++) {
for (int j = 0; j < img[0].length; j++) {
for (int f_w = 0; f_w<=img[0].length; f_w++) {//f_w is smaller than the factor_w
for (int f_h = 0; f_h <= img.length; f_h++) {
answer[(int) (i * factor_h) + f_h][(int) (j * factor_w) + f_w] = img[j][i];
}
}
}
}
return answer;
}
}
公共静态int[]scaleup(双因子,双因子,int[]img)//
int[]answer=new int[img.length][img[0.length];
对于(int i=0;iint[][] answer= new int[img.length][img[0].length];
在这里,您说您的答案与旧img大小相同(让我们假设64x64作为示例)。因此,您现在有一个64x64数组。因此最后一个字段/像素是answer[63][63]
在这一行代码中:
answer[(int) (i * factor_h) + f_h][(int) (j * factor_w) + f_w] = img[j][i];
你说第一个索引是i*factor_h+f_h
变量i
从0变为63,变量f_h
从0变为64
我不认为你可以选择一个根本不会出现越界错误的因子。所以,是的,用正确的方法初始化你的答案数组,否则它将无法工作
对于第二个索引以及j
和factor_w
/f_w
public static int[][]scaleup(双因子h,双因子w,int[][]im){
public static int[][] scaleup(double factor_h, double factor_w, int[][] im) {
int[][] answer = new int[(int) (im.length * factor_h) + 1][(int) (im[0].length * factor_w) + 1];
for (int i = 0; i < im.length; i++) {
for (int j = 0; j < im[0].length; j++) {
for (int f_w = 0; f_w < factor_h; f_w++) {
for (int f_h = 0; f_h < factor_w; f_h++) {
answer[(int) (i * factor_h + f_w)][(int) (j * factor_w + f_h)] = im[i][j];
}
}
}
}
return answer;
}
int[][]答案=新的int[(int)(im.length*factor_h)+1][(int)(im[0]。length*factor_w)+1];
for(int i=0;i
请使用更多有意义的变量或更多的注释,使您的代码更清晰。谢谢,这是非常有用的公共静态int[]scaleup(双因子,双因子,int[]im){int[]answer=new int[(int)(im.lengthfactor_h)+1][(int)(im 0.lengthfactor_w)+1];for(int i=0;i