E E 458 Project 003

16
Penn State University E E 458 Project 3 Using Adaptive Filters to Remove Gaussian Noise By Chad Ryan Weiss 4/6/2016

Transcript of E E 458 Project 003

Page 1: E E 458 Project 003

Penn State University

E E 458 Project 3

Using Adaptive Filters to Remove Gaussian Noise

By Chad Ryan Weiss4/6/2016

Page 2: E E 458 Project 003

Abstract:

Adaptive filtering is a popular digital image processing technique that uses statistical data from an image to alter the contents of that image. The type of filter used in this report will take the variance and mean of the image and apply it in a way that will hopefully reduce the amount of Gaussian noise present in the image. The first part of this report will go into the theory behind adaptive filtering. Then we will apply this theory and create a MatLab computer program that will remove unwanted noise from an image in the second part of this report.

Page 3: E E 458 Project 003

Theory: Adaptive Filtering

Adaptive filters use dynamic variables as inputs to acquire a desired output.

Application: Removing Gaussian Noise

% Name: Chad Weiss% Date: March 25, 2016% Project: E E 458 Project 003% Instructor: Dr. Aldo Morales % This program implements an adaptive filtering technique that uses the% statistical means and variances of the image to reduce gaussian noise. clear all;clc; % Read-In Image I = imread('Neighbors.jpg'); Status = imfinfo('Neighbors.jpg'); % Add Noise to Imagenoise_variance = 0.03;J = imnoise(I,'gaussian',0,noise_variance); % Zero BorderInput = [zeros(1,Status.Width,3);J;zeros(1,Status.Width,3)];Input = [zeros(Status.Height+2,1,3),Input,zeros(Status.Height+2,1,3)];imtool(Input); % Noisy r,g and b planes (RGB format)r = Input; r(:,:,2:3)=0;g = Input; g(:,:,1)=0; g(:,:,3)=0; b = Input; b(:,:,1:2)=0; R = double(r(:,:,1))/256; G = double(g(:,:,2))/256; B = double(b(:,:,3))/256; % Define the Filter Maskmask = zeros(3,3); % Define Output ImageOutput_R = zeros(Status.Height+2, Status.Width+2);Output_G = zeros(Status.Height+2, Status.Width+2);Output_B = zeros(Status.Height+2, Status.Width+2);

% Red Value Plane Loopfor i = 1:Status.Height for j = 1:Status.Width mask = R(i:i+2,j:j+2); M = mean(reshape(mask,[],1)); % Mean V = var(reshape(mask,[],1)); output = (mask(5)-((noise_variance/V)*(mask(5)-M))); % Output if output < 0 output = 0;

Page 4: E E 458 Project 003

end if output > 1 output = 1; end Output_R(i+1,j+1) = output; % Insert Output endend

% Green Value Plane Loopfor i = 1:Status.Height for j = 1:Status.Width mask = G(i:i+2,j:j+2); M = mean(reshape(mask,[],1)); V = var(reshape(mask,[],1)); output = (mask(5)-((noise_variance/V)*(mask(5)-M))); if output < 0 output = 0; end if output > 1 output = 1; end Output_G(i+1,j+1) = output; endend

% Blue Value Plane Loopfor i = 1:Status.Height for j = 1:Status.Width mask = B(i:i+2,j:j+2); M = mean(reshape(mask,[],1)); V = var(reshape(mask,[],1)); output = (mask(5)-((noise_variance/V)*(mask(5)-M))); if output < 0 output = 0; end if output > 1 output = 1; end Output_B(i+1,j+1) = output; endendOutput = uint8(cat(3,Output_R*256,Output_G*256,Output_B*256));imtool(Output);

Page 5: E E 458 Project 003

Figure 1: Noisy Input

Figure 2: Adaptive Filter Output

Page 6: E E 458 Project 003

Analysis:

The code breaks down like so:

1. Read-In the Image2. Add Noise to the Image3. Create a Zero Border around the perimeter of the image4. Isolate the Red, Green and Blue Planes5. Convert to type single or double6. Define the 3-by-3 mask7. Fill the mask with the noisy image values8. Determine the variance and mean of the mask9. Apply the formula (highlighted in yellow)10. Set the center value of the mask equal to the output of the formula and apply to a similar size

zero matrix11. Repeat for the Green and Blue Planes12. Concatenate the three planes to form the final output13. Display the Input and Output images for comparison

Discussion:

It was difficult to get this filter to produce a clear output despite several different approaches. First, we tried converting RGN to HSV format but found out that this type of filter did not work at all on the Hue and Saturation planes of the images; furthermore, it produced a very inconsistent smearing of the Gaussian noise across the V planes of the HSV images. The second attempt consisted of simply converting the RGB image to grayscale and changing the datatype to single or double so that the var function could work. The result was not great. Finally, we tried to make some adjustments to the code to try and find some errors like substituting the output of the filter into a zero matrix instead of substituting it directly into the input image (even though that has worked in previous filtering methods such as median filtering). The result turned out much better than the original but the input image still appeared to be much clearer than the output image which shows that the filter was in fact ineffective.

