双线性差值应用

// testC.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
using namespace std;

int a[100][100] = { 0 };
int b[100][100] = { 0 };
/*生成初始参考权重*/
void genRefWgt(int(*a)[100],int w,int h)
{
    int start_w = 0;
    int start_h = 0;
    for (int i = 0; i < h; i++)
    {
        for (int j = 0; j < w; j++)
        {
            *(*(a + i) + j) = 20;
        }
    }
    start_w = w * 10 / 100;
    start_h = h * 10 / 100;
    for (int i = start_h; i < h - start_h; i++)
    {
        for (int j = start_w; j < w - start_w; j++)
        {
            *(*(a + i) + j) = 80;
        }
    }

    start_w = w * 30 / 100;
    start_h = h * 30 / 100;
    for (int i = start_h; i < h - start_h; i++)
    {
        for (int j = start_w; j < w - start_w; j++)
        {
            *(*(a + i) + j) = 100;
        }
    }
}

/*双线性插值权重*/
int getSrcWgt(int(*a)[100],float idxw,float idxh)
{
    float result = 0;
    int x0 = (int)idxw;
    int y0 = (int)idxh;
    float u = idxw - x0;
    float v = idxh - y0;
    result = (1 - u)*(1 - v) * (*(*(a + y0) + x0)) + (1 - u) * v * (*(*(a + y0 + 1) + x0)) +  \
        u * (1 - v) * (*(*(a + y0) + x0 + 1)) + u * v * (*(*(a + y0 + 1) + x0 + 1));
    return (int)result;
}
/*生成目标权重*/
void genDstWgt(int(*a0)[100], int(*a1)[100], int w0, int h0, int w1, int h1)
{
    float wRatio = (float)w0 / w1;
    float hRatio = (float)h0 / h1;
    float idxi = 0, idxj = 0;
    for (int i = 0; i < h1; i++)
    {
        for (int j = 0; j < w1 ; j++)
        {
            idxj = wRatio * j;
            idxi = hRatio * i;
            if (j == w1 - 1 || i == h1 - 1)
            {
                *(*(a1 + i) + j) = *(*(a0 + (int)idxi) + (int)idxj);
            }
            else
            {
                *(*(a1 + i) + j) = getSrcWgt(a0, idxj, idxi);
            }

        }
    }

}

int main(int argc, char* argv[])
{
    int x0 = 15, y0 = 15;
    genRefWgt(a, x0, y0);
    for (int i = 0; i < y0; i++)
    {
        for (int j = 0; j < x0; j++)
        {
            cout << a[i][j] << " ,";
            if (j == x0-1)
            {
                cout << endl;
            }
        }
    }

    int x1 = 15, y1 = 17;
    genDstWgt(a, b, 15, 15, x1, y1);
    cout << "#######################################" << endl;
    for (int i = 0; i < y1; i++)
    {
        for (int j = 0; j < x1; j++)
        {
            cout << b[i][j] << " ,";
            if (j == x1-1)
            {
                cout << endl;
            }
        }
    }
    system("pause");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值