博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Image Smoother
阅读量:4625 次
发布时间:2019-06-09

本文共 1814 字,大约阅读时间需要 6 分钟。

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) = 0For the point (0,1), (1,0), (1,2), (2,1): floor(5/6) = floor(0.83333333) = 0For the point (1,1): floor(8/9) = floor(0.88888889) = 0

 

Note:

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

 

1 class Solution { 2     public int[][] imageSmoother(int[][] M) { 3         int m = M.length, n = M[0].length; 4         int result[][] = new int[m][n]; 5          6         for (int i = 0; i < m; i++) { 7             for (int j = 0; j < n; j++) { 8                 int[] surroundings = countSurroundings(M, i, j); 9                 result[i][j] = surroundings[0] / surroundings[1];10             }11         }12         13         return result;14     }15     16     private int[] countSurroundings(int[][] M, int i, int j) {17         int result[] = {0, 0}; // surroundingCount, surroundingNumber18         for (int ii = i - 1; ii <= i + 1; ii++) {19             for (int jj = j - 1; jj <= j + 1; jj++) {20                 if (ii < 0 || jj < 0 || ii >= M.length || jj >= M[0].length) {21                     continue;22                 } else {23                     result[1] += 1;24                     result[0] += M[ii][jj];25                 }26             }27         }28         29         return result;30     }31 }

 

转载于:https://www.cnblogs.com/amazingzoe/p/9076058.html

你可能感兴趣的文章
YYHSOI模拟赛题解(T5卡片游戏)
查看>>
好程序员web前端技术分享移动端页面布局
查看>>
进度条的使用 Progress控件
查看>>
Java - 网络与多线程 - 多线程之内存可见性
查看>>
【自动化__持续集成】___java___水仙花
查看>>
实验任务一
查看>>
【转】【金蝶K3Cloud】 在插件中调用工作流
查看>>
数据库实验课笔记20190509
查看>>
HDU1131_卡特兰数算二叉树个数
查看>>
TortoiseSVN安装攻略和使用教程(详细)
查看>>
git 提示 Please move or remove them before you can merge 解决办法
查看>>
oracle Redhat64 安装
查看>>
UOJ #271 炸铁路
查看>>
T-SQL自定义函数ConvertSecondsToTime
查看>>
获取数据库中所有触发器
查看>>
存储过程接收JSON格式数据
查看>>
Linux下C编程入门(4)
查看>>
标准C程序设计七---42
查看>>
Git学习笔记
查看>>
Vim查找替换及正则表达式的使用
查看>>