Conclusion:

In conclusion, the point of an adaptive filter is to use the dynamic variables of an image, such as the statistical data, to alter an image to produce a cleaner output. For the purposes of this experiment, it did not turn out so well. You can see the results in Figs. 1 and 2. Also, I have added the results of previous computer programs as well as the result of using the built-in MatLab function specifically designed for removing Gaussian noise.

Page 7: E E 458 Project 003

Previous Attempts:

% Name: Chad Weiss% Date: March 25, 2016% Project: E E 458 Project 003% Instructor: Dr. Aldo Morales % This program implements an adaptive filtering technique that uses the% statistical means and variances of the image to reduce gaussian noise. clear all;clc; % Read-In Image I = imread('Neighbors.jpg'); Status = imfinfo('Neighbors.jpg'); % Add Noise to Imagenoise_variance = 0.01;J = imnoise(I,'gaussian',0,noise_variance); % Zero BorderInput = [zeros(1,Status.Width,3);J;zeros(1,Status.Width,3)];Input = [zeros(Status.Height+2,1,3),Input,zeros(Status.Height+2,1,3)];imtool(Input); % Noisy r,g and b planes (RGB format)r = Input; r(:,:,2:3)=0;g = Input; g(:,:,1)=0; g(:,:,3)=0; b = Input; b(:,:,1:2)=0; % Noisy r planes (HSV format)r_hsv = rgb2hsv(r); r_v = r_hsv(:,:,3); % Noisy g planes (HSV format)g_hsv = rgb2hsv(g); g_v = g_hsv(:,:,3); % Noisy b planes (HSV format)b_hsv = rgb2hsv(b); b_v = b_hsv(:,:,3); % Define the Filter Maskmask = zeros(3,3); for i = 1:Status.Height for j = 1:Status.Width mask = r_v(i:i+2,j:j+2); M = mean(reshape(mask,[],1)); % Mean V = var(reshape(mask,[],1)); % Variance output = (mask(5)-((noise_variance/V)*(mask(5)-M))); % Output r_v(i+1,j+1) = output; % Insert Output

Page 8: E E 458 Project 003

endend% Green Value Plane Loopfor i = 1:Status.Height for j = 1:Status.Width mask = g_v(i:i+2,j:j+2); M = mean(reshape(mask,[],1)); V = var(reshape(mask,[],1)); output = (mask(5)-((noise_variance/V)*(mask(5)-M))); g_v(i+1,j+1) = output; endend% Blue Value Plane Loopfor i = 1:Status.Height for j = 1:Status.Width mask = b_v(i:i+2,j:j+2); M = mean(reshape(mask,[],1)); V = var(reshape(mask,[],1)); output = (mask(5)-((noise_variance/V)*(mask(5)-M))); b_v(i+1,j+1) = output; endend Output = uint8(cat(3,(r_v*256),(g_v*256),(b_v*256)));imtool(Output);

Figure 3: Noisy Input Image

Page 9: E E 458 Project 003

Figure 4: Adaptive Filter Output Image

Using the MatLab filter:

% Name: Chad Weiss% Date: March 25, 2016% Project: E E 458 Project 003% Instructor: Dr. Aldo Morales % This program implements an adaptive filtering technique that uses the% statistical means and variances of the image to reduce gaussian noise. clear all;clc; % Read-In Image I = imread('Neighbors.jpg'); Status = imfinfo('Neighbors.jpg'); % Add Noise to Imagenoise_variance = 0.03;J = imnoise(I,'gaussian',0,noise_variance); % Zero BorderInput = [zeros(1,Status.Width,3);J;zeros(1,Status.Width,3)];Input = [zeros(Status.Height+2,1,3),Input,zeros(Status.Height+2,1,3)];imtool(Input); % Noisy r,g and b planes (RGB format)r = Input; r(:,:,2:3)=0;g = Input; g(:,:,1)=0; g(:,:,3)=0;

Page 10: E E 458 Project 003

b = Input; b(:,:,1:2)=0; R = double(r(:,:,1))/256; imtool(R);G = double(g(:,:,2))/256; imtool(G);B = double(b(:,:,3))/256; imtool(B); % Define the Filter Maskmask = zeros(3,3); % Define Output ImageOutput_R = wiener2(R);Output_G = wiener2(G);Output_B = wiener2(B); % Display Output Imageimtool(Output_R); imtool(Output_G); imtool(Output_B);Output = uint8(cat(3,Output_R*256,Output_G*256,Output_B*256));imtool(Output);

Figure 5: Noisy Input Image

Page 11: E E 458 Project 003

Figure 6: Noisy Input Red

Figure 7: Noisy Input Green

Page 12: E E 458 Project 003

Figure 8: Noisy Input Blue

Figure 9: Filtered Red

Page 13: E E 458 Project 003

Figure 10: Filtered Green

Figure 11: Filtered Blue

Page 14: E E 458 Project 003

Figure 12: Filtered Output