简体   繁体   中英

Discrete wavelet transformation on image using 'haar' wavelet in python

I am trying to apply haar wavelet on an image in python. Here is the code

from pywt import dwt2, idwt2
img = cv2.imread('xyz.png')
cA, (cH, cV, cD) = dwt2(img, 'haar')  

Then I modify coefficients embedding some data like given below

cH1=cH+k*pn_sequence_h
cV1=cV+k*pn_sequence_v

After that, I apply idwt with below code

idwt2(cA,(cH1,cV1,cD),'haar')[:Mc,:Nc]

where Mc and Nc are height and width of decomposed image.

But, I get an error with this code. Below is an error.

 Traceback (most recent call last):
 File "dwt.py", line 15, in <module>
 idwt2(cA,(cH1,cV1,cD),'haar')[:Mc,:Nc]
 File "C:\Python27\lib\site-packages\pywt\_multidim.py", line 104, in idwt2
 LL, (HL, LH, HH) = coeffs
 ValueError: too many values to unpack

What should I do in order to fix this error? I am new to python. Any help will be greatly appreciated.

I have also tried like this in below code. But here I don't get coefficients like CA, CH, CV, CD. what I get is all coefficients together.

import numpy as np
import pywt
import numpy
import PIL
from PIL import Image
img = PIL.Image.open("rot.png").convert("L")
imgarr = numpy.array(img) 
coeffs = pywt.dwt2(imgarr, 'haar')
pywt.idwt2(coeffs, 'haar')

The following should work:

idwt2((cA,(cH1,cV1,cD)),'haar')[:Mc,:Nc]

You missed packing the coeffs in a single tuple.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM