python中的离散小波变换和逆离散小波变换TypeError

Posted

技术标签:

【中文标题】python中的离散小波变换和逆离散小波变换TypeError【英文标题】:discrete wavelet transform and inverse discrete wavelet transform TypeError in python 【发布时间】:2018-10-20 15:22:51 【问题描述】:

我目前正在做一个图像处理项目。我是 python 新手。任何帮助将不胜感激。我正在使用离散小波变换将图像分解成波段并修改系数。所以下面的代码给了我一个错误。 这是代码

`import cv2
import numpy as np
from PIL import Image
import numpy
from pywt import dwt2, idwt2
from random import Random
import random
import pywt


img = cv2.imread('xyy.png')

def im2double(im):
    min_val = np.min(im.ravel())
    max_val = np.max(im.ravel())
    out = (im.astype('float') - min_val) / (max_val - min_val)
    return out
k=2
cover_object=im2double(img)

Mc=np.shape(cover_object)
Nc=np.shape(cover_object)
print(Mc[0],Nc[1])

#watermark image
water = cv2.imread('1.png')
gray_image = cv2.cvtColor(water, cv2.COLOR_BGR2GRAY)
xy=cv2.imwrite('grayim.png',gray_image) 
messagee = cv2.imread('grayim.png')
file_name1=im2double(messagee)
print(file_name1)
a=np.shape(gray_image)
b=a[0]*a[1]

images_rss = gray_image.reshape([b, 1])/256
print(images_rss)

np.random.seed(0)
key=round(100*numpy.random.rand(1))
print(key)

cA, (cH, cV, cD) = dwt2(img, 'haar')
leng=len(images_rss)
#print(leng)

for kk in range(1,leng):
    q = 2*(random.randint(512/2,512/2)-0.5)
    pn_sequence_h=round(q,0)
    w = 2*(random.randint(512/2,512/2)-0.5)
    pn_sequence_v=round(w,0)
    #print(pn_sequence_h)
    if (file_name1(kk) == 0):
        cH=cH+k*pn_sequence_h
        cV=cV+k*pn_sequence_v
idwt2(cA,cH,cV,cD,'haar')[:Mc,:Nc]

下面是错误

Traceback (most recent call last):
  File "stack.py", line 53, in <module>
    if (file_name1(kk) == 0):
TypeError: 'numpy.ndarray' object is not callable

我怎样才能摆脱这个错误?另请告知 DWT 和 IDWT 语法是否正确?

【问题讨论】:

【参考方案1】:
    import cv2
    import numpy as np
    from PIL import Image
    import numpy
    from pywt import dwt2, idwt2
    from random import Random
    import random
    import pywt


    img = cv2.imread('xyy.png')

    def im2double(im):
        min_val = np.min(im.ravel())
        max_val = np.max(im.ravel())
        out = (im.astype('float') - min_val) / (max_val - min_val)
        return out
    k=2
    cover_object=im2double(img)

    Mc=np.shape(cover_object)
    Nc=np.shape(cover_object)
    print(Mc[0],Nc[1])

    #watermark image
    water = cv2.imread('1.png')
    gray_image = cv2.cvtColor(water, cv2.COLOR_BGR2GRAY)
    xy=cv2.imwrite('grayim.png',gray_image) 
    messagee = cv2.imread('grayim.png')
    file_name1=im2double(messagee)
    print(file_name1)
    a=np.shape(gray_image)
    b=a[0]*a[1]

    images_rss = gray_image.reshape([b, 1])/256
    print(images_rss)

    np.random.seed(0)
    key=round(100*numpy.random.rand(1))
    print(key)

    cA, (cH, cV, cD) = dwt2(img, 'haar')
    leng=len(images_rss)
    #print(leng)

    for kk in range(1,leng):
        q = 2*(random.randint(512/2,512/2)-0.5)
        pn_sequence_h=round(q,0)
        w = 2*(random.randint(512/2,512/2)-0.5)
        pn_sequence_v=round(w,0)
        #print(pn_sequence_h)
        if (file_name1[kk] == 0.0):
            cH=cH+k*pn_sequence_h
            cV=cV+k*pn_sequence_v

coeffs1 = (cA, (cH, cV, cD) )           
watermarked_image = np.array(pywt.idwt2(coeffs1, 'haar'),np.uint8);

【讨论】:

以上是关于python中的离散小波变换和逆离散小波变换TypeError的主要内容,如果未能解决你的问题,请参考以下文章

用于图像纹理分析的离散小波变换

CDF 9/7 离散小波变换(卷积)

2DWT:2维离散小波变换(附Pytorch代码)

离散小波变换——一维

图像的小波变换

单尺度二维离散小波重构(逆变换)idwt2