Experiment 4 Image Frequency Domain Enhancement and Image Restoration (Digital Image Processing)

Experiment 4: Image frequency domain enhancement and image restoration

1. Experimental significance and purpose

(1) Further master the image processing tool Matlab and become familiar with image processing functions based on Matlab.

(2) Master various image frequency domain enhancement methods based on orthogonal transformation.

(3) Master the basic methods of image restoration.

2. Experimental content

Based on the previous functions, the following functions:

       1. For a noisy image, perform ideal low-pass filtering, Butterworth low-pass filtering, ideal high-pass filtering, Butterworth high-pass filtering, and high-pass enhancement

Experimental comparison: superimpose different noises (Gaussian, salt and pepper), use different cutoff frequencies, and compare the processing results.

– 2. Use the fspcial function to simulate different degradation effects on an image: motion blur, Gaussian blur, mean blur

– 3. Perform Wiener filter restoration on blurred images

Experimental comparison: Compare the filtering results of blurred images with and without superimposed noise;

3. Algorithm Principle

  1. Image filtering: Process the input image to remove noise, smooth details, etc. Among them, ideal low-pass filtering, Butterworth low-pass filtering, ideal high-pass filtering, Butterworth high-pass filtering, and high-pass enhancement are different filter types. The low-pass filter can extract low-frequency information in the image, such as the overall brightness change of the image; while the high-pass filter can highlight high-frequency detailed information such as edges and textures in the image.
  2. Image simulation degradation: Use the fspcial function to simulate the degradation effect, in which motion blur, Gaussian blur, and mean blur are different blur kernel types. Motion blur is image blur caused by the movement of the camera or object. It is common when the camera shakes when shooting moving objects or hand-held shooting; Gaussian blur is caused by the image being contaminated by Gaussian noise; mean blur is a simple linear blur method. Replace the value of each pixel with the average of surrounding pixels.
  3. Wiener filter restoration: Wiener filter is a restoration method based on the minimum mean square error (MSE) criterion, which can be used to restore images affected by degradation and noise. It reduces the effects caused by noise and degradation by weighted averaging of images in the frequency domain, while retaining detailed information in the image.

4. Program flow

The program flow of processing a noisy image with different filters:

  1. Read in a noisy image.
  2. Perform ideal low-pass filtering, Butterworth low-pass filtering, ideal high-pass filtering, Butterworth high-pass filtering and high-pass enhancement on the image, and record the image processed by each method.
  3. For each processed image, Gaussian noise and salt-and-pepper noise are superimposed, and the processed results are recorded respectively.
  4. Change the cutoff frequency, re-do the above process, and record the results at different cutoff frequencies for each method.
  5. Compare the processing effects of each filter under different parameters.

The program flow of using the fspcial function to simulate different degradation effects and Wiener filter restoration:

  1. Reads an image and displays the original image.
  2. Use the fspcial function to perform motion blur, Gaussian blur, and mean blur on the image, and record the image processed by each method.
  3. For each processed image, Gaussian noise and salt-and-pepper noise are superimposed, and the processed results are recorded respectively.
  4. The noisy images corresponding to each blur processing method are processed using the Wiener filter restoration method, and the processed results are recorded.
  5. Compare each blurring method and the filtered restoration effect with noise.

5. Main codes and comments (running results) of each part of the algorithm

1. 1

f=imread('rice.png');

f=double(f);

I1=imnoise(f,'gaussian',0.1,0.1); %Add Gaussian noise pollution to the image

I4=imnoise(f,'salt & pepper'); %Add salt and pepper noise pollution to the image

F=fftshift(fft2(f));% Perform Fourier transform on the image and shift the frequency domain center to the midpoint

d0=20; % cutoff frequency

[m,n]=size(f); %Ideal low-pass filter

h=0;

for u=1:m

    for v=1:n

        if sqrt((u-m/2)*(u-m/2)+(v-n/2)*(v-n/2))<=d0

            h=1;

        else

            h=0;

        end

        FH(u,v)=F(u,v)*h;

    end

end

fh=ifft2(FH); %Butterworth low-pass filter

nb=1;

for u=1:m

    for v=1:n

        hb=1/(1+(sqrt(2)-1)*(sqrt((u-m/2)*(u-m/2)+(v-n/2)*(v-n/2))/d0)^(2*nb));

        FHB(u,v)=F(u,v)*hb;

    end

end

figure

subplot(2,4,1),imshow(uint8(f)); title('original image');

subplot(2,4,2),imshow(uint8(abs(fh)),[]);title('Ideal low-pass filter')

subplot(2,4,3),imshow(abs(fhb),[]);title('Butterworth Low Pass Filter')

 

1.2

f=imread('rice.png');

f=double(f);

I1=imnoise(f,'gaussian',0.1,0.1); %Add Gaussian noise pollution to the image

I4=imnoise(f,'salt & pepper'); %Add salt and pepper noise pollution to the image

F=fftshift(fft2(I1));% Perform Fourier transform on the image and shift the frequency domain center to the midpoint

