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

 更新时间:2020年12月11日 09:54:03   作者:我坚信阳光灿烂  
这篇文章主要介绍了python实现图像高斯金字塔的示例代码,帮助大家更好的利用python处理图片,感兴趣的朋友可以了解下
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 图像高斯金字塔的资料请关注脚本之家其它相关文章!

相关文章

  • Python中np.linalg.norm()用法实例总结

    Python中np.linalg.norm()用法实例总结

    在线性代数中一个向量通过矩阵转换成另一个向量时,原有向量的大小就是向量的范数,这个变化过程的大小就是矩阵的范数,下面这篇文章主要给大家介绍了关于Python中np.linalg.norm()用法的相关资料,需要的朋友可以参考下
    2022-07-07
  • 将matplotlib绘图嵌入pyqt的方法示例

    将matplotlib绘图嵌入pyqt的方法示例

    这篇文章主要介绍了将matplotlib绘图嵌入pyqt的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-01-01
  • python八皇后问题的解决方法

    python八皇后问题的解决方法

    这篇文章主要为大家详细介绍了python八皇后问题的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-09-09
  • PyTorch中的Variable变量详解

    PyTorch中的Variable变量详解

    今天小编就为大家分享一篇PyTorch中的Variable变量详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-01-01
  • Python使用eval函数执行动态标表达式过程详解

    Python使用eval函数执行动态标表达式过程详解

    这篇文章主要介绍了Python使用eval函数执行动态标表达式过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-10-10
  • 基于Python实现身份证信息识别功能

    基于Python实现身份证信息识别功能

    身份证是用于证明个人身份和身份信息的官方证件,在现代社会中,身份证被广泛应用于各种场景,如就业、教育、医疗、金融等,它包含了个人的基本信息,本文给大家介绍了如何基于Python实现身份证信息识别功能,感兴趣的朋友可以参考下
    2024-01-01
  • Python实现的个人所得税计算器示例

    Python实现的个人所得税计算器示例

    这篇文章主要介绍了Python实现的个人所得税计算器,涉及Python条件判断与数值运算相关操作技巧,需要的朋友可以参考下
    2018-06-06
  • python实现过滤敏感词

    python实现过滤敏感词

    这篇文章主要介绍了python如何实现过滤敏感词,帮助大家更好的理解和学习使用python,感兴趣的朋友可以了解下
    2021-05-05
  • Python configparser模块应用过程解析

    Python configparser模块应用过程解析

    这篇文章主要介绍了Python configparser模块应用过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-08-08
  • python中绕过反爬虫的方法总结

    python中绕过反爬虫的方法总结

    在本篇文章里小编给大家整理的是一篇关于python中绕过反爬虫的方法总结内容,需要的朋友们可以参考下。
    2020-11-11

最新评论