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)
- 下一篇: 描述统计学基础