求解

B2108 图像模糊处理

``` #include <iostream> #include<cmath> using namespace std; double a[100][100]; int b[100][100]; int main() { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; b[i][j] = a[i][j]; } } for (int i = 1; i < (n - 1); i++) { for (int j = 1; j < (m - 1); j++) b[i][j] = round((a[i][j]+ a[i - 1][j] + a[i][j - 1] + a[i][j + 1] + a[i + 1][j]) / 5); } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) cout << b[i][j]<<" "; cout << endl; } return 0; } ```
by jiejieya @ 2024-02-21 20:18:19


@[SHHD](/user/1236137) 是其上下左右及其原点灰度值的平均,所以 ```cpp a[i][j]=(a[i-1][j]+a[i][j-1]+a[i][j+1]+a[i+1][j])/4; ``` 改成 ```cpp a[i][j]=(a[i-1][j]+a[i][j-1]+a[i][j+1]+a[i+1][j]+a[i][j])/5; ```
by xiangzhenze611 @ 2024-02-21 20:19:05


@[SHHD](/user/1236137) 另外,由于要取整,还要加个 `round` 函数
by xiangzhenze611 @ 2024-02-21 20:23:09


几个错的地方 1. ``` for (int i=1;i<(n-1);i++) { for (int j=1;j<(m-1);j++) a[i][j]=(a[i-1][j]+a[i][j-1]+a[i][j+1]+a[i+1][j])/4; } ``` 这边a[i][j]应该是其自己本身加邻边的数值的和再除5 2.还是同一位置比如一行a[i][1]你先修改了他的值,在接下来计算a[i][2]的值会受到影响,所以不如从新开一个数组来记录模糊过后的数值,这样就不会因为前面的修改而影响后面的值 3.题目要求取最近的整数,原本int类型无法做到,所以改用double类型,再用round()函数进行四舍五入的操作
by jiejieya @ 2024-02-21 20:23:26


还有输出要空格!!!
by jiejieya @ 2024-02-21 20:25:24


@[xiangzhenze611](/user/1005749) 谢谢啦
by SHHD @ 2024-02-21 20:28:01


@[SHHD](/user/1236137) 不用谢
by xiangzhenze611 @ 2024-02-21 20:32:02


```cpp // #include <bits/stdc++.h> using namespace std; int a[100][100],b[100][100]; int main() { int n,m; cin>>n>>m; for (int i=0;i<n;i++) { for (int j=0;i<m;j++) cin>>a[i][j]; } for (int i=1;i<(n-1);i++) { for (int j=1;j<(m-1);j++) round(b[i][j]=(a[i-1][j]+a[i][j-1]+a[i][j+1]+a[i+1][j]+a[i][j])/5); } for (int i=0;i<n;i++) { for (int j=0;j<m;j++) cout<<b[i][j]<<" "; cout<<endl; } return 0; } ``` 超时了
by SHHD @ 2024-02-21 20:33:09


@[xiangzhenze611](/user/1005749) 再帮我看一下呗
by SHHD @ 2024-02-21 20:36:56


@[SHHD](/user/1236137) `round` 函数要用 `double`
by xiangzhenze611 @ 2024-02-21 20:46:01


| 下一页