图像缩小算法

use*_*662 7 c c++ algorithm image image-recognition

你能帮我找到适合图像大小调整的算法吗?我有一个数字的图像.最大尺寸为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];
                    }
            }
Run Code Online (Sandbox Code Playgroud)

但它不适用于图像的减少,这是我之前的目标.你能帮我找一个可以解决这个问题的算法(质量一定不是完美的,速度甚至不重要).考虑到我是新手的事实,关于它的一些信息也是完美的.当然,一小段c/c ++代码(或库)也是完美的.

编辑:我找到了一个算法.它适合压缩200到20?

Mar*_*som 10

一般方法是过滤输入以生成较小的大小,并将阈值转换为单色.要实现的最简单的过滤器是简单的平均值,它通常会产生OK结果.该正弦滤波器在理论上是最好的,但它是不切实际的实施,并已振铃效应这往往是不可取的.还有许多其他过滤器,例如Lanczos或Tent(它是Bilinear的通用形式).

这是一个结合阈值的平均过滤器的版本.假设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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我现在已经测试了这段代码.这是输入200x200图像,然后是最近邻居减少到15x15(在Paint Shop Pro中完成),然后是此代码的结果.我会让你决定哪一个更忠实于原作; 如果原件有一些细节,差异会更明显.

原版的 最近的邻居 平均+阈


Hig*_*ark 0

200x200将图像尺寸缩小到 的一种方法100x100是沿每行和每列获取每第二个像素。我将让您自行编写代码以将大小缩小到不是原始大小的除数的大小。我不保证这种方法是否适合您的问题。