d0=10; % cutoff frequency

[m,n]=size(I1); %Ideal low-pass filter

h=0;

for u=1:m

    for v=1:n

        if sqrt((u-m/2)*(u-m/2)+(v-n/2)*(v-n/2))<=d0

            h=1;

        else

            h=0;

        end

        FH(u,v)=F(u,v)*h;

    end

end

fh=ifft2(FH); %Butterworth low-pass filter

nb=1;

for u=1:m

    for v=1:n

        hb=1/(1+(sqrt(2)-1)*(sqrt((u-m/2)*(u-m/2)+(v-n/2)*(v-n/2))/d0)^(2*nb));

        FHB(u,v)=F(u,v)*hb;

    end

end

figure

subplot(2,4,1),imshow(uint8(f)); title('original image');

subplot(2,4,2),imshow(I1),title('Gaussian noise contaminated image');

subplot(2,4,3),imshow(I4),title('Salt and pepper noise contaminated image');

subplot(2,4,4),imshow(uint8(abs(fh)),[]);title('Ideal low-pass filtering after Gaussian noise pollution')

subplot(2,4,5),imshow(abs(fhb),[]);title('Butterworth Low Pass Filter')

 

1.3

             f=imread('rice.png');

f=double(f);

F=fftshift(fft2(f));% Perform Fourier transform on the image and shift the frequency domain center to the midpoint

d0=10; % cutoff frequency

[m,n]=size(f); %ideal high-pass filtering

h=0;

for u=1:m

    for v=1:n

        if sqrt((u-m/2)*(u-m/2)+(v-n/2)*(v-n/2))<=d0

               h=0;

        else

            h=1;

        end

        FH(u,v)=F(u,v)*h;

    end

end

fh=ifft2(FH); %Butterworth high-pass filter

nb=1;

for u=1:m

    for v=1:n

        hb=1/(1+(sqrt(2)-1)*(d0/sqrt((u-m/2)*(u-m/2)+(v-n/2)*(v-n/2)))^(2*nb));

        FHB(u,v)=F(u,v)*hb;

    end

end

fhb=ifft2(FHB);

figure,subplot(1,3,1),imshow(uint8(f)); title('original image');

subplot(1,3,2),imshow(uint8(abs(fh)),[]);title('Ideal High Pass Filter');;

subplot(1,3,3),imshow(abs(fhb),[]);title('Butterworth High Pass Filter')

 

1.4

f=imread('rice.png');

f=double(f);

I1=imnoise(f,'gaussian',0.1,0.1); %Add Gaussian noise pollution to the image

I4=imnoise(f,'salt & pepper'); %Add salt and pepper noise pollution to the image

F=fftshift(fft2(I4));% Perform Fourier transform on the image and shift the frequency domain center to the midpoint

d0=10; % cutoff frequency

[m,n]=size(I4); %ideal high-pass filtering

h=0;

for u=1:m

    for v=1:n

        if sqrt((u-m/2)*(u-m/2)+(v-n/2)*(v-n/2))<=d0

               h=0;

        else

            h=1;

        end

        FH(u,v)=F(u,v)*h;

    end

end

fh=ifft2(FH); %Butterworth high-pass filter

nb=1;

for u=1:m

    for v=1:n

        hb=1/(1+(sqrt(2)-1)*(d0/sqrt((u-m/2)*(u-m/2)+(v-n/2)*(v-n/2)))^(2*nb));

        FHB(u,v)=F(u,v)*hb;

    end

end

fhb=ifft2(FHB);

figure

subplot(2,3,1),imshow(uint8(f)); title('original image');

subplot(2,3,2),imshow(I1),title('Gaussian noise contaminated image');

subplot(2,3,3),imshow(I4),title('Salt and pepper noise contaminated image');

subplot(2,3,4),imshow(uint8(abs(fh)),[]);title('Ideal high-pass filtering after salt and pepper noise pollution');

subplot(2,3,5),imshow(abs(fhb),[]);title('Butterworth high-pass filtering of salt and pepper noise pollution');

 

2.1

%Add noise to the image

I=imread('Lena.bmp');

%I=rgb2gray(I);

PSF=fspecial('motion',31,11);

J=imfilter(I,PSF,'conv');

figure,subplot(2,2,1),imshow(I);title('source image')

subplot(2,2,2),imshow(J),title('Motion blur image')

PSF=fspecial('disk',5); J=imfilter(I,PSF,'conv');

subplot(2,2,3),imshow(J),title('Mean blur image')

PSF=fspecial('gaussian',31,3); J=imfilter(I,PSF,'conv');

subplot(2,2,4),imshow(J),title('Gaussian blur image')

 

3.1

I=imread('Lena.bmp');

psf=fspecial('motion',31,11);

blurred=imfilter(I,psf,'full');

wnr=deconvwnr(blurred,psf,0.1);

figure,subplot(2,2,1),imshow(I),title('original image')

