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


猜你喜欢
- 首先,创建一个DataFrame格式数据作为举例数据。# 创建一个DataFrame格式数据data = {'a': [
- 本文分为两个部分,第一部分是关于pip,第二部分关于pygal,主要关于二者的简介以及安装过程的分享,希望对大家有所帮助。一、pip1.简介
- router 动态路由清除重置matcher可达到路由还原效果在用户退出时调用 resetRouter(router) 即可还原路由impo
- UDP 客户端一个使用UDP协议的客户端示例代码,用于实现连续对话。请注意,UDP是无连接协议,因此在实现连续对话时需要特别小心。以下是示例
- 用例子说明fruit = ['pineapple','grape','pear']fruit
- 如果你想进一步了解如何用JavaScript来为网页添加交互性的话,你也许已经听过JavaScript的事件代理(event delegat
- [code]<script> var a=4.2343; alert(a.toFixed(3)); </script>
- 前言对于很多接触Python的人而言,字符的处理和语言整体的温顺可靠相比显得格外桀骜不驯难以驾驭。文章针对Python 2.7,主要因为3对
- 花了两个多钟在看 ThinkPHP 框架,不想太过深入的知道它的所有高深理论。单纯想知道怎么可以用起来,可以快捷的搭建一个网站。所以是有选择
- 一、复合查询1.1 多表查询实际开发中往往数据来自不同的表,所以需要多表查询,但是可以将多张表做笛卡尔积后的表当做是一张表,也就是单表查询。
- 1.plt.pie()饼图 常常用来显示 整体中各部分所占的比例,在python-matplotlib库中通过plt.pie()方法来实现。
- 1、编写TCP服务器程序。2、获取浏览器发送的http请求消息数据。3、读取固定的页面数据,将页面数据组装成HTTP响应消息数据并发送给浏览
- 如果希望重新定义在表中添加新记录时该列中自动生成并存储于列中的序列号,则可以更改该列的标识属性。在每个表中只能设置一个列的标识属性。具有标识
- python UDP通信1.打开虚拟通信程序,选择UDP通信并选定收发数据的ip地址和port端口:2.在虚拟机中编写python程序与主机
- 在主图中背景颜色不知道怎么改,plt.plot()中没有axisbg参数。但是子图可以对plt.subplot的参数做修改,下面是对子图的背
- 微软今天宣布正式发布SQL Server 2008服务器软件,这将帮助微软与Oracle 11g,IBM DB2 9.5数据库产品对抗.此前
- 环境配置1:安装mysql,环境变量添加mysql的bin目录环境配置2:python安装MySQL-Python请根据自身操作系统下载安装
- 条形码和二维码#引入所需要的基本包from reportlab.pdfgen import canvasfrom reportlab.gra
- 原由定期更换密码是一种非常重要的安全措施,这种做法可以有效地保护你的账户和个人信息不受黑客和网络攻击者的侵害。密码泄露是一个非常普遍的问题,
- 本文实例为大家分享了python画条形图的具体代码,供大家参考,具体内容如下在做毕设的过程中有些数据用表格来展现,会很难看出数据之间的差别,