Inverse filtering and Wiener filtering

Inverse filtering and Wiener filtering

1. Algorithm Description

1. Motion Blur

Mainly use textbook formula: H(u, v) = sin(pi * (a * u + b * v)) * exp(-1i * pi * (a * u + b * v)) * T / (pi * (a * u + b * v)). But in addition, it should be noted that the image matrix f must be centered before the Fourier transform and after the inverse Fourier transform, and the degradation function H must also be centered.

2. Add Gaussian noise

The image with noise is the image obtained by adding the original image matrix to a matrix generated by a single element randomly generated. Each bit on this randomly generated equal-sized matrix is ​​uncorrelated with each other, but the generated values ​​appear as 0. is the mean and 500 is the variance, which is a matrix containing both positive and negative values. Therefore, after adding this noise matrix to the original image, the pixel value of each bit will become larger or smaller, but the change is a Gaussian distribution with 0 as the mean and 500 as the standard deviation.

The method of generating the noise matrix is ​​to generate a random matrix with a value between 0 and 1 as large as the original image as a probability matrix. For each probability value, the abscissa corresponding to the probability value is obtained inversely on the Gaussian distribution, and the abscissa is the value at the corresponding position of the noise matrix. The resulting noise matrix will obey a Gaussian distribution with a mean of 0 and a variance of 500.

3. Inverse filtering

In the case of the known degradation function spectrum H, since the degradation process G = F * H, on the contrary, the restoration process is roughly F = G / H, so although the degraded image often loses the imaginary part information because only the real part is taken , but still recover more information.

4. Wiener filter

Mainly based on the Wiener filtering formula: F(u,v) = (1 / H(u,v)) * (H(u,v)H*(u,v) / (H(u,v)H*( u,v) + Sn(u,v) / Sf(u,v))). Among them, Sn(u,v) / Sf(u,v) is the ratio of the noise power spectrum and the original image power spectrum. Since this value is difficult to determine, multiple constants are used to replace this value during the experiment until the recovery is found. better value.

2. Matlab code

(1) Function MotionBlur (motion blur function, incoming original image matrix and degradation function spectrum):

MotionBlur.m

function g = MotionBlur(f, H)
    f = Centralize(f);
    F = fft2(f);

    G = F .* H;
    g = ifft2(G);
    g = real(g);
    g = Centralize(g);
end

(2) Gaussian noise function GaussianNoise:

GaussianNoise.m

function gn = GaussianNoise(g, mean, var)
    [height, width] = size(g);
    noise = norminv(rand(height, width), mean, sqrt(var));
    gn = noise + double(Expand(g));
end

(3) Inverse filter function InverseFilter:

InverseFilter.m

function f_inv = InverseFilter(g, H)
    [height, width] = size(g);
    G = fft2(Centralize(g));
    B = BLPF(50, 2, height, width);

    for u = 1 : height
        for v = 1 : width
            if abs(H(u, v)) < 0.001
                tmp = 1;
            else
                tmp = H(u, v);
            end
            F(u, v) = G(u, v) * B(u, v) / tmp;
        end
    end

    f_inv = ifft2(F);
    f_inv = real(f_inv);
    f_inv = Centralize(f_inv);
end

(4) Wiener Filter function WienerFilter:

WienerFilter.m

function f = WienerFilter(g, H, k)
    G = fft2(Centralize(g));
    H2 = H .* conj(H);
    F = (G .* H2) ./ (H .* (H2 + k));
    f = ifft2(F);
    f = real(f);
    f = Centralize(f);
end

(5) Function Centralize (center transformation):

Centralize.m

function mat = Centralize( mat )
    [height, width] = size(mat);
    for i = 1 : height
        for j = 1 : width
            if mod(i + j, 2) == 1
                mat(i, j) = -mat(i, j);
            end
        end
    end
end

(6) Butterworth low-pass filter BLPF:

BLPF.m

function H = BLPF( D0, order, height, width )
    for i = 1 : height
        x = i - (height / 2);
        for j = 1 : width
            y = j - (width / 2);
            H(i, j) = 1 / (1 + ((x ^ 2 + y ^ 2) ^ order) / (D0 ^ (2 * order)));
        end
    end
end

(7) The function Expand that stretches the floating point number to 0~255:

Expand.m

function new_img = Expand( img ) 
    [height, width] = size(img);
    max_pixel = max(max(img));
    min_pixel = min(min(img));
    for i = 1 : height
        for j = 1 : width
            new_img(i, j) = 255 * (img(i, j) - min_pixel) / (max_pixel - min_pixel);
        end
    end
    new_img = uint8(new_img);
end

(8) Script Task that can be run directly by calling the above functions:

Task.m

img = imread('book_cover.jpg');
[height, width] = size(img);

f = double(img);
a = 0.1;
b = 0.1;
T = 1;
for u = 1 : height
    for v = 1 : width
        vt = a * (u - height / 2) + b * (v - width / 2);
        H(u, v) = sin(pi * vt) * exp(-1i * pi * vt) * T / (pi * vt);
        if isnan(H(u, v)) == 1
            H(u, v) = 1;
        end
    end
end

g = MotionBlur(f, H);
figure();
subplot(2, 2, 1);
imshow(Expand(g));
title('Blurred Image');

gn = GaussianNoise(g, 0, 500);
subplot(2, 2, 2);
imshow(Expand(gn));
title('Blurred Image with Gaussian Noise');

f_inv = InverseFilter(gn, H);
subplot(2, 2, 3);
imshow(uint8(f_inv));
title('Inverse Filtered Image');

f_wie = WienerFilter(gn, H, 0.01);
subplot(2, 2, 4);
imshow(uint8(f_wie));
title('Wiener Filtered Image');

3. Processing results

1. Motion Blur

write picture description here

2. Add Gaussian noise

write picture description here

3. Inverse filtering

Inverse filter the motion blurred image directly:

write picture description here

Inverse filtering of motion blur + Gaussian noise image:

write picture description here

4. Wiener filter

When the Wiener filter power ratio is 0.01:

write picture description here

Too high or too low the effect will be significantly worse.

It can be seen that both filtering methods can produce some effects, and some content can be seen in the original blurred picture. However, for the image after adding noise, the inverse filtering loses more information, and the recovery effect will not become better; and when the Wiener filter power ratio is 0.01, the effect is obviously better, and the words written on the cover can be clearly seen. .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325588905&siteId=291194637