首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于双三次的C++图像插值

基于双三次的C++图像插值
EN

Stack Overflow用户
提问于 2016-04-07 15:24:08
回答 2查看 4K关注 0票数 0

我只是想通过BiCubic插值来平滑图像。我得到了一些代码,这是用来插值RGB图像。我已经更改了代码,以便为灰度图像工作。但结果我只得到了全黑的图像。考虑输入和输出图像大小相同。代码粘贴在下面。请帮帮我。提前谢谢。

代码语言:javascript
复制
 inline Uint16 saturate(float x, unsigned max_pixel)
{
    return x > max_pixel ? max_pixel
        : x < 0.0f ? 0
        : Uint16(x);
}

inline float get_subpixel(const Uint16* in, std::size_t dest_width, std::size_t dest_height, unsigned x, unsigned y)
{
    if (x < dest_width && y < dest_height)
        return in[(y * dest_width) + x];

    return 0;
}


void interpolate(unsigned dest_width, unsigned dest_height, unsigned bits_allocated, const Uint16* src, Uint16** dest)
{
    const double tx = 1;
    const double ty = 1;
    float C[5] = { 0 };
    unsigned max_bit = pow(2, bits_allocated);

    for (unsigned i = 0; i < dest_height; ++i)
    {
        for (unsigned j = 0; j < dest_width; ++j)
        {
            const float x = float(tx * j);
            const float y = float(ty * i);
            const float dx = tx * j - x, dx2 = dx * dx, dx3 = dx2 * dx;
            const float dy = ty * i - y, dy2 = dy * dy, dy3 = dy2 * dy;


            for (int jj = 0; jj < 4; ++jj)
            {
                const int idx = y - 1 + jj;
                float a0 = get_subpixel(src, dest_width, dest_height, x, idx);
                float d0 = get_subpixel(src, dest_width, dest_height, x - 1, idx) - a0;
                float d2 = get_subpixel(src, dest_width, dest_height, x + 1, idx) - a0;
                float d3 = get_subpixel(src, dest_width, dest_height, x + 2, idx) - a0;
                float a1 = -(1.0f / 3.0f) * d0 + d2 - (1.0f / 6.0f) * d3;
                float a2 = 0.5f  * d0 + 0.5f *  d2;
                float a3 = -(1.0f / 6.0f) * d0 - 0.5f * d2 + (1.0f / 6.0f) * d3;
                C[jj] = a0 + a1 * dx + a2 * dx2 + a3 * dx3;

                d0 = C[0] - C[1];
                d2 = C[2] - C[1];
                d3 = C[3] - C[1];
                a0 = C[1];
                a1 = -(1.0f / 3.0f) * d0 + d2 - (1.0f / 6.0f) * d3;
                a2 = 0.5f  * d0 + 0.5f  * d2;
                a3 = -(1.0f / 6.0f) * d0 - 0.5f * d2 + (1.0f / 6.0f) * d3;
                (*dest)[i * dest_width + j] = saturate(a0 + a1 * dy + a2 * dy2 + a3 * dy3, max_bit);
            }
        }
    }
}
EN

回答 2

Stack Overflow用户

发布于 2016-04-07 19:53:40

你怎么能有这个?在jj循环结束之前,c还没有被计算出来,大括号应该在d之上,否则我不会考虑这个方法是否正确。

代码语言:javascript
复制
for (int jj = 0; jj < 4; ++jj)
        {
            const int idx = y - 1 + jj;
            float a0 = get_subpixel(src, dest_width, dest_height, x, idx);
            float d0 = get_subpixel(src, dest_width, dest_height, x - 1, idx) - a0;
            float d2 = get_subpixel(src, dest_width, dest_height, x + 1, idx) - a0;
            float d3 = get_subpixel(src, dest_width, dest_height, x + 2, idx) - a0;
            float a1 = -(1.0f / 3.0f) * d0 + d2 - (1.0f / 6.0f) * d3;
            float a2 = 0.5f  * d0 + 0.5f *  d2;
            float a3 = -(1.0f / 6.0f) * d0 - 0.5f * d2 + (1.0f / 6.0f) * d3;
            C[jj] = a0 + a1 * dx + a2 * dx2 + a3 * dx3;

        // } // end jj

            d0 = C[0] - C[1];
            d2 = C[2] - C[1];
            d3 = C[3] - C[1];
            a0 = C[1];
            a1 = -(1.0f / 3.0f) * d0 + d2 - (1.0f / 6.0f) * d3;
            a2 = 0.5f  * d0 + 0.5f  * d2;
            a3 = -(1.0f / 6.0f) * d0 - 0.5f * d2 + (1.0f / 6.0f) * d3;
            (*dest)[i * dest_height + j] = saturate(a0 + a1 * dy + a2 * dy2 + a3 * dy3, max_bit);
        } // end jj move his above
    }
}
票数 1
EN

Stack Overflow用户

发布于 2019-05-16 19:39:29

我想分享很棒的链接cubic splines

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36469357

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档