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


Posted in Python onDecember 11, 2020
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 图像高斯金字塔的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
Python单元测试框架unittest简明使用实例
Apr 13 Python
python遍历数组的方法小结
Apr 30 Python
Python脚本实现自动发带图的微博
Apr 27 Python
Python实现excel转sqlite的方法
Jul 17 Python
Python代码实现KNN算法
Dec 20 Python
Python并发编程协程(Coroutine)之Gevent详解
Dec 27 Python
opencv实现图片模糊和锐化操作
Nov 19 Python
对python中Json与object转化的方法详解
Dec 31 Python
Python+Pyqt实现简单GUI电子时钟
Feb 22 Python
python爬虫 基于requests模块的get请求实现详解
Aug 20 Python
详解有关PyCharm安装库失败的问题的解决方法
Feb 02 Python
python实现飞机大战游戏(pygame版)
Oct 26 Python
Pycharm plot独立窗口显示的操作
Dec 11 #Python
Python OpenCV中的numpy与图像类型转换操作
Dec 11 #Python
使用python操作lmdb对数据读取的实例
Dec 11 #Python
PyTorch 中的傅里叶卷积实现示例
Dec 11 #Python
python中append函数用法讲解
Dec 11 #Python
python实现图像随机裁剪的示例代码
Dec 10 #Python
python opencv图像处理(素描、怀旧、光照、流年、滤镜 原理及实现)
Dec 10 #Python
You might like
多文件上传的例子
2006/10/09 PHP
smarty 缓存控制前的页面静态化原理
2013/03/15 PHP
Session服务器配置指南与使用经验的深入解析
2013/06/17 PHP
利用php+mcDropdown实现文件路径可在下拉框选择
2013/08/07 PHP
PHP写的资源下载防盗链类分享
2014/05/12 PHP
php简单日历函数
2015/10/28 PHP
PHP call_user_func和call_user_func_array函数的简单理解与应用分析
2019/11/25 PHP
JavaScript事件列表解说
2006/12/22 Javascript
JavaScript Eval 函数使用
2010/03/23 Javascript
将两个div左右并列显示并实现点击标题切换内容
2013/10/22 Javascript
Javascript学习笔记之函数篇(五) : 构造函数
2014/11/23 Javascript
jQuery与js实现颜色渐变的方法
2016/12/30 Javascript
原生javascript实现图片放大镜效果
2017/01/18 Javascript
前端分页功能的实现以及原理(jQuery)
2017/01/22 Javascript
Node.js连接mongodb实例代码
2017/06/06 Javascript
一个有意思的鼠标点击文字特效jquery代码
2017/09/23 jQuery
浅析Proxy可以优化vue的数据监听机制问题及实现思路
2018/11/29 Javascript
ES6箭头函数和扩展实例分析
2020/05/23 Javascript
javascript实现前端input密码输入强度验证
2020/06/24 Javascript
js动态生成表格(节点操作)
2021/01/12 Javascript
[07:43]《辉夜杯》公开赛晋级外卡赛战队—TRG训练生活探秘
2015/12/11 DOTA
用Python的Flask框架结合MySQL写一个内存监控程序
2015/11/07 Python
Python结巴中文分词工具使用过程中遇到的问题及解决方法
2017/04/15 Python
详解python使用递归、尾递归、循环三种方式实现斐波那契数列
2018/01/16 Python
在flask中使用python-dotenv+flask-cli自定义命令(推荐)
2020/01/05 Python
职业技术学校毕业生推荐信
2013/12/03 职场文书
上课迟到检讨书100字
2014/01/11 职场文书
小溪流的歌教学反思
2014/02/13 职场文书
学校党的群众路线教育实践活动个人整改方案
2014/10/31 职场文书
劳动争议和解协议书范本
2014/11/20 职场文书
小学中等生评语
2014/12/29 职场文书
幼儿园大班教师个人总结
2015/02/05 职场文书
培训师岗位职责
2015/02/14 职场文书
委托收款证明
2015/06/23 职场文书
浅谈CSS不规则边框的生成方案
2021/05/25 HTML / CSS
Python实现双向链表基本操作
2022/05/25 Python