python-2.7numpyimage-processingtypeerrordwt

discrete wavelet transform and inverse discrete wavelet transform TypeError in python


I am currently doing a project in image processing. I am new to python. Any help will be appreciated. I am decomposing image into bands using discrete wavelet transform and modify the coefficients. So below code gives me an error. here is the code

`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]

Below is error

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

How can I get rid of this error? Also please tell if DWT and IDWT syntax is correct?


Solution

  •     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);