使用双线性插值缩放图像

双线性插值原理解析

import numpy as np
import cv2

def main(src, dst, src_w, src_h, dst_w, dst_h, scale_x, scale_y):
    for n in range(3):  # 对channel循环
        for dst_y in range(dst_h):  # 对height循环
            for dst_x in range(dst_w):  # 对width循环
                # 目标在源上的坐标
                src_x = (dst_x + 0.5) * scale_x - 0.5
                src_y = (dst_y + 0.5) * scale_y - 0.5
                # 计算在源图上四个近邻点的位置
                src_x_0 = int(np.floor(src_x))
                src_y_0 = int(np.floor(src_y))
                src_x_1 = min(src_x_0 + 1, src_w - 1)
                src_y_1 = min(src_y_0 + 1, src_h - 1)

                # 双线性插值
                value0 = (src_x_1 - src_x) * src[src_y_0, src_x_0, n] + (src_x - src_x_0) * src[src_y_0, src_x_1, n]
                value1 = (src_x_1 - src_x) * src[src_y_1, src_x_0, n] + (src_x - src_x_0) * src[src_y_1, src_x_1, n]
                dst[dst_y, dst_x, n] = int((src_y_1 - src_y) * value0 + (src_y - src_y_0) * value1)
    return dst

if __name__ == '__main__':
    img = cv2.imread('scaleTest.jpg')

    src_h, src_w, channel = img.shape
    dst_h, dst_w = 1920, 1080
    dst = np.zeros((dst_h, dst_w, 3),np.uint8)
    args = (img, dst, src_w, src_h, dst_w, dst_h, src_w / dst_w, src_h / dst_h)
    dst = main(*args)
    cv2.namedWindow('dd', cv2.WINDOW_NORMAL)
    print(dst)
    cv2.imshow('dd',dst)
    cv2.waitKey(0)
    cv2.imwrite('bie.jpg', dst)
    cv2.destroyAllWindows()



# import numpy as np
# import cv2
# from matplotlib import pyplot as plt
#
# img_path = 'scaleTest.jpg'
# img = cv2.imread(img_path)
# src_h = img.shape[0]
# src_w = img.shape[1]
# dst_h = int(1.8 * src_h)  # 图像缩放倍数
# dst_w = int(1.8 * src_w)  # 图像缩放倍数
#
# dst_img = np.zeros((dst_h, dst_w, 3), dtype=np.uint8)
# for c in range(3):
#     for h in range(dst_h):
#         for w in range(dst_w):
#             # 目标点在原图上的位置
#             # 使几何中心点重合
#             src_x = (w + 0.5) * src_w / dst_w - 0.5
#             src_y = (h + 0.5) * src_h / dst_h - 0.5
#             if src_x < 0:
#                 src_x = 0
#             if src_y < 0:
#                 src_y = 0
#             # 不考虑几何中心重合直接对应
#             #             src_x = w*src_w/dst_w
#             #             src_y = h*src_h/dst_h
#
#             # 确定最近的四个点
#             # np.floor()返回不大于输入参数的最大整数。(向下取整)
#             x1 = int(np.floor(src_x))
#             y1 = int(np.floor(src_y))
#             x2 = int(min(x1 + 1, src_w - 1))  # 防止超出原图像范围
#             y2 = int(min(y1 + 1, src_h - 1.6))
#
#             # x方向线性插值,原公式本来要除一个(x2-x1),这里x2-x1=1
#             R1 = (x2 - src_x) * img[y1, x1, c] + (src_x - x1) * img[y1, x2, c]
#             R2 = (x2 - src_x) * img[y2, x1, c] + (src_x - x1) * img[y2, x2, c]
#
#             # y方向线性插值,同样,原公式本来要除一个(y2-y1),这里y2-y1=1
#             P = (y2 - src_y) * R1 + (src_y - y1) * R2
#             dst_img[h, w, c] = P
# plt.imshow(dst_img)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值