I became interested in wavelet transform for a while, so I actually tried to see what it was like.
The following three are required. Let's put it in with pip etc.
gist:image_wavelet_transform.py
image_wavelet_transform.py
# coding: utf8
# 2013/2/1 [email protected]
"""Sample script to get an image of the wavelet transform
Require: pip install PyWavelets numpy PIL
Usage: python image_wavelet_transform.py <filename> (<level>:=3) (wavelet:=db1)
"""
import sys
from PIL import Image
import pywt, numpy
filename = sys.argv[1]
LEVEL = len(sys.argv) > 2 and int(sys.argv[2]) or 3
WAVLET = len(sys.argv) > 3 and sys.argv[3] or "db1"
def merge_images(cA, cH_V_D):
"""numpy.4 array(upper left,(Upper right, lower left, lower right))Stick"""
cH, cV, cD = cH_V_D
print cA.shape, cH.shape, cV.shape, cD.shape
cA = cA[0:cH.shape[0], 0:cV.shape[1]] #If the original image is not a power of 2, there may be fractions, so adjust the size. Match the smaller one.
return numpy.vstack((numpy.hstack((cA,cH)), numpy.hstack((cV, cD)))) #Attach pixels at top left, top right, bottom left, bottom right
def create_image(ary):
"""numpy.Convert array to Grayscale image"""
newim = Image.new("L", ary.shape)
newim.putdata(ary.flatten())
return newim
def wavlet_transform_to_image(gray_image, level, wavlet="db1", mode="sym"):
"""Wavelet transform gray image for level hierarchy and return each stage as image representation
return [Restore level 0 image,Restore level 1 image,... ,Restore level<level-1>Image of, 各2D係数を1枚Image ofにした画像]
"""
ret = []
data = numpy.array(list(gray_image.getdata()), dtype=numpy.float64).reshape(gray_image.size)
images = pywt.wavedec2(data, wavlet, level=level, mode=mode) # http://www.pybytes.com/pywavelets/ref/2d-dwt-and-idwt.html
for i in range(2, len(images)+1): #Partially restore and pack in ret
ary = pywt.waverec2(images[0:i], WAVLET) * 2**(i-1) / 2**level #The added value is not returned when partially restored(It becomes whitish)So adjust
ret.append(create_image(ary))
#Make each 2D coefficient into one image
merge = images[0] / (2**level) #Values are added to the cA part, so take the average for image display.
for i in range(1, len(images)):
merge = merge_images(merge, images[i]) #Match the four images
ret.append(create_image(merge))
return ret
if __name__ == "__main__":
im = Image.open(filename)
if im.size[0] != im.size[1]: #If the vertical and horizontal sizes are not the same, something will not work, so I will match it for the time being
max_size = max(im.size)
newim = Image.new(im.mode, (max_size, max_size))
newim.paste(im, (0,0))
im = newim
im.getdata() #If you don't call this, for some reason the next split()Fails ...? why?
bands = im.split() #Process for each RGB channel
converted_bands_array = [wavlet_transform_to_image(gray, LEVEL, wavlet=WAVLET) for gray in bands] #Convert for each RGB channel
# zip(*hoge)Is difficult to understand, but converted_For bands(R,G,B)Image of(PIL.Image)Enter.Restore to RGB image with merge
converted_array = [Image.merge(im.mode, converted_bands) for converted_bands in zip(*converted_bands_array)]
# converted_array: [Restore level 0 image,Restore level 1 image,... ,Restore level<level-1>Image of, 各2D係数を1枚Image ofにした画像]
for i, img in enumerate(converted_array):
img.save("%s_%d.png " % (filename, i)) #Appropriate image output
Original image (kuma_005.jpg)
Image of coefficient imaged (level = 5) (kuma_005.jpg_5.png)
Recommended Posts