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

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

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")   # 保存数据图像

结果:

相关推荐
HackTorjan2 小时前
2026年5月29日:全球首个通用人工智能操作系统正式发布,开启人机协同新纪元
人工智能
刘大猫.3 小时前
智造短剧新引擎:火山引擎上线「火山剧创 1.0」,制作效率提升 80%
人工智能·ai·chatgpt·机器人·大模型·火山引擎·短剧新引擎
红尘散仙3 小时前
我把终端小说阅读器接上了 AI Agent:TRNovel 现在能用 skill 生成书源了
人工智能·后端·rust
雅菲奥朗3 小时前
企业级 AI 自动化|OpenClaw 龙虾实战与认证
运维·人工智能·自动化·openclaw
HIT_Weston3 小时前
99、【Agent】【OpenCode】task 工具提示词(Slash command)(一)
人工智能·agent·opencode
25 Hz3 小时前
Mind 爱好者时空表征刊 第24期 | 时间结构学习、空间对时间表征的补偿、事件内部的时间扭曲……
人工智能
心中有国也有家3 小时前
GE图引擎深度解析——CANN的计算图优化与执行引擎
人工智能·pytorch·python·学习·numpy
海兰3 小时前
【文字三国志:第一篇】天命重构,大语言模型(LLM)动态生成文言风格的叙事文本的文字游戏
人工智能·游戏·语言模型
cxr8284 小时前
高分子复合材料 AI 逆向设计合——验证闭环、决策优化与中试放大
人工智能·材料逆向设计合成
litble4 小时前
如何速成LLM以伪装成一个AI研究者(6)——LoRA,Adapter,P-tuning,量化,QLoRA
人工智能·lora·量化·peft·qlora·高效微调