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]
我该怎么做才能打印出新数组?
看起来您正在打印两次原始数组,而您只需要更改第二个即可打印出新数组,而将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();
}
旁边的事实是,您要像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;
}
}