方框模糊简化 CS50
Box Blur simplified CS50
我希望有人能帮助我。我很难解决 pset4 中的框模糊问题。我正在尝试简化这个概念,以便我能够真正理解它。我离开了位图,我只是想弄清楚如何获得二维数组中一个框的平均值。我当前的公式将求出整个盒子的平均值。
所以我希望能够查看 arx[0][0] 并获得其周围 3 个框加上 [0][0] 框的平均值。
所以它看起来像 1 + 1 + 1 + 3
平均值为 1.5(显示为 1)
然后看下一个盒子
arx[0][1]
1 + 1 + 1 + 1 + 3 + 3 = 10
10/6
2
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main(void)
{
int height = 4;
int width = 4;
int arx[4][4] = {{1 ,1, 1,3},
{ 1, 3, 3,1},
{ 3,3, 3,3}};
int counter = 0;
int average = 0;
int x[width][height];
int sum = 0;
for (int i = 0; i < height ; i++)
{
for (int j = 0; j < width; j++)
{
if (i >= 0 && i < height && j >=0 && j < width)
{
sum += arx[i][j];
counter++;
}
else
{
}
}
}
printf("%i TOTAL\n" , sum / counter);
printf("%i", counter);
}
对 (x,y) 处当前索引周围所有索引处的所有值求和:
(x-1,y-1) (x,y-1) (x+1,y-1)
(x-1,y) (x,y) (x+1,y)
(x-1,y+1) (x,y+1) (x+1,y+1)
将这9个值取平均得到(x,y)的新值。只要确保索引没有超出范围即可。
int main(void)
{
int height = 4;
int width = 4;
int arx[4][4] = {{1 ,1, 1, 3},
{1, 3, 3, 1},
{3, 3, 3, 3},
{2, 2, 2, 2}};
int counter = 0;
int average = 0;
int x[width][height];
int sum = 0;
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
counter = 0;
sum = 0;
// Sum surrounding values
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
int col_index = col + x;
int row_index = row + y;
// Ensure legal indexes
if (col_index >= 0 && col_index < width && row_index >= 0 && row_index < height) {
sum += arx[row_index][col_index];
counter++;
}
}
}
x[row][col] = sum / counter;
}
}
// x array now contains the blurred array
}
我希望有人能帮助我。我很难解决 pset4 中的框模糊问题。我正在尝试简化这个概念,以便我能够真正理解它。我离开了位图,我只是想弄清楚如何获得二维数组中一个框的平均值。我当前的公式将求出整个盒子的平均值。
所以我希望能够查看 arx[0][0] 并获得其周围 3 个框加上 [0][0] 框的平均值。
所以它看起来像 1 + 1 + 1 + 3 平均值为 1.5(显示为 1)
然后看下一个盒子 arx[0][1]
1 + 1 + 1 + 1 + 3 + 3 = 10 10/6 2
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main(void)
{
int height = 4;
int width = 4;
int arx[4][4] = {{1 ,1, 1,3},
{ 1, 3, 3,1},
{ 3,3, 3,3}};
int counter = 0;
int average = 0;
int x[width][height];
int sum = 0;
for (int i = 0; i < height ; i++)
{
for (int j = 0; j < width; j++)
{
if (i >= 0 && i < height && j >=0 && j < width)
{
sum += arx[i][j];
counter++;
}
else
{
}
}
}
printf("%i TOTAL\n" , sum / counter);
printf("%i", counter);
}
对 (x,y) 处当前索引周围所有索引处的所有值求和:
(x-1,y-1) (x,y-1) (x+1,y-1) (x-1,y) (x,y) (x+1,y) (x-1,y+1) (x,y+1) (x+1,y+1)
将这9个值取平均得到(x,y)的新值。只要确保索引没有超出范围即可。
int main(void)
{
int height = 4;
int width = 4;
int arx[4][4] = {{1 ,1, 1, 3},
{1, 3, 3, 1},
{3, 3, 3, 3},
{2, 2, 2, 2}};
int counter = 0;
int average = 0;
int x[width][height];
int sum = 0;
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
counter = 0;
sum = 0;
// Sum surrounding values
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
int col_index = col + x;
int row_index = row + y;
// Ensure legal indexes
if (col_index >= 0 && col_index < width && row_index >= 0 && row_index < height) {
sum += arx[row_index][col_index];
counter++;
}
}
}
x[row][col] = sum / counter;
}
}
// x array now contains the blurred array
}