How to Combine pyWavelet and openCV for image processing?
Asked Answered
Q

2

9

I need to do an image processing in python. i want to use wavelet transform as the filterbank. Can anyone suggest me which one library should i use? I had pywavelet installed, but i don't know how to combine it with opencv. If i use wavedec2 command, it raise ValueError("Expected 2D input data.")

Can anyone help me?

Question answered 2/7, 2014 at 16:36 Comment(0)
N
18

Hope this helps

import numpy as np
import pywt
import cv2    

def w2d(img, mode='haar', level=1):
    imArray = cv2.imread(img)
    #Datatype conversions
    #convert to grayscale
    imArray = cv2.cvtColor( imArray,cv2.COLOR_RGB2GRAY )
    #convert to float
    imArray =  np.float32(imArray)   
    imArray /= 255;
    # compute coefficients 
    coeffs=pywt.wavedec2(imArray, mode, level=level)

    #Process Coefficients
    coeffs_H=list(coeffs)  
    coeffs_H[0] *= 0;  

    # reconstruction
    imArray_H=pywt.waverec2(coeffs_H, mode);
    imArray_H *= 255;
    imArray_H =  np.uint8(imArray_H)
    #Display result
    cv2.imshow('image',imArray_H)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

w2d("test1.png",'db1',10)
Now answered 14/7, 2014 at 23:39 Comment(0)
K
5

Answer of Navaneeth is correct but with two correction:

1- Opencv read and save the images as BGR not RGB so you should do cv2.COLOR_BGR2GRAY to be exact.

2- Maximum level of _multilevel.py is 7 not 10, so you should do : w2d("test1.png",'db1',7)

Kenyakenyatta answered 21/7, 2017 at 14:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.