给定 nn 行 mm 列的图像各像素点的灰度值,要求用如下方法对其进行模糊化处理:
-
四周最外侧的像素点灰度值不变;
-
中间各像素点新灰度值为该像素点及其上下左右相邻四个像素点原灰度值的平均(舍入到最接近的整数)。
输入格式
第一行包含两个整数 nn 和 mm,表示图像包含像素点的行数和列数。1 \le n \le 1001≤n≤100,1 \le m \le 1001≤m≤100。
接下来 nn 行,每行 mm 个整数,表示图像的每个像素点灰度。相邻两个整数之间用单个空格隔开,每个元素均在 00 ~ 255255 之间。
输出格式
nn 行,每行 mm 个整数,为模糊处理后的图像。相邻两个整数之间用单个空格隔开。
输出时每行末尾的多余空格,不影响答案正确性
样例输入
4 5
100 0 100 0 50
50 100 200 0 0
50 50 100 100 200
100 100 50 50 100
样例输出
100 0 100 0 50
50 80 100 60 0
50 80 100 90 200
100 100 50 50 100
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
int a[100][100],b[100][100];
int n,m;
bool check(int ans){
if((double)ans/5.0-ans/5>=0.5) return false;
else return true;
}
void solve(){
int ans=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++){
if(i==0||j==0||i==n-1||j==m-1)
b[i][j]=a[i][j];
else{
ans=a[i][j]+a[i-1][j]+a[i][j-1]+a[i+1][j]+a[i][j+1];
if(check(ans)) b[i][j]=ans/5;
else b[i][j]=ans/5+1;
}
}
for(int i=0;i<n;i++)
for(int j=0;j<m;j++){
cout<<b[i][j]<<" ";
if(j==m-1) cout<<endl;
}
}
int main(){
while(cin>>n>>m){
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
cin>>a[i][j];
solve();
}
return 0;
}