The Image Smoother Algorithm in C++/Java

  • 时间:2020-10-05 13:15:44
  • 分类:网络文摘
  • 阅读:110 次

Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother to make the gray scale of each cell becomes the average gray scale (rounding down) of all the 8 surrounding cells and itself. If a cell has less than 8 surrounding cells, then use as many as you can.

Example 1:
Input:
[[1,1,1],
[1,0,1],
[1,1,1]]

Output:
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]

Explanation:
For the point (0,0), (0,2), (2,0), (2,2): floor(3/4) = floor(0.75) = 0
For the point (0,1), (1,0), (1,2), (2,1): floor(5/6) = floor(0.83333333) = 0
For the point (1,1): floor(8/9) = floor(0.88888889) = 0

Note:
The value in the given matrix is in the range of [0, 255].
The length and width of the given matrix are in the range of [1, 150].

How to Smooth Image in C++?

The following C++ code implements O(N) algorithm (where N is the number of pixels in the image) that iterates each pixel. We can’t modify the existing image, rather, it has to be done on a separate copy of image.

We can deep copy the std::vector, or just assign new pixel to it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
    vector<vector<int>> imageSmoother(vector<vector<int>>& M) {
        int row = M.size();
        if (row == 0) return M;
        int width = M[0].size();
        if (width == 0) return M;
        vector<vector<int>> N(row, vector<int>(width)); // or N = M deep copy of vector
        for (int i = 0; i < row; ++ i) {
            for (int j = 0; j < width; ++ j) {
                int sum = 0, c = 0;
                for (int k = max(0, i - 1); k <= min(i + 1, row - 1); k ++) {
                    for (int u = max(0, j - 1); u <= min(j + 1, width - 1); u ++) {
                        sum += M[k][u];
                        c ++;
                    }
                }
                N[i][j] = sum / c;
            }
        }
        return N;
    }
};
class Solution {
public:
    vector<vector<int>> imageSmoother(vector<vector<int>>& M) {
        int row = M.size();
        if (row == 0) return M;
        int width = M[0].size();
        if (width == 0) return M;
        vector<vector<int>> N(row, vector<int>(width)); // or N = M deep copy of vector
        for (int i = 0; i < row; ++ i) {
            for (int j = 0; j < width; ++ j) {
                int sum = 0, c = 0;
                for (int k = max(0, i - 1); k <= min(i + 1, row - 1); k ++) {
                    for (int u = max(0, j - 1); u <= min(j + 1, width - 1); u ++) {
                        sum += M[k][u];
                        c ++;
                    }
                }
                N[i][j] = sum / c;
            }
        }
        return N;
    }
};

We can check if the neighbour index of pixels are valid. Alternatively, we can use min/max to make sure the indices are always valid. We don’t need to use floor function as the integer division is floor anyway.

How to Smooth Image in Java?

The same image smooth algorithm can be implemented in Java as follows.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
    public int[][] imageSmoother(int[][] M) {
        int row = M.length;
        if (row == 0) return M;
        int width = M[0].length;
        if (width == 0) return M;
        int[][] N = new int[row][width];
        for (int i = 0; i < row; ++ i) {
            for (int j = 0; j < width; ++ j) {
                int sum = 0, c = 0;
                for (int k = Math.max(0, i - 1); k <= Math.min(i + 1, row - 1); k ++) {
                    for (int u = Math.max(0, j - 1); u <= Math.min(j + 1, width - 1); u ++) {
                        sum += M[k][u];
                        c ++;
                    }
                }
                N[i][j] = sum / c;
            }
        }
        return N;        
    }
}
class Solution {
    public int[][] imageSmoother(int[][] M) {
        int row = M.length;
        if (row == 0) return M;
        int width = M[0].length;
        if (width == 0) return M;
        int[][] N = new int[row][width];
        for (int i = 0; i < row; ++ i) {
            for (int j = 0; j < width; ++ j) {
                int sum = 0, c = 0;
                for (int k = Math.max(0, i - 1); k <= Math.min(i + 1, row - 1); k ++) {
                    for (int u = Math.max(0, j - 1); u <= Math.min(j + 1, width - 1); u ++) {
                        sum += M[k][u];
                        c ++;
                    }
                }
                N[i][j] = sum / c;
            }
        }
        return N;        
    }
}

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
数学题:有一个底面周长为9.42厘米的圆柱体,斜着截去一段  百度算法经常更新要怎么解决?  讲一讲我这10年的站长经历  为什么企业网站不要用模板建站 模板建站有哪些弊端  网站安全渗透测试难度系数有多大  什么内容才是被百度肯定的优质内容?优质内容应该这样做!  seo-网站权重怎么提高?  万词霸屏与SEO优化合二为一才能给企业带来真实效益  熟知百度蜘蛛原理,按照优化规则才能做好seo优化  SEO关键词排名优化原理是什么? 
评论列表
添加评论