python实现图像高斯金字塔的示例代码

作者:我坚信阳光灿烂 时间:2023-05-06 02:02:32 


import cv2
import numpy as np
import matplotlib.pyplot as plt

# Grayscale
def BGR2GRAY(img):
 # Grayscale
 gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0]
 return gray

# Bi-Linear interpolation
def bl_interpolate(img, ax=1., ay=1.):
 if len(img.shape) > 2:
   H, W, C = img.shape
 else:
   H, W = img.shape
   C = 1

aH = int(ay * H)
 aW = int(ax * W)

# get position of resized image
 y = np.arange(aH).repeat(aW).reshape(aW, -1)
 x = np.tile(np.arange(aW), (aH, 1))

# get position of original position
 y = (y / ay)
 x = (x / ax)

ix = np.floor(x).astype(np.int)
 iy = np.floor(y).astype(np.int)

ix = np.minimum(ix, W-2)
 iy = np.minimum(iy, H-2)

# get distance
 dx = x - ix
 dy = y - iy

if C > 1:
   dx = np.repeat(np.expand_dims(dx, axis=-1), C, axis=-1)
   dy = np.repeat(np.expand_dims(dy, axis=-1), C, axis=-1)

# interpolation
 out = (1-dx) * (1-dy) * img[iy, ix] + dx * (1 - dy) * img[iy, ix+1] + (1 - dx) * dy * img[iy+1, ix] + dx * dy * img[iy+1, ix+1]

out = np.clip(out, 0, 255)
 out = out.astype(np.uint8)

return out

# make image pyramid
def make_pyramid(gray):
 # first element
 pyramid = [gray]
 # each scale
 for i in range(1, 6):
   # define scale
   a = 2. ** i

# down scale
   p = bl_interpolate(gray, ax=1./a, ay=1. / a)

# add pyramid list
   pyramid.append(p)

return pyramid

# Read image
img = cv2.imread("../bird.png").astype(np.float)

gray = BGR2GRAY(img)

# pyramid
pyramid = make_pyramid(gray)

for i in range(6):
 cv2.imwrite("out_{}.jpg".format(2**i), pyramid[i].astype(np.uint8))
 plt.subplot(2, 3, i+1)
 plt.title('1/' + str((i+1)**2) )
 plt.imshow(pyramid[i], cmap='gray')
 plt.axis('off')
 plt.xticks(color="None")
 plt.yticks(color="None")

plt.show()

python实现图像高斯金字塔的示例代码

python实现图像高斯金字塔的示例代码

来源:https://www.cnblogs.com/wojianxin/p/12565234.html

标签:python,图像,高斯金字塔
0
投稿

猜你喜欢

  • Python模块pexpect安装及使用流程

    2023-04-19 05:41:29
  • Go语言驱动低代码应用引擎工具Yao开发管理系统

    2024-04-30 09:56:59
  • Thinkphp5微信小程序获取用户信息接口的实例详解

    2023-10-26 09:57:08
  • 10 行 Python 代码教你自动发送短信(不想回复工作邮件妙招)

    2021-02-14 05:30:27
  • Python selenium如何设置等待时间

    2023-08-31 18:53:39
  • inner join和left join之间的区别详解

    2024-01-27 12:57:17
  • Python入门篇之面向对象

    2023-10-19 16:31:51
  • Python定义一个跨越多行的字符串的多种方法小结

    2022-08-04 03:34:27
  • Python使用百度api做人脸对比的方法

    2023-08-18 12:52:24
  • 将数据插入到MySQL表中的详细教程

    2024-01-12 22:01:21
  • 关于javascript原型的修改与重写(覆盖)差别详解

    2023-07-02 05:07:26
  • 基于python爬虫数据处理(详解)

    2023-06-07 11:38:39
  • 详解mysql中的concat相关函数

    2024-01-16 06:36:22
  • perl读写文件代码实例

    2023-01-11 22:04:39
  • C# 操作 access 数据库的实例代码

    2024-01-28 15:05:11
  • Pycharm github配置实现过程图解

    2022-06-16 03:49:08
  • PyQt5 界面显示无响应的实现

    2021-09-05 08:59:11
  • 关于点击区域

    2009-07-24 13:08:00
  • 基于Python实现通过微信搜索功能查看谁把你删除了

    2022-07-10 00:19:37
  • python随机模块random使用方法详解

    2022-07-02 04:31:10
  • asp之家 网络编程 m.aspxhome.com