Python代码:数字图像处理(DIP)7.1.1图像金字塔example7.1

Approximation and prediction residual pyramids

知识请看课本,这里交流代码如下:

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]
    #边界通过0灰度值填充扩展(在原来的基础上扩大m-1行,n-1列),保证图像大小不变
    img1 = np.zeros((img.shape[0]+m-1,img.shape[1]+n-1))
    #然后从扩充后的矩阵中截取和输入图像(矩阵)同样大小的一块儿,当然是从中间截取的
    img1[(m-1)//2:(img.shape[0]+(m-1)//2),(n-1)//2:(img.shape[1]+(n-1)//2)] = img
    img2 = np.zeros(img.shape)
    #逐像素移动模板
    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):
    #默认下采样因子为 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(newimg.shape[0]):
        for j in range(newimg.shape[1]):
            newimg[i,j] = img[factor*i, factor*j]
    return newimg
    
def 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 newimg
 

def 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 calculated
    if kernel_size % 2 == 1:
        centre_location = upscale_factor - 1
    else:
        centre_location = upscale_factor - 0.5
        
    bilinear = 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] = value
    return bilinear

GaussianLowpass = np.array([[1, 2, 1],[2, 4, 2],[1, 2, 1]]) / 16

img_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') #去掉坐标轴
# plt.figure('approximation pyramid')
# plt.imshow(img_a2, cmap='gray')
# plt.axis('off') #去掉坐标轴
# 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')
  • 第一个函数为卷积操作,0填充,没有设置步长
  • 第二个和第三个函数是下采样和上采样函数,函数如课本一样
  • 第三个是获得双线性插值卷积核,这里获得的是(4,4)大小卷积核,详细讲解看:
    双线性插值(Bilinear Interpolation)
    结果
    近似金字塔

512*512

256*256
128*128
64*64
128*128
256*256
512*512
后面的结果不知道对吗,希望大家教一下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值