您能帮我找到正确的图像调整大小算法吗?我有一个数字的图像。最大尺寸为 200x200,我需要获得尺寸为 15x15 甚至更小的图像。图像是单色的(黑白),结果应该是相同的。这就是关于我的任务的信息。
我已经尝试过一种算法,这里是
// xscale, yscale - decrease/increase rate
for (int f = 0; f<=49; f++)
{
for (int g = 0; g<=49; g++)//49+1 - final size
{
xpos = (int)f * xscale;
ypos = (int)g * yscale;
picture3[f][g]=picture4[xpos][ypos];
}
}
但它不适用于图像的缩小,这是我之前的目标。
你能帮我找到一个可以解决这个问题的算法吗(质量不一定是完美的,速度甚至不重要)。考虑到我是新手,有关它的一些信息也将是完美的。当然,一小段 c/c++ 代码(或库)也将是完美的。
编辑:
我找到了一个算法。适合从200压缩到20吗?
一般的方法是过滤输入以生成较小的尺寸,并使用阈值将其转换为单色。最容易实现的过滤器是简单的平均值,它通常会产生不错的结果。这正弦滤波器理论上是最好的,但实施起来不切实际,并且具有通常不受欢迎的振铃效应。还有许多其他过滤器可用,例如Lanczos或帐篷(这是双线性的广义形式)。
这是平均滤波器与阈值结合的版本。假设picture4
是像素值为0或1的输入,输出是picture3
以相同的格式。我还假设x
是最不重要的维度,与通常的数学符号相反,并且与问题中的坐标相反。
int thumbwidth = 15;
int thumbheight = 15;
double xscale = (thumbwidth+0.0) / width;
double yscale = (thumbheight+0.0) / height;
double threshold = 0.5 / (xscale * yscale);
double yend = 0.0;
for (int f = 0; f < thumbheight; f++) // y on output
{
double ystart = yend;
yend = (f + 1) / yscale;
if (yend >= height) yend = height - 0.000001;
double xend = 0.0;
for (int g = 0; g < thumbwidth; g++) // x on output
{
double xstart = xend;
xend = (g + 1) / xscale;
if (xend >= width) xend = width - 0.000001;
double sum = 0.0;
for (int y = (int)ystart; y <= (int)yend; ++y)
{
double yportion = 1.0;
if (y == (int)ystart) yportion -= ystart - y;
if (y == (int)yend) yportion -= y+1 - yend;
for (int x = (int)xstart; x <= (int)xend; ++x)
{
double xportion = 1.0;
if (x == (int)xstart) xportion -= xstart - x;
if (x == (int)xend) xportion -= x+1 - xend;
sum += picture4[y][x] * yportion * xportion;
}
}
picture3[f][g] = (sum > threshold) ? 1 : 0;
}
}
我现在已经测试了这段代码。下面是输入 200x200 图像,然后是最近邻缩小到 15x15(在 Paint Shop Pro 中完成),然后是此代码的结果。我会让你决定哪个更忠实于原作;如果原作有一些细节,差异会更加明显。
![average+threshold](https://i.stack.imgur.com/PiaDb.png)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)