Python代码:数字图像处理(DIP)7.1.2子带编码example7.2

Subband Coding

例子实现的代码如下:

import cv2
import numpy as np
from matplotlib import pyplot as plt


def correl_1D(img, window, direction = 'Row'):      
    m = window.shape[0]
    img2 = np.zeros(img.shape)
    if direction == 'Row':
        img1 = np.zeros([img.shape[0] + m-1, img.shape[1]])
        filter2d = np.zeros([m, img.shape[1]])
        img1[(m-1)//2:(img.shape[0]+(m-1)//2),:] = img
        for i in range(img.shape[1]):
            filter2d[:,i] = window
        for j in range(img.shape[0]):
            temp = img1[j:j+m, :]
            img2[j, :] = np.sum(np.multiply(temp, filter2d), 0)
    if direction == 'Column':
        img1 = np.zeros([img.shape[0], img.shape[1] + m-1])
        filter2d = np.zeros([img.shape[0], m])
        img1[:,(m-1)//2:(img.shape[1]+(m-1)//2)] = img
        for i in range(img.shape[0]):
            filter2d[i,:] = window
        for j in range(img.shape[1]):
            temp = img1[:, j:j+m]
            img2[:, j] = np.sum(np.multiply(temp, filter2d), 1)
    
    return img2  

def Downsampler2d(img, direction = 'Row', factor=2):
    #默认下采样因子为 2
    factor = int(factor)
    m = img.shape[0]
    n = img.shape[1]
    if direction == 'Row':
        newimg = np.zeros([int(m / factor), n])
        for i in range(newimg.shape[0]):
            newimg[i,:] = img[factor*i, :]
    if direction == 'Column':
        newimg = np.zeros([m, int(n / factor)])
        for i in range(newimg.shape[1]):
            newimg[:,i] = img[:, factor*i]
    return newimg

def Orthonormal_filter(g0):
    K = len(g0)
    g1 = np.zeros([K,])
    h0 = np.zeros([K,])
    h1 = np.zeros([K,])
    for n in range(K):
        g1[n] = (-1)**n * g0[K - 1 - n]
        h0[n] = g0[K - 1 - n]
        h1[n] = g1[K - 1 - n]
    return (g1, h0, h1)

g0 = np.array([0.23037781, 0.71484657, 0.63088076, -0.02798376,
               -0.18703481, 0.03084138, 0.03288301, -0.01059740])

(g1,h0,h1) = Orthonormal_filter(g0)
img = cv2.imread('original.jpg',0) 
img_1 = correl_1D(img, h0, 'Row')
img_1 = Downsampler2d(img_1,'Row')
#近似子带
img_a = correl_1D(img_1, h0, 'Column')
img_a = Downsampler2d(img_a,'Column')
plt.imsave('subband_approximation.jpg', img_a, cmap='gray')
#垂直细节子带
img_c = correl_1D(img_1, h1, 'Column')
img_c = Downsampler2d(img_c,'Column')
plt.imsave('subband_vertical detail.jpg', img_c, cmap='gray') 
#水平细节子带
img_2 = correl_1D(img, h1, 'Row')
img_2 = Downsampler2d(img_2,'Row')
img_b = correl_1D(img_2, h0, 'Column')
img_b = Downsampler2d(img_b,'Column')
plt.imsave('subband_horizontal detail.jpg', img_b, cmap='gray')
#对角线细节子带
img_d = correl_1D(img_2, h1, 'Column')
img_d = Downsampler2d(img_d,'Column')
plt.imsave('subband_diagonal detail.jpg', img_d, cmap='gray')
  • 这里的卷积函数和下采样函数都是一维且带有方向的
    结果如下:
    原图
    original
    近似子带
     近似子带
    水平细节子带
    水平细节子带
    垂直细节子带
    垂直细节子带
    对角线细节子带
    对角线细节子带
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值