subplot(2,2,2),imshow(blurred),title('motion blurred image')

subplot(2,2,3),imshow(wnr),title('Weiner filter restored image')

 

3.2

I=imread('Lena.bmp');

psf=fspecial('motion',21,11);

blurred=imfilter(I,psf,'full');

noise=0.1*randn(size(blurred)); %Generate random noise matrix

bn=imadd(blurred,im2uint8(noise)); %bn is a noisy blurred image

nsr=sum(noise(:).^2)/sum(im2double(I(:)).^2); %Calculate signal-to-noise ratio

wnr=deconvwnr(bn,psf,0.1); wnr1=deconvwnr(bn,psf,nsr);

NP=abs(fftn(noise).^2); % Calculate the energy of the noisy image

IP=abs(fftn(im2double(I)).^2); % Calculate the energy of the original image, you can replace the original image with a noisy blurred image

NCORR=fftshift(real(ifftn(NP))); % Calculate the correlation coefficient of the noise image

ICORR=fftshift(real(ifftn(IP))); % Calculate the correlation coefficient of the original image

wnr2=deconvwnr(bn,psf,NCORR,ICORR);

figure,subplot(2,3,1),imshow(I),title('original image')

subplot(2,3,2),imshow(blurred),title('motion blurred image')

subplot(2,3,3),imshow(bn),title('Noisy motion blur image')

subplot(2,3,4),imshow(wnr),title('Restore image without considering noise')

subplot(2,3,5),imshow(wnr1),title('Restored image with known signal-to-noise ratio')

subplot(2,3,6),imshow(wnr2),title('Known NCORR and ICORR restored image')

 

6. Result analysis

  1. The results of the experimental comparison of ideal low-pass filtering, Butterworth low-pass filtering, ideal high-pass filtering, Butterworth high-pass filtering and high-pass enhancement on a noise image are as follows:

This experiment compares the processing results by applying different types of filters to the same noisy image and applying different cutoff frequencies when adding Gaussian and salt-and-pepper noise. Since the ideal filter has infinite response, it is difficult to implement in practical applications, so the picture will appear black. The Butterworth filter can control the filtering effect by changing the cutoff frequency. The ideal low-pass filter can effectively remove high-frequency noise, but will blur the image; the Butterworth low-pass filter can balance noise suppression and image clarity, and its performance is smoother. An ideal high-pass filter can highlight high-frequency details in an image, but it can also enhance noise. The Butterworth high-pass filter suppresses low-frequency information and enhances high-frequency information. The high-pass enhancement filter can eliminate low-frequency noise while retaining high-frequency details as much as possible, but you need to find a suitable gain factor to balance the two.

2. Use the fspcial function to simulate different degradation effects on an image: the results of motion blur, Gaussian blur, and mean blur are analyzed as follows:

This experiment simulates different image degradation effects by applying different types of blur filters to the same image. Motion blur is a blur caused by the movement of the camera or target object, which can be simulated by a linear motion blur filter; Gaussian blur is a blur caused by the pixels in the image being affected by random noise, which can be simulated by a Gaussian filter; Mean Blur is blur caused by the replacement of pixels in an image by the average of surrounding pixels, and can be simulated by a mean filter.

3. The analysis of the results of Wiener filter restoration of blurred images is as follows:

The first is image quality. In the experiment, we performed Wiener filter restoration on the blurred image and compared the processed image with the original image to evaluate the quality of the processed image. Typically, filtered images should have higher clarity, less noise, and better detail without excessive distortion.

The second is the filtering effect. In the experiment, we compared the results of filtering blurred images with and without superimposed noise. By comparing the filtered images in the two cases, we can conclude that for blurred images with superimposed noise, Wiener filtering can better remove noise and restore details, and for blurred images without superimposed noise, Wiener filtering can also improve Image quality, but the improvement may not be as dramatic as for blurred images superimposed with noise.

7. Summary

When performing image processing, different effects can be achieved through different types of filters. In this experiment, methods such as ideal low-pass filtering, Butterworth low-pass filtering, ideal high-pass filtering, Butterworth high-pass filtering and high-pass enhancement were used to process a noise image, and different cutoff frequencies and superposition of different noises were compared. Impact. It can be found that low-pass filtering can remove high-frequency information in the image, retain lower-frequency information, and smooth the image. High-pass filtering, on the other hand, can enhance high-frequency information in the image and make the image look sharper. In short, in image processing, choosing appropriate filters and parameters is very important. Matlab's fspcial function was also used in the experiment to simulate different types of degradation effects, including motion blur, Gaussian blur and mean blur. By simulating these effects, you can better understand the impact of these blurring effects on the image. At the same time, we also learned the method of Wiener filtering restoration of blurred images. This method can effectively remove noise and blur in images and improve image clarity and quality. Overall, this experiment allowed me to have a deeper understanding of relevant knowledge, and at the same time, I also experienced some techniques and details in practical applications.

Guess you like

Origin blog.csdn.net/m0_63975371/article/details/131579514