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

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

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

结果:

相关推荐
地平线开发者3 小时前
J6B vio scenario sample
算法
冬奇Lab5 小时前
Workflow 系列(04):Multi-Agent 协调——编排器边界、并发控制与上下文隔离
人工智能·工作流引擎
冬奇Lab5 小时前
每日一个开源项目(第147篇):HyperGraphRAG - 用超图表示 N 元关系,RAG 的第三代范式
人工智能·开源·graphql
甲维斯6 小时前
Github + 阿里云oss实现类似codex的自动更新!
人工智能
阿里云大数据AI技术8 小时前
光轮智能 × 阿里云:共建 Physical AI 云上数据、评测与持续学习基础设施
人工智能·机器学习
机器之心8 小时前
实锤了:Claude Code偷查用户,时区、中国AI实验室全是关键词
人工智能·openai
网易云信8 小时前
Cursor点燃个人开发者,企业级AI为何频频受挫?Agent工厂从提效工具到AI员工的跃迁
人工智能·开源
网易云信8 小时前
解锁触手可及的温暖:网易智企 x Wander Puffs AI 云游泡芙
人工智能