Python代码:数字图像处理(DIP)7.1.1图像金字塔example7.1
生活随笔
收集整理的這篇文章主要介紹了
Python代码:数字图像处理(DIP)7.1.1图像金字塔example7.1
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Approximation and prediction residual pyramids
知識(shí)請(qǐng)看課本,這里交流代碼如下:
import cv2 import numpy as np from matplotlib import pyplot as plt#二維卷積操作 def correl2d(img, window): #window表示濾波模板,img表示原始矩陣#獲取模板的行和列m = window.shape[0]n = window.shape[1]#邊界通過(guò)0灰度值填充擴(kuò)展(在原來(lái)的基礎(chǔ)上擴(kuò)大m-1行,n-1列),保證圖像大小不變img1 = np.zeros((img.shape[0]+m-1,img.shape[1]+n-1))#然后從擴(kuò)充后的矩陣中截取和輸入圖像(矩陣)同樣大小的一塊兒,當(dāng)然是從中間截取的img1[(m-1)//2:(img.shape[0]+(m-1)//2),(n-1)//2:(img.shape[1]+(n-1)//2)] = imgimg2 = np.zeros(img.shape)#逐像素移動(dòng)模板for i in range(img2.shape[0]):for j in range(img2.shape[1]):temp = img1[i:i+m,j:j+n]img2[i,j] = np.sum(np.multiply(temp,window))return img2 def Downsampler2d(img, factor=2):#默認(rèn)下采樣因子為 2factor = int(factor)height = img.shape[0]width = img.shape[1]newimg = np.zeros([int(width / factor), int(height / factor)])for i in range(newimg.shape[0]):for j in range(newimg.shape[1]):newimg[i,j] = img[factor*i, factor*j]return newimgdef Upsampler2d(img, factor=2):factor = int(factor)height = img.shape[0]width = img.shape[1]newimg = np.zeros([int(width * factor), int(height * factor)])for i in range(height):for j in range(width):newimg[i*factor, j*factor] = img[i, j]return newimgdef get_bilinear_filter(filter_shape, upscale_factor):##filter_shape is [width, height, num_in_channels, num_out_channels]kernel_size = 2*upscale_factor - upscale_factor%2### Centre location of the filter for which value is calculatedif kernel_size % 2 == 1:centre_location = upscale_factor - 1else:centre_location = upscale_factor - 0.5bilinear = np.zeros([filter_shape[0], filter_shape[1]])for x in range(filter_shape[0]):for y in range(filter_shape[1]):value = (1 - abs((x - centre_location)/ upscale_factor)) * (1 - abs((y - centre_location)/ upscale_factor))bilinear[x, y] = valuereturn bilinearGaussianLowpass = np.array([[1, 2, 1],[2, 4, 2],[1, 2, 1]]) / 16img_a1 = cv2.imread('Fig0704_Vase.tif',0)img_a2 = correl2d(img_a1, GaussianLowpass) img_a2 = Downsampler2d(img_a2)img_a3 = correl2d(img_a2, GaussianLowpass) img_a3 = Downsampler2d(img_a3)img_a4 = correl2d(img_a3, GaussianLowpass) img_a4 = Downsampler2d(img_a4) #show # plt.figure('original') # plt.imshow(img_a1, cmap='gray') # plt.axis('off') #去掉坐標(biāo)軸 # plt.figure('approximation pyramid') # plt.imshow(img_a2, cmap='gray') # plt.axis('off') #去掉坐標(biāo)軸 # plt.show #save plt.imsave('approximation pyramid_a2.jpg', img_a2, cmap='gray') plt.imsave('approximation pyramid_a3.jpg', img_a3, cmap='gray') plt.imsave('approximation pyramid_a4.jpg', img_a4, cmap='gray')Bilinear_Interpolation_Filter = get_bilinear_filter((4,4), 2) LaplacianFilter = np.array([[-1,-1,-1],[-1,8,-1],[-1,-1,-1]])img_b3 = Upsampler2d(img_a4) img_b3 = correl2d(img_b3, Bilinear_Interpolation_Filter) img_b3 = correl2d(img_b3, LaplacianFilter)img_b2 = Upsampler2d(img_a3) img_b2 = correl2d(img_b2, Bilinear_Interpolation_Filter) img_b2 = correl2d(img_b2, LaplacianFilter)img_b1 = Upsampler2d(img_a2) img_b1 = correl2d(img_b1, Bilinear_Interpolation_Filter) img_b1 = correl2d(img_b1, LaplacianFilter)plt.imsave('Laplacian_pyramid_b3.jpg', img_b3, cmap='gray') plt.imsave('Laplacian_pyramid_b2.jpg', img_b2, cmap='gray') plt.imsave('Laplacian_pyramid_b1.jpg', img_b1, cmap='gray')- 第一個(gè)函數(shù)為卷積操作,0填充,沒(méi)有設(shè)置步長(zhǎng)
- 第二個(gè)和第三個(gè)函數(shù)是下采樣和上采樣函數(shù),函數(shù)如課本一樣
- 第三個(gè)是獲得雙線性插值卷積核,這里獲得的是(4,4)大小卷積核,詳細(xì)講解看:
雙線性插值(Bilinear Interpolation)
結(jié)果
近似金字塔
后面的結(jié)果不知道對(duì)嗎,希望大家教一下
總結(jié)
以上是生活随笔為你收集整理的Python代码:数字图像处理(DIP)7.1.1图像金字塔example7.1的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Python:cv2.imread读入图
- 下一篇: Python代码:数字图像处理(DIP)