图像缩小算法 [英] Image downscaling algorithm

查看:140
本文介绍了图像缩小算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能帮我找到合适的图像大小调整算法吗?我有一个数字的图像。最大尺寸为200x200,我需要获得尺寸为15x15甚至更小的图像。图像是单色(黑白),结果应该相同。这是关于我的任务的信息。

Could you help me find the right algorithm for image resizing? I have an image of a number. The maximum size is 200x200, I need to get an image with size 15x15 or even less. The image is monochrome (black and white) and the result should be the same. That's the info about my task.

我已经尝试了一种算法,这里是

I've already tried one algorithm, here it is

// 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 ++代码(或库)也是完美的。

But it won't work with the decrease of an image, which is my prior target. Could you help me find an algorithm, which could solve that problem (quality mustn't be perfect, the speed doesn't even matter). Some information about it would be perfect too considering the fact I'm a newbie. Of course, a short piece of c/c++ code (or a library) will be perfect too.

编辑:
我找到了算法。它是否适合从200压缩到20?

I've found an algorithm. Will it be suitable for compressing from 200 to 20?

推荐答案

一般的方法是过滤输入以生成更小的尺寸,和转换为单色的阈值。要实现的最简单的过滤器是简单的平均值,它通常会产生OK结果。 Sinc过滤器在理论上是最好的,但它实现起来是不切实际的,并且具有通常不合需要的振铃伪像。还有许多其他过滤器,例如 Lanczos 或Tent(这是Bilinear的通用形式)。

The general approach is to filter the input to generate a smaller size, and threshold to convert to monochrome. The easiest filter to implement is a simple average, and it often produces OK results. The Sinc filter is theoretically the best but it's impractical to implement and has ringing artifacts which are often undesirable. Many other filters are available, such as Lanczos or Tent (which is the generalized form of Bilinear).

这是一个平均过滤器与阈值组合的版本。假设 picture4 是像素值为0或1的输入,输出为 picture3 ,格式相同。我还假设 x 是与通常的数学符号相反的最不重要的维度,与你问题中的坐标相反。

Here's a version of an average filter combined with thresholding. Assuming picture4 is the input with pixel values of 0 or 1, and the output is picture3 in the same format. I also assumed that x is the least significant dimension which is opposite to the usual mathematical notation, and opposite to the coordinates in your question.

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中完成),然后是此代码的结果。我会让你决定哪一个更忠实于原作;如果原版有一些细节,差别会更明显。

I've now tested this code. Here's the input 200x200 image, followed by a nearest-neighbor reduction to 15x15 (done in Paint Shop Pro), followed by the results of this code. I'll leave you to decide which is more faithful to the original; the difference would be much more obvious if the original had some fine detail.



这篇关于图像缩小算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