public class Tester2 {
public static void main(String[] args) {
int[][] image = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 0, 0},
{0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 0, 0},
{0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 0, 0},
{0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 0, 0},
{0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 0, 0},
{0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 0, 0},
{0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 0, 0},
{0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
// assume a rectangular image
int[][] smooth = new int[image.length][image[0].length];
int sum = 0;
int col = 0;
// Compute the smoothed value for non-edge locations in the image.
for (int row = 1; row < image.length - 1; row++) {
for (col = 1; col < image[row].length - 1; col++) {
sum = image[row - 1][col - 1] + image[row - 1][col] + image[row - 1][col + 1] +
image[row][col - 1] + image[row][col] + image[row][col + 1] +
image[row + 1][col - 1] + image[row + 1][col] + image[row + 1][col + 1];
}
smooth[row][col] = sum / 9;
}
// write out the input
for (int row = 0; row < image.length; row++) {
for (int col1 = 0; col1 < image[row].length; col1++) {
System.out.print(image[row][col1] + " ");
}
System.out.println();
}
for (int row = 0; row < image.length; row++) {
for (int col1 = 0; col1 < image[row].length; col1++) {
System.out.print(image[row][col1] + " ");
}
System.out.println();
}
}
}
我想打印新阵列。新数组已创建:
sum = image[row-1][col-1] + image[row-1][col] + image[row-1][col+1] +
image[row][col-1] + image[row][col] + image[row][col+1] +
image[row+1][col-1] + image[row+1][col] + image[row+1][col+1]
如何打印新阵列?
答案 0 :(得分:0)
您似乎已经两次打印原始数组,而只需更改第二个数据就可以打印出新数组,将image
更改为smooth
:
for ( int row=0; row < smooth.length; row++ )
{
for ( int col1=0; col1 < smooth[row].length; col1++ )
System.out.print( smooth[row][col1] + " ");
System.out.println();
}
答案 1 :(得分:0)
除了像Hew Wolff建议的那样打印图像数组两次之外,还应该更正算法以计算平滑数组的每个像素:
for (int row = 1; row < image.length - 1; row++) {
for (col = 1; col < image[row].length - 1; col++) {
sum = image[row - 1][col - 1] + image[row - 1][col] + image[row - 1][col + 1] +
image[row][col - 1] + image[row][col] + image[row][col + 1] +
image[row + 1][col - 1] + image[row + 1][col] + image[row + 1][col + 1];
smooth[row][col] = sum / 9;
}
}