Python代码:数字图像处理(DIP)7.1.2子带编码example7.2
生活随笔
收集整理的这篇文章主要介绍了
Python代码:数字图像处理(DIP)7.1.2子带编码example7.2
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
Subband Coding
例子实现的代码如下:
import cv2 import numpy as np from matplotlib import pyplot as pltdef 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),:] = imgfor i in range(img.shape[1]):filter2d[:,i] = windowfor 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)] = imgfor i in range(img.shape[0]):filter2d[i,:] = windowfor 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):#默认下采样因子为 2factor = 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 newimgdef 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')- 这里的卷积函数和下采样函数都是一维且带有方向的
结果如下:
原图
近似子带
水平细节子带
垂直细节子带
对角线细节子带
总结
以上是生活随笔为你收集整理的Python代码:数字图像处理(DIP)7.1.2子带编码example7.2的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: Python代码:数字图像处理(DIP)
- 下一篇: 1.2.4 在Python中使用向量化的