python實現(xiàn)圖像高斯金字塔的示例代碼
更新時間:2020年12月11日 09:54:03 作者:我堅信陽光燦爛
這篇文章主要介紹了python實現(xiàn)圖像高斯金字塔的示例代碼,幫助大家更好的利用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()
以上就是python實現(xiàn)圖像高斯金字塔的示例代碼的詳細內(nèi)容,更多關(guān)于python 圖像高斯金字塔的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python獲取微信企業(yè)號打卡數(shù)據(jù)并生成windows計劃任務
由于公司的系統(tǒng)用的是Java版本,開通了企業(yè)號打卡之后又沒有預算讓供應商做數(shù)據(jù)對接,所以只能自己搗鼓這個,以下是個人設(shè)置的一些內(nèi)容,僅供大家參考2019-04-04Selenium Webdriver元素定位的八種常用方式(小結(jié))
這篇文章主要介紹了Selenium Webdriver元素定位的八種常用方式(小結(jié)),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01Python計算一個點到所有點的歐式距離實現(xiàn)方法
今天小編就為大家分享一篇Python計算一個點到所有點的歐式距離實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07