python實(shí)現(xiàn)圖像高斯金字塔的示例代碼
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實(shí)現(xiàn)圖像高斯金字塔的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于python 圖像高斯金字塔的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 詳解瀏覽器的緩存機(jī)制2. ASP動態(tài)網(wǎng)頁制作技術(shù)經(jīng)驗(yàn)分享3. jsp+servlet簡單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))4. 利用CSS制作3D動畫5. 一款功能強(qiáng)大的markdown編輯器tui.editor使用示例詳解6. Python xlrd/xlwt 創(chuàng)建excel文件及常用操作7. .Net加密神器Eazfuscator.NET?2023.2?最新版使用教程8. Python 實(shí)現(xiàn)勞拉游戲的實(shí)例代碼(四連環(huán)、重力四子棋)9. 存儲于xml中需要的HTML轉(zhuǎn)義代碼10. Xml簡介_動力節(jié)點(diǎn)Java學(xué)院整理
