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实现图像高斯金字塔的示例代码的详细内容,更多关于python 图像高斯金字塔的资料请关注其它相关文章!

广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?