用double数组模糊java以上的图片表示像素集

Blurring a picture over java with a double array represents the set of pixels

Write a function called "smooth", which blurs the picture - a double array represents the group of pixels of the picture. Smoothing a picture for a parameter n, is taking all pixel in the picture to be the average of nn neighbors. Meaning that is looking at a nn square which the current pixel is his center, and replace the pixel with the average of the neighbors.


我的尝试: 所以我写了以下内容:

public void smooth(int n) {
    int N = (n-1)/2;
    for (int x = N; x<frame.length-N;x++){
        for (int y=N;y<frame[x].length-N;y++){
            frame[x][y]=average(x,y,N,this.frame);
        }
    }
    //for edges:
    for (int x = 0; x<frame.length;x++) {
        for (int y = 0; y < frame[0].length; y++) {
            if (FilterOutOfBounds(x,y,this.frame,N)!=-1) frame[x][y] = FilterOutOfBounds(x, y, this.frame, N);
        }
    }

}
public static int average(int x, int y, int N,int[][] frame){
    int sum = 0, cnt=0;
    for (int i = x-N;i<x+N;i++){
        for (int j = y-N;j<y+N;j++){
            sum+=frame[i][j]; cnt++;
        }
    }
    return sum/cnt;
}

public static int FilterOutOfBounds(int x, int y, int[][] frame, int N){
    int cnt = 0,sum=0;
    if (frame[0].length-1-y<N && x!=frame.length-1){
        for (int j = y;j>=y-1;j--){
            for (int i = x;i<=x+1;i++){
                cnt++;
                sum+=frame[i][j];
            }
        }
    }
    else if (y<N && x!=frame.length-1 ){
        for (int j = y;j<=y+1;j++){
            for (int i = x;i<=x+1;i++){
                cnt++;
                sum+=frame[i][j];
            }
        }
    }

    else if(frame.length-x<N && y!=0){
        for (int i = x;i>=x-1;i--){
            for (int j = y;j>=y-1;j--){
                cnt++;
                sum+=frame[i][j];
            }
        }

    }

    else if (x<N && y!=frame.length-1){
        for (int i = x;i<=x+1;i++){
            for (int j = y;j<=y+1;j++){
                cnt++;
                sum+=frame[i][j];
            }
        }
    }
    if (cnt==0) return -1;
    return sum/cnt;
}

但是,由于某种原因,它并没有模糊边缘,我想不出为什么。因此,我很乐意提供一些帮助。谢谢!

我认为您的代码存在一些问题。首先,您直接更新 frame 变量,这将为您的计算累积平均值。我相信这也是一种不需要的副作用。 另一件事是,FilterOutOfBounds 方法使用 frame.length 而不是 y,它应该是 frame[0].length,因为它指的是 y 的矩形。此外,这些内部计算不处理 N,因此 for 循环中的 x+1y+1 可能是误解,它们应该是 x+Ny+N 之类的相似。

我已经为您的问题创建了一个更简单的解决方案。基本上我已经修改了 avarage 方法,以便能够处理边缘情况并让 smooth for 循环遍历 frame 的每个单元格。为我工作:

public void smooth( int n ) {
    int N = (n - 1) / 2;
    int[][] blurred = new int[frame.length][frame[0].length];
    for ( int x = 0; x < frame.length; x++ ) {
        for ( int y = 0; y < frame[x].length; y++ ) {
            blurred[x][y] = average(x, y, N, frame);
        }
    }
    frame = blurred;
}

public static int average( int x, int y, int N, int[][] frame ) {
    int sum = 0, cnt = 0;
    for ( int i = Math.max(x - N, 0); i <= Math.min(x + N, frame.length - 1); i++ ) {
        for ( int j = Math.max(y - N, 0); j <= Math.min(y + N, frame[i].length - 1); j++ ) {
            sum += frame[i][j];
            cnt++;
        }
    }
    return sum / cnt;
}