栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

什么是双线性插值_python 双线性插值?

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

什么是双线性插值_python 双线性插值?

双线性插值
参考链接:
[1]https://gitee.com/angerial/opencv/blob/master/interpolation/bilinear_interpolation.py
[2]链接: 双线性插值详解
[3]双线性插值python实现

代码:

import cv2
import numpy as np


def bilinear_interpolation(img, out_dim):
    src_h, src_w, channel = img.shape  # 原图片的高、宽、通道数
    dst_h, dst_w = out_dim[1], out_dim[0]  # 输出图片的高、宽
    print('src_h,src_w=', src_h, src_w)
    print('dst_h,dst_w=', dst_h, dst_w)
    if src_h == dst_h and src_w == dst_w:
        return img.copy()
    dst_img = np.zeros((dst_h, dst_w, 3), dtype=np.uint8)
    scale_x, scale_y = float(src_w) / dst_w, float(src_h) / dst_h
    for i 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) * srcWidth/dstWidth - 0.5
                # src_y = (dst_y + 0.5) * srcHeight/dstHeight - 0.5
                src_x = (dst_x + 0.5) * scale_x - 0.5
                src_y = (dst_y + 0.5) * scale_y - 0.5

                # 计算在源图上四个近邻点的位置
                src_x0 = int(np.floor(src_x))
                src_y0 = int(np.floor(src_y))
                src_x1 = min(src_x0 + 1, src_w - 1)
                src_y1 = min(src_y0 + 1, src_h - 1)

                # 双线性插值
                temp0 = (src_x1 - src_x) * img[src_y0, src_x0, i] + (src_x - src_x0) * img[src_y0, src_x1, i]
                temp1 = (src_x1 - src_x) * img[src_y1, src_x0, i] + (src_x - src_x0) * img[src_y1, src_x1, i]
                dst_img[dst_y, dst_x, i] = int((src_y1 - src_y) * temp0 + (src_y - src_y0) * temp1)

    return dst_img


img = cv2.imread('C:\Users\Administrator\Desktop\tufen_0137.jpg')
dst = bilinear_interpolation(img, (500, 500))
cv2.imshow("blinear", dst)
cv2.waitKey()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/786588.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号