以下用于图像调整大小的 cpp Bucubic 插值代码有什么问题

发布于 2024-11-30 09:55:48 字数 1370 浏览 0 评论 0原文

我正在尝试使用双三次插值对图像进行上采样,我需要与 opencv 的 cvResize() 函数匹配的准确值,但是以下代码的结果与 cvResize() 的结果不匹配,您可以看一下并帮助我修复错误。

Image *Image::resize_using_Bicubic(int w, int h) {
    float dx,dy;
    float x,y;
    float tx, ty;
    float i,j,m,n;

Image *result=new Image(w, h); tx = (float)this->m_width/(float)w; ty = (float)this->m_height/(float)h; for(i=0; i< w; i++) { for(j=0; j< h; j++) { x = i*tx; y = j*ty; dx = float(i-x)-(int)(i-x); dy = float(j-y)-(int)(j-y); float temp=0.0; for(m=-1;m<=2;m++) { for(n=-1;n<=2;n++) { int HIndex,WIndex; HIndex=(y+n); WIndex=(x+m); if (HIndex<0) { HIndex=0; } else if(HIndex>this->getHeight()) { HIndex=this->getHeight()-1; } if (WIndex<0) { WIndex=0; } else if(WIndex>this->getWidth()) { WIndex=this->getWidth()-1; } temp+=this->getPixel(HIndex,WIndex)*R(m-dx)*R(dy-n); } } result->setPixel(j, i, temp); } } return result;

}

I am trying to upsample an Image using Bicubic Interpoloation, I need the accurate values matching the cvResize() function of opencv, but the results of following code is not matching the results from cvResize(), can you take a look and help me to fix the bug.

Image *Image::resize_using_Bicubic(int w, int h) {
    float dx,dy;
    float x,y;
    float tx, ty;
    float i,j,m,n;

Image *result=new Image(w, h); tx = (float)this->m_width/(float)w; ty = (float)this->m_height/(float)h; for(i=0; i< w; i++) { for(j=0; j< h; j++) { x = i*tx; y = j*ty; dx = float(i-x)-(int)(i-x); dy = float(j-y)-(int)(j-y); float temp=0.0; for(m=-1;m<=2;m++) { for(n=-1;n<=2;n++) { int HIndex,WIndex; HIndex=(y+n); WIndex=(x+m); if (HIndex<0) { HIndex=0; } else if(HIndex>this->getHeight()) { HIndex=this->getHeight()-1; } if (WIndex<0) { WIndex=0; } else if(WIndex>this->getWidth()) { WIndex=this->getWidth()-1; } temp+=this->getPixel(HIndex,WIndex)*R(m-dx)*R(dy-n); } } result->setPixel(j, i, temp); } } return result;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

好菇凉咱不稀罕他 2024-12-07 09:55:48

你还没有说结果有多么不同。如果它们非常接近,例如每个 RGB 通道中的 1 或 2 以内,则可以简单地通过舍入差异来解释。

双三次插值的算法不止一种。 Don Mitchell 和 Arun Netravali 进行了分析,并提出了一个公式来描述其中的一些:http:// /www.mentallandscape.com/Papers_siggraph88.pdf

编辑:还有一件事,各个滤波器系数应该相加并用于除以最终值最后,对值进行标准化。我不确定为什么一个是 m-dx ,另一个是 dy-n ,它们不应该是相同的符号吗?

r=R(m-dx)*R(dy-n);
r_sum+=r;
temp+=this->getPixel(HIndex,WIndex)*r;
. . .
result->setPixel(j, i, temp/r_sum);

You haven't said how different the results are. If they're very close, say within 1 or 2 in each RGB channel, this could be explained simply by roundoff differences.

There is more than one algorithm for Bicubic interpolation. Don Mitchell and Arun Netravali did an analysis and came up with a single formula to describe a number of them: http://www.mentallandscape.com/Papers_siggraph88.pdf

Edit: One more thing, the individual filter coefficients should be summed up and used to divide the final value at the end, to normalize the values. And I'm not sure why you have m-dx for one and dy-n for the other, shouldn't they be the same sign?

r=R(m-dx)*R(dy-n);
r_sum+=r;
temp+=this->getPixel(HIndex,WIndex)*r;
. . .
result->setPixel(j, i, temp/r_sum);
吃颗糖壮壮胆 2024-12-07 09:55:48

将:更改

            else if(HIndex>this->getHeight())

为:

            else if(HIndex >= this->getHeight())

并将:更改

            else if(WIndex>this->getWidth())

为:

            else if(WIndex >= this->getWidth())

编辑

还将:更改

    for(m=-1;m<=2;m++)
    {
        for(n=-1;n<=2;n++)

为:

    for(m = -1; m <= 1; m++)
    {
        for(n = -1; n <= 1; n++)

Change:

            else if(HIndex>this->getHeight())

to:

            else if(HIndex >= this->getHeight())

and change:

            else if(WIndex>this->getWidth())

to:

            else if(WIndex >= this->getWidth())

EDIT

Also change:

    for(m=-1;m<=2;m++)
    {
        for(n=-1;n<=2;n++)

to:

    for(m = -1; m <= 1; m++)
    {
        for(n = -1; n <= 1; n++)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文