gpt4 book ai didi

c++ - 尝试 "blur"C++ 中的矩阵;有缺陷的算法或代码?

转载 作者:行者123 更新时间:2023-11-30 04:49:25 25 4
gpt4 key购买 nike

问题

我正在尝试使用 koef(见下文)“模糊”矩阵(就像图像一样),但它没有按照我想要的方式工作。我通过 .txt 文件提供矩阵。

什么是 koef?

This site很好地解释了它是如何工作的。对于这个模糊,我使用了 koef 为 14 的“模糊光”,它是以下 3x3 矩阵(代表模糊密度)的总和:

1 2 1
2 2 2
1 2 1

可能有一种更简单的方法可以做到这一点,而不是将同样的事情硬编码九次。我还有手绘图来表示循环/条件结构以及它们实际在做什么(如果有用的话)。

void blur(int pic[][MAX_COL]/*OUT*/, int height, int width)
{
int newPic[MAX_ROW][MAX_COL] = {0};

for (int h = 0; h < height; h++)
{
for (int w = 0; w < width; w++)
{
if (h == 0 && w == 0)
newPic[h][w] = (2 * pic[h][w] + 2 * pic[h][w-1] + 2 * pic[h+1][w] + 1 * pic[h+1][w-1])
/(pic[h][w] + pic[h][w-1] + pic[h+1][w] + pic[h+1][w-1]);
else if (h == height - 1 && w == width - 1)
newPic[h][w] = (2 * pic[h][w] + 2 * pic[h-1][w] + 2 * pic[h][w-1] + 1 * pic[h-1][w-1])
/(pic[h][w] + pic[h-1][w] + pic[h][w-1] + pic[h-1][w-1]);
else if (h == height - 1 && w == 0)
newPic[h][w] = (2 * pic[h][w] + 2 * pic[h-1][w] + 2 * pic[h][w+1] + 1 * pic[h-1][w+1])
/(pic[h][w] + pic[h-1][w] + pic[h][w+1] + pic[h-1][w+1]);
else if (h == 0 && w == width - 1)
newPic[h][w] = (2 * pic[h][w] + 2 * pic[h][w-1] + 2 * pic[h+1][w] + 1 * pic[h+1][w-1])
/(pic[h][w] + pic[1][w-1] + pic[h+1][w] + pic[h+1][w-1]);
else if (h == 0 && (w != 0 && w != width - 1))
newPic[h][w] = (2 * pic[h][w] + 2 * pic[h+1][w] + 2 * pic[h-1][w] + 2 * pic[h][w+1]
+ 1 * pic[h+1][w+1] + 1 * pic[h-1][w+1])/(pic[h][w] + pic[h+1][w] + pic[h-1][w]
+ pic[h][w+1] + pic[h+1][w+1] + pic[h-1][w+1]);
else if (w == 0 && (h != 0 && h != height - 1))
newPic[h][w] = (2 * pic[h][w] + 2 * pic[h+1][w] + 2 * pic[h][w+1] + 2 * pic[h][w-1] + 1 * pic[h+1][w+1]
+ 1 * pic[h+1][w-1])/(pic[h][w] + pic[h+1][w] + pic[h][w+1] + pic[h][w-1]
+ pic[h+1][w+1] + pic[h+1][w-1]);
else if (h == height - 1 && (w != 0 && w != width - 1))
newPic[h][w] = (2 * pic[h][w] + 2 * pic[h+1][w] + 2 * pic[h-1][w] + 2 * pic[h][w-1] + 1 * pic[h+1][w-1]
+ 1 * pic[h-1][w-1])/(pic[h][w] + pic[h+1][w] + pic[h-1][w] + pic[h][w-1]
+ pic[h+1][w-1] + pic[h-1][w-1]);
else if (w == width - 1 && (h != 0 && h != height - 1))
newPic[h][w] = (2 * pic[h][w] + 2 * pic[h][w+1] + 2 * pic[h][w-1] + 2 * pic[h-1][w] + 1 * pic[h-1][w+1]
+ 1 * pic[h-1][w-1])/(pic[h][w] + pic[h][w+1] + pic[h][w-1] + pic[h-1][w]
+ pic[h-1][w+1] + pic[h-1][w-1]);
else
newPic[h][w] = (1 * pic[h-1][w-1] + 2 * pic[h-1][w] + 1 * pic[h-1][w+1] + 2 * pic[h][w-1]
+ 2 * pic[h][w] + 2 * pic[h][w+1] + 1 * pic[h+1][w-1] + 2 * pic[h+1][w] + 1
* pic[h+1][w+1]) / (pic[h][w] + pic[h-1][w-1] + pic[h-1][w] + pic[h-1][w+1]
+ pic[h][w-1] + pic[h][w+1] + pic[h+1][w-1] + pic[h+1][w]);
}
}
memcpy(pic, newPic, sizeof(newPic));
}

提供矩阵

10 100 10 100 10 100
10 100 10 100 10 100
100 10 100 10 100 10
100 10 100 10 100 10

“模糊”后所需的矩阵

48 46 64 46 64 61
55 48 61 48 61 55
55 61 48 61 48 55
61 64 46 64 46 48

“模糊”后的实际矩阵

2 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1


编辑:我更新了我的代码以反射(reflect)所做的两个更正;第一个校正是第一个条件 block 中的 [w-1] 而不是 [w+1],第二个校正是除以邻居密度的总和(而不是值的总和)。

更新的代码块

if (h == 0 && w == 0)
newPic[h][w] = (2 * pic[h][w] + 2 * pic[h][w+1] + 2 * pic[h+1][w] + 1 * pic[h+1][w+1])/7;
else if (h == height - 1 && w == width - 1)
newPic[h][w] = (2 * pic[h][w] + 2 * pic[h-1][w] + 2 * pic[h][w-1] + 1 * pic[h-1][w-1])/7;
else if (h == height - 1 && w == 0)
newPic[h][w] = (2 * pic[h][w] + 2 * pic[h-1][w] + 2 * pic[h][w+1] + 1 * pic[h-1][w+1])/7;
else if (h == 0 && w == width - 1)
newPic[h][w] = (2 * pic[h][w] + 2 * pic[h][w-1] + 2 * pic[h+1][w] + 1 * pic[h+1][w-1])/7;
else if (h == 0 && (w != 0 && w != width - 1))
newPic[h][w] = (2 * pic[h][w] + 2 * pic[h+1][w] + 2 * pic[h-1][w] + 2 * pic[h][w+1] + 1 * pic[h+1][w+1] + 1 * pic[h-1][w+1])/10;
else if (w == 0 && (h != 0 && h != height - 1))
newPic[h][w] = (2 * pic[h][w] + 2 * pic[h+1][w] + 2 * pic[h][w+1] + 2 * pic[h][w-1] + 1 * pic[h+1][w+1] + 1 * pic[h+1][w-1])/10;
else if (h == height - 1 && (w != 0 && w != width - 1))
newPic[h][w] = (2 * pic[h][w] + 2 * pic[h+1][w] + 2 * pic[h-1][w] + 2 * pic[h][w-1] + 1 * pic[h+1][w-1] + 1 * pic[h-1][w-1])/10;
else if (w == width - 1 && (h != 0 && h != height - 1))
newPic[h][w] = (2 * pic[h][w] + 2 * pic[h][w+1] + 2 * pic[h][w-1] + 2 * pic[h-1][w] + 1 * pic[h-1][w+1] + 1 * pic[h-1][w-1])/10;
else
newPic[h][w] = (1 * pic[h-1][w-1] + 2 * pic[h-1][w] + 1 * pic[h-1][w+1] + 2 * pic[h][w-1] + 2 * pic[h][w] + 2 * pic[h][w+1] + 1 * pic[h+1][w-1] + 2 * pic[h+1][w] + 1 * pic[h+1][w+1]) / 14;

更新输出

48  43  34  43  34  61
43 48 61 48 61 43
43 61 48 61 48 43
61 34 43 34 43 48

Edit2: 结果证明我是个大傻瓜,我的行和列在我脑子里乱转。以下正确运行,再次感谢大家的帮助!

更新的代码块

// top left corner
if (h == 0 && w == 0)
newPic[h][w] = (2 * pic[h][w] + 2 * pic[h][w+1] + 2 * pic[h+1][w] + 1 * pic[h+1][w+1])/7;
// bottom right corner
else if (h == height - 1 && w == width - 1)
newPic[h][w] = (2 * pic[h][w] + 2 * pic[h-1][w] + 2 * pic[h][w-1] + 1 * pic[h-1][w-1])/7;
// top right corner
else if (h == height - 1 && w == 0)
newPic[h][w] = (2 * pic[h][w] + 2 * pic[h-1][w] + 2 * pic[h][w+1] + 1 * pic[h-1][w+1])/7;
// bottom left corner
else if (h == 0 && w == width - 1)
newPic[h][w] = (2 * pic[h][w] + 2 * pic[h][w-1] + 2 * pic[h+1][w] + 1 * pic[h+1][w-1])/7;
// top edges
else if (h == 0 && (w != 0 && w != width - 1))
newPic[h][w] = (2 * pic[h][w] + 2 * pic[h][w+1] + 2 * pic[h][w-1] + 2 * pic[h+1][w]
+ 1 * pic[h+1][w+1] + 1 * pic[h+1][w-1])/10;
// left edges
else if (w == 0 && (h != 0 && h != height - 1))
newPic[h][w] = (2 * pic[h][w] + 2 * pic[h][w+1] + 2 * pic[h+1][w] + 2 * pic[h-1][w] + 1 * pic[h+1][w+1]
+ 1 * pic[h-1][w+1])/10;
// bottom edges
else if (h == height - 1 && (w != 0 && w != width - 1))
newPic[h][w] = (2 * pic[h][w] + 2 * pic[h][w+1] + 2 * pic[h][w-1] + 2 * pic[h-1][w] + 1 * pic[h-1][w+1]
+ 1 * pic[h-1][w-1])/10;
// right edges
else if (w == width - 1 && (h != 0 && h != height - 1))
newPic[h][w] = (2 * pic[h][w] + 2 * pic[h+1][w] + 2 * pic[h-1][w] + 2 * pic[h][w-1] + 1 * pic[h+1][w-1]
+ 1 * pic[h-1][w-1])/10;
// middle cells
else
newPic[h][w] = (1 * pic[h-1][w-1] + 2 * pic[h-1][w] + 1 * pic[h-1][w+1] + 2 * pic[h][w-1]
+ 2 * pic[h][w] + 2 * pic[h][w+1] + 1 * pic[h+1][w-1] + 2 * pic[h+1][w] + 1
* pic[h+1][w+1]) / 14;

更新输出

48  46  64  46  64  61
55 48 61 48 61 55
55 61 48 61 48 55
61 64 46 64 46 48

最佳答案

您应该通过权重之和而不是输入像素值(或矩阵值,如果您愿意)的总和来归一化(除)。

例如第一种情况是:

newPic[h][w] = (2 * pic[h][w] + 2 * pic[h][w-1] + 2 * pic[h+1][w] + 1 * pic[h+1][w-1])
/ (2 + 2 + 2 + 1);

关于c++ - 尝试 "blur"C++ 中的矩阵;有缺陷的算法或代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55408488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com