def wavelet_subtraction(img, level): """6- 7 level is recommended""" if level == 0: return img wp = WaveletPacket2D(data=img, wavelet='haar', mode='sym') back = resize(np.array(wp['a'*level].data), img.shape, order=3, mode='reflect')/(2**level) img = img - back return img
#!/usr/bin/env python # -*- coding: utf-8 -*- import numpy as np import matplotlib.pyplot as plt from PIL import Image from pywt import WaveletPacket2D im = Image.open("data/aero.png").convert('L') arr = np.fromstring(im.tostring(), np.uint8) arr.shape = (im.size[1], im.size[0]) wp2 = WaveletPacket2D(arr, 'db2', 'sym', maxlevel=2) # Show original figure plt.imshow(arr, interpolation="nearest", cmap=plt.cm.gray) path = ['d', 'v', 'h', 'a'] # Show level 1 nodes fig = plt.figure() for i, p2 in enumerate(path): ax = fig.add_subplot(2, 2, i + 1) ax.imshow(np.sqrt(np.abs(wp2[p2].data)), origin='image', interpolation="nearest", cmap=plt.cm.gray) ax.set_title(p2) # Show level 2 nodes
import numpy as np from sklearn.cluster import KMeans #import matplotlib.pyplot as plt from pywt import WaveletPacket2D import cv2 Num_Img = 5000 DIR = './titles/' pos = './pos_examples' neg = './neg_examples' file_list = os.listdir(DIR) samples = [] for i in range(Num_Img): gray = cv2.imread(DIR + file_list[i], 0) wp2 = WaveletPacket2D(gray, 'db2', 'symmetric', maxlevel=1) print(file_list[i]) path = ['d', 'v', 'h', 'a'] feature = np.zeros([4, wp2['d'].data.size]) for j, p2 in enumerate(path): feature[j, :] = np.reshape(wp2[p2].data, [1, wp2[p2].data.size]) f = np.cov(feature) f = f.reshape(f.size) samples.append(f) samples = np.array(samples) kmeans = KMeans(n_clusters=2, random_state=0).fit(samples) l = kmeans.labels_ for i in range(Num_Img): if l[i] == 1: shutil.copy(DIR + file_list[i], pos)