661.图片平滑器

包含整数的二维矩阵 M 表示一个图片的灰度。你需要设计一个平滑器来让每一个单元的灰度成为平均灰度 (向下舍入) ,平均灰度的计算是周围的8个单元和它本身的值求平均,如果周围的单元格不足八个,则尽可能多的利用它们。

示例 1:

输入:
[[1,1,1],
 [1,0,1],
 [1,1,1]]
输出:
[[0, 0, 0],
 [0, 0, 0],
 [0, 0, 0]]
解释:
对于点 (0,0), (0,2), (2,0), (2,2): 平均(3/4) = 平均(0.75) = 0
对于点 (0,1), (1,0), (1,2), (2,1): 平均(5/6) = 平均(0.83333333) = 0
对于点 (1,1): 平均(8/9) = 平均(0.88888889) = 0

注意:

  1. 给定矩阵中的整数范围为 [0, 255]。
  2. 矩阵的长和宽的范围均为 [1, 150]。
class Solution {
    public int[][] imageSmoother(int[][] M) {
        if(M==null || M.length == 0 || M[0].length == 0){
            return M;
        }
        int[][] matrix = new int[M.length][M[0].length];
        for(int i=0;i<M.length;++i){
            for(int j=0;j<M[0].length;++j){
                matrix[i][j] = calValue(M,i,j);
            }
        }
        return matrix;
    }
    
    private int calValue(int[][] M, int i, int j){
        int sum = M[i][j];
        int count = 1;
        if(validateLoc(M, i+1, j)){
            sum+=M[i+1][j];
            ++count;
        }
        if(validateLoc(M, i-1, j)){
            sum+=M[i-1][j];
            ++count;
        }
        if(validateLoc(M, i+1, j+1)){
            sum+=M[i+1][j+1];
            ++count;
        }
        if(validateLoc(M, i+1, j-1)){
            sum+=M[i+1][j-1];
            ++count;
        }
        if(validateLoc(M, i-1, j+1)){
            sum+=M[i-1][j+1];
            ++count;
        }
        if(validateLoc(M, i-1, j-1)){
            sum+=M[i-1][j-1];
            ++count;
        }
        if(validateLoc(M, i, j-1)){
            sum+=M[i][j-1];
            ++count;
        }
        if(validateLoc(M, i, j+1)){
            sum+=M[i][j+1];
            ++count;
        }
        return sum/count;
    }
    
    private boolean validateLoc(int[][] M, int i,int j){
        if(i<0||i>=M.length||j<0||j>=M[0].length){
            return false;
        }
        return true;
    }
}
发布了106 篇原创文章 · 获赞 16 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_35356190/article/details/83478973
今日推荐