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')
- 这里的卷积函数和下采样函数都是一维且带有方向的
结果如下:
原图
近似子带
水平细节子带
垂直细节子带
对角线细节子带