python实现图像高斯金字塔的示例代码
作者:我坚信阳光灿烂 发布时间:2023-05-06 02:02:32
标签: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()
来源:https://www.cnblogs.com/wojianxin/p/12565234.html
0
投稿
猜你喜欢
- 希腊Web 设计师Christos Chiotis 发表在 CssGlobe 的一篇文章,讲述了黄金分割率在 CSS 中的应用。黄金分割率是
- 前言我在使用mac安装virtualwrapper的时候遇到了问题,搞了好长时间,才弄好,在这里总结一下分享出来,供遇到相同的问题的朋友使用
- 本文实例讲述了Python实现将Excel转换成xml的方法。分享给大家供大家参考,具体如下:最近写了个小工具 用于excel转成xml直接
- 1.业务需求背景业务提供一张底层图片1以及需要在底层图片上添加的图片2,两张图片大小不一致,将小图2添加到底图1中,并在其他的空白部分添加个
- 你的SQL Server最近是否运行不正常?不,我指的不是我们肯定会遇到的通常的数据库和操作系统问题。我的意思是,你是否经历过服务器的反应迟
- For 循环可以遍历字符串,也可以遍历列表# for 循环# 语法特点:遍历操作,依次取集合容器中的几个值# for 临时变量
- PHP的isset()函数 一般用来检测变量是否设置 格式:bool isset ( mixed var [, mixed var [, .
- 我们都一定对比过编程的轻松与简单性。虽然我们都确认php和perl是最容易学习和编程的语言,但我仍旧想知道,如果用php、asp、jsp以及
- 最近设计一个优惠券列表,有个属性是有效日期,因为空间有限,必须要把开始日期和结束日期在一行显示,这样就出现一排的数字,日期有好几种我们习惯的
- 脚本1:这两天用python写了一个删除指定目录下过期时间的脚本。也可能是我初学python,对python还不够熟习,总觉得这个脚本用sh
- 测试代码1:def test(self): data = {"add": {"doc":
- Python单元测试unittest中提供了一下四种装饰器实现测试跳过和预期故障。(使用Python 2.7.13)请查考Python手册中
- 以下以 IE 代替 Internet Explorer,以 MF 代替 
- 假如一个页面中的文本采用的都是同样的字体、同样的字号、同样的颜色,做为读者的你能轻易的区分出哪里是标题,哪里是正文内容吗?所以通常情况下,设
- isNaN函数 返回一个 Boolean 值,指明提供的值是否是保留值 NaN (不是数字)。 NaN 即 Not a Number isN
- 之前有写过一篇浏览器的tab设计,这回说说网站的tab设计。一说到tab很自然地就想到了导航、信息架构。随着网站信息结构的复杂化,选择tab
- “Be conservative in what you send; be liberal in what you accept. &nbs
- python实现情感分析(Word2Vec)** 前几天跟着老师做了几个项目,老师写的时候劈里啪啦一顿敲,写了个啥咱也布吉岛,线下自己就瞎琢
- 在linux服务器lvs负载均衡、双机热备应用中经常用到mysql双机热备,安装和配置过程如下:一、 安装MYSQL# cp mysql-s
- 本周暂时比较清闲,可以保持每日一更的速度。国外身份证项目新增需求,检测出身份证正面的人脸。最开始考虑mobilenet-ssd,经同事提醒,