python实现图像高斯金字塔的示例代码
import cv2import numpy as npimport matplotlib.pyplot as plt# Grayscaledef BGR2GRAY(img): # Grayscale gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] return gray# Bi-Linear interpolationdef 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 pyramiddef 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 imageimg = cv2.imread('../bird.png').astype(np.float)gray = BGR2GRAY(img)# pyramidpyramid = 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 图像高斯金字塔的资料请关注好吧啦网其它相关文章!
相关文章:
1. 如何基于python3和Vue实现AES数据加密2. Python unittest生成测试报告过程解析3. CSS3实例分享之多重背景的实现(Multiple backgrounds)4. python matplotlib工具栏源码探析二之添加、删除内置工具项的案例5. php去掉数组的第一个值的两个函数:array_shift、array_splice6. CSS Hack大全-教你如何区分出IE6-IE10、FireFox、Chrome、Opera7. 解决VUE项目使用Element-ui 下拉组件的验证失效问题8. uni-app结合PHP实现单用户登陆demo及解析9. Vue+express+Socket实现聊天功能10. 数组在java中的扩容的实例方法

网公网安备