I recently started learning image processing I was thinking that I would like to perform wavelet transform on the image, I found the following great article.
Mokemokechicken: Wavelet transform sample of image with Python
However, as the article author mokemokechicken also states. ** There was a problem that "it will not work unless the vertical and horizontal sizes of the images are the same" **.
So, this time, while referring to the sample code of mokemokechicken, By loading an image with OpenCV, ** "regardless of the vertical and horizontal size of the input image" ** wavelet transform to the image is possible. (Although it is written coolly, it is a punch line that I was able to do it well if I wanted to read the image with OpenCV and tampered with the code)
・ PyWavelets ・ Numpy ・ OpenCV
URL : http://www.pybytes.com/pywavelets/ref/2d-dwt-and-idwt.html
By using a function called * pywt.dwt2 *, for a two-dimensional array (this time an image) Two-dimensional wavelet transform can be done easily,
A function that has been extended to enable multi-level two-dimensional wavelet transform It is prepared as * pywt.wavedec2 *, and this time I dealt with this function.
This time we are doing wavelet transform for lena
import pywt
import numpy as np
import cv2
def image_normalization(src_img):
"""
Normalization process to prevent overexposure
cv2.Required when displaying wavelet-converted images with imshow (only for images with large values)
"""
norm_img = (src_img - np.min(src_img)) / (np.max(src_img) - np.min(src_img))
return norm_img
def merge_images(cA, cH_V_D):
"""numpy.4 array(upper left,(Upper right, lower left, lower right))Connect"""
cH, cV, cD = cH_V_D
cH = image_normalization(cH) #Even if you remove it, it's ok
cV = image_normalization(cV) #Even if you remove it, it's ok
cD = image_normalization(cD) #Even if you remove it, it's ok
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 np.vstack((np.hstack((cA,cH)), np.hstack((cV, cD)))) #Attach pixels at top left, top right, bottom left, bottom right
def coeffs_visualization(cof):
norm_cof0 = cof[0]
norm_cof0 = image_normalization(norm_cof0) #Even if you remove it, it's ok
merge = norm_cof0
for i in range(1, len(cof)):
merge = merge_images(merge, cof[i]) #Match the four images
cv2.imshow('', merge)
cv2.waitKey(0)
cv2.destroyAllWindows()
def wavelet_transform_for_image(src_image, level, M_WAVELET="db1", mode="sym"):
data = src_image.astype(np.float64)
coeffs = pywt.wavedec2(data, M_WAVELET, level=level, mode=mode)
return coeffs
if __name__ == "__main__":
filename = 'lena.jpg'
LEVEL = 3
# 'haar', 'db', 'sym' etc...
# URL: http://pywavelets.readthedocs.io/en/latest/ref/wavelets.html
MOTHER_WAVELET = "db1"
im = cv2.imread(filename)
print('LEVEL :', LEVEL)
print('MOTHER_WAVELET', MOTHER_WAVELET)
print('original image size: ', im.shape)
"""
Convert for each BGR channel
cv2.imread is B,G,Note that the images are spit out in the order of R.
"""
B = 0
G = 1
R = 2
coeffs_B = wavelet_transform_for_image(im[:, :, B], LEVEL, M_WAVELET=MOTHER_WAVELET)
coeffs_G = wavelet_transform_for_image(im[:, :, G], LEVEL, M_WAVELET=MOTHER_WAVELET)
coeffs_R = wavelet_transform_for_image(im[:, :, R], LEVEL, M_WAVELET=MOTHER_WAVELET)
coeffs_visualization(coeffs_B)
# coeffs_visualization(coeffs_G)
# coeffs_visualization(coeffs_R)
Original image (lena.jpg)
Image of wavelet coefficient (B channel)
If you have any corrections, please let us know.
Recommended Posts