0

I have a code that denoise image using pywt, but when I put colored image, on denoised image gets artifacts. Code:

import pywt
import numpy as np
import matplotlib.pyplot as plt
import cv2

def inputter(img_path, wavelet, coeficient, lvl=None):
    #2.1
    im = cv2.imread(img_path)
    im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
    r,g,b = cv2.split(im)
    
    plt.figure()
    plt.imshow(im)
    plt.axis('off')
    plt.title('Original')
    
    w = wavelet #'bior1.3'
    n = lvl
    koef = coeficient/100

    def rgb_transform(image):
        C = pywt.wavedec2(image, wavelet=w, mode='periodization', level=n)
        coeff_array, coeff_slices = pywt.coeffs_to_array(C)
        #2.2
        Csort = np.sort(abs(coeff_array.reshape(-1)))  #sortiranje u rastucem poretku

        for x in [koef]:  # x - koliko se koeficijenata zadrzava

            # t - vrednost na poziciji poslednjek koeficijenta koji se odbacuje
            t = Csort[int(np.floor((1-x)*len(Csort)))]
            # maska sa 0 i 1; 1 - koeficijent je veci od definisanog praga
            m = abs(coeff_array)>t
            coeff_filt = coeff_array*m

            #2.3
            Cfilt = pywt.array_to_coeffs(coeff_filt, coeff_slices, output_format='wavedec2')
            im_r = pywt.waverec2(Cfilt, wavelet=w, mode='periodization')
            return np.uint8(im_r)

    r = rgb_transform(r)
    g = rgb_transform(g)
    b = rgb_transform(b)
    image_merge = cv2.merge([r,g,b])

    plt.figure()
    plt.imshow(image_merge)
    plt.axis('off')
    plt.show()

inputter('test.jpg', 'bior1.3', coeficient=10)

enter image description here

What could be the mistake?

I tried to use other wavelets, increase the coefficient and level, nothing fits.

0 Answers0