深度学习基础知识 最近邻插值法、双线性插值法、双三次插值算法

深度学习基础知识 最近邻插值法、双线性插值法、双三次插值算法

1、最近邻插值法

*最邻近插值:将每个目标像素找到距离它最近的原图像素点,然后将该像素的值直接赋值给目标像素

  • 优点:实现简单,计算速度快
  • 缺点 :插值结果缺乏连续性,可能会产生锯齿状的边缘,对于图像质量影响较大,因此当处理精度要求较高的图像时,通常会采用更加精细的插值算法,例如:双线性插值、三次插值。
  • 代码示例:

python 复制代码
import numpy as np
from PIL import Image


def nearest_neighbor_interpolation(image,scale_factor):
    """
        image:输入图像数组
        scale_factor:图像缩放因子
    
    """

    # 得到输入图像的高与宽
    height,width=image.shape[:2]
    # 计算输出图像的高与宽
    out_height=int(height * scale_factor)
    out_width=int(width * scale_factor)

    # 创建爱你输出图像
    output_imaage=np.zeros((out_height,out_width,3),dtype=np.uint8)
    print(output_imaage.shape)

    # 遍历输出的每个像素,分别计算其在图像中最近邻的像素坐标,并将其像素值赋给当前像素
    for out_y in range(out_height):
        for out_x in range(out_width):
            # 计算当前像素在输入图像中的坐标
            input_x=int(round(out_x / scale_factor))
            input_y=int(round(out_y / scale_factor))
            # 判断计算出来的输入像素坐标是否越界,如果越界则赋值为边界像素
            input_x=min(input_x,width - 1)
            input_y=min(input_y,height - 1)
            # 将输入图像的像素值赋值给输出图像的对应位置上的像素值
            output_imaage[out_y,out_x]=image[input_y,input_x]
    
    return output_imaage




# 读取原始图像
input_image=Image.open("./test_image.PNG").convert("RGB")
print(input_image)

image_array=np.array(input_image)
print(image_array.shape)


output_imaage=nearest_neighbor_interpolation(image_array,5.0)


out_image_pil=Image.fromarray(output_imaage.astype("uint8"))
print(out_image_pil)

out_image_pil.save("./result.jpg")   # 保存数据图像

结果:

相关推荐
xzzd_jokelin1 分钟前
Spring AI 接入 DeepSeek:开启智能应用的新篇章
java·人工智能·spring·ai·大模型·rag·deepseek
简简单单做算法2 分钟前
基于WOA鲸鱼优化的BiLSTM双向长短期记忆网络序列预测算法matlab仿真,对比BiLSTM和LSTM
人工智能·lstm·bilstm·woa-bilstm·双向长短期记忆网络·woa鲸鱼优化·序列预测
web_155342746563 分钟前
性能巅峰对决:Rust vs C++ —— 速度、安全与权衡的艺术
c++·算法·rust
星霜旅人9 分钟前
开源机器学习框架
人工智能·机器学习·开源
资源大全免费分享19 分钟前
清华大学第五版《DeepSeek与AI幻觉》附五版合集下载方法
人工智能
龚大龙32 分钟前
机器学习(李宏毅)——RL(强化学习)
人工智能·机器学习
LaughingZhu38 分钟前
PH热榜 | 2025-02-23
前端·人工智能·经验分享·搜索引擎·产品运营
java_heartLake2 小时前
基于deepseek的AI知识库系统搭建
人工智能·deepseek
阿里云云原生3 小时前
山石网科×阿里云通义灵码,开启研发“AI智造”新时代
网络·人工智能·阿里云·ai程序员·ai程序员体验官
diemeng11194 小时前
AI前端开发技能变革时代:效率与创新的新范式
前端·人工智能