Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

E E 458 Project 002

95 views

Published on

  • Be the first to comment

  • Be the first to like this

E E 458 Project 002

  1. 1. PENNSYLVANIASTATE UNIVERSITY Digital Image Processing Project 2 Using median filter theory to create a similar function Chad RyanWeiss 3/8/2016
  2. 2. Abstract: This report contains information on median filter theory as well as some of the basic MatLab functions and code (written by author) that demonstrates the practicality of such a digital image processing filter.
  3. 3. Theory: Median filtering is a common filtering technique used in digital image processing, often used to eliminate unwanted noise in any given image. This type of filter works best for removing salt & pepper noise (which basically consists of black and white specks appearing throughout the image). To operate, the filter must:  Create a square, odd-numbered matrix (mask or window).  Fill the mask with the values from the original noisy image.  Calculate the median value of the mask.  Replace the centermost cell of the mask with the median.  Proceed to the next row or column and repeat. Figure 1: Median Filtering If you refer to Fig. 1 shown above, you will notice that we begin with an image, an intensity matrix consisting of rows and columns each filled with pixel values used to designate the strength or magnitude of the color of the cell (top). Next you will see the mask, which is a square, three-by-three dimensional matrix that has focused itself upon one particular area within the image (center). The mask captures the values of the image and sorts them in ascending order into a data array, which can then be used to determine the median value of the mask (bottom). Once that has been completed, the median value will replace the center value of the mask which will imprint itself upon the original image thus completing the cycle. It will shift and repeat the same process until the entire image has been filtered. This is how the median filter operates.
  4. 4. MatLab: The code below shows a MatLab based computer program used to median filter a RGB color image. It takes no more than 180 seconds to filter a 1045-by-585-by-3 dimensional image. % Name: Chad Weiss % Date: March 8, 2016 % Instructor: Dr. Morales % Program: This program is a median filter clear all % Image I = imread('Focus.jpg'); Status = imfinfo('Focus.jpg'); % Noisy Image J = imnoise(I, 'salt & pepper', 0.1); figure; imshow(J); title('Noisy Image'); % Image Conversions Jhsv = rgb2hsv(J); Jhsv0 = [zeros(1,Status.Width,3);Jhsv;zeros(1,Status.Width,3)]; Jhsv0 = [zeros(Status.Height+2,1,3),Jhsv0,zeros(Status.Height+2,1,3)]; Jh = Jhsv0(:,:,1); Js = Jhsv0(:,:,2); Jv = Jhsv0(:,:,3); % Apply Filter for i = 1:Status.Height for j = 1:Status.Width mask = Jh(i:i+2,j:j+2); med = median(sort(transpose(single(mask(1:9))))); Jh(i+1,j+1) = med; end end % Apply Filter for i = 1:Status.Height for j = 1:Status.Width mask = Js(i:i+2,j:j+2); med = median(sort(transpose(single(mask(1:9))))); Js(i+1,j+1) = med; end end % Apply Filter for i = 1:Status.Height for j = 1:Status.Width mask = Jv(i:i+2,j:j+2); med = median(sort(transpose(single(mask(1:9))))); Jv(i+1,j+1) = med; end end F = hsv2rgb(cat(3,Jh,Js,Jv)); figure; imshow(F); title('Filtered Image'); You can see the results on the following page.
  5. 5. Figure 2: Noisy Image Figure 3: Filtered Image (using Median Filtering)
  6. 6. The code used above was written by the author of this report. There are functions in MatLab that perform similar operations such as the medfilt2 function. See the code below for a demonstration of this function. clear all % Image I = imread('Focus.jpg'); Status = imfinfo('Focus.jpg'); % Noisy Image J = imnoise(I, 'salt & pepper', 0.1); figure; imshow(J); title('Noisy Image'); % Image Conversions Jhsv = rgb2hsv(J); Jhsv0 = [zeros(1,Status.Width,3);Jhsv;zeros(1,Status.Width,3)]; Jhsv0 = [zeros(Status.Height+2,1,3),Jhsv0,zeros(Status.Height+2,1,3)]; Jh = Jhsv0(:,:,1); Js = Jhsv0(:,:,2); Jv = Jhsv0(:,:,3); % Apply Filters Ih = medfilt2(Jh); Is = medfilt2(Js); Iv = medfilt2(Jv); % Image Conversions output = hsv2rgb(cat(3, Ih,Is,Iv)); % Display Image figure; imshow(output); title('Filtered Image'); You can see that the image has been filtered from the original in Fig. 2; however, Fig. 3 appears to have a much better filter than Fig. 4. Figure 4: Filtered Image (using medfilt2)
  7. 7. Analysis/Observations: Upon coding the filter, it was noticed that the filtering techniques (both MatLab’s and the author’s) were ineffective against three dimensional images; i.e., images including height, width and depth, suggesting multiple planes. To overcome this challenge it was necessary to convert the RGB (Red, Green, Blue) image into HSV (Hue, Saturation, Value) format; furthermore, it was necessary to create variables holding the individual planes H, S and V, for they are only two dimensional images containing height and width. After isolating the planes, it was possible to apply the filters individually (to H, S and V), eliminating any unwanted noise. Lastly, to recombine the three, two-dimensional filtered images into a three-dimensional filtered image, we simply concatenated the three planes and converted it back to RGB format. Conclusion: In conclusion, the hand-written median filter took about three minutes to run. In comparison, the pre-installed, built-in MatLab functions took only about three seconds to run; however, the quality of the image is much better the first time around for the hand- written version. For an explanation of the code, please refer to Appendix A.
  8. 8. Appendix A: Code Variables If you refer to Fig. 5, you will see the list of variables used in running the median filter computer program. I: This variable contains the original image. J: This variable contains the noisy image. Figure 5: List of Variables
  9. 9. Jhsv: This variable contains the hsv noisy image. Jhsv0: This variable contains the hsv noisy image w/ a zero border (1 pixel black border). Jh: This variable contains the h-plane image of the Jhsv0 image.
  10. 10. Js: This variable contains the s-plane image of the Jhsv0 image. Jv: This variable contains the v-plane image of the Jhsv0 image.
  11. 11. Status: This variable contains the original image I information. mask: This variable is the 3x3x3 dimensional mask used in filtering the noisy images. med: This variable contains the median value of the mask. i: This is a row counter variable. j: This is a column counter variable. F: This variable contains the filtered image.
  12. 12. Functions: imread(_): Reads-in an image imfinfo(_): Reads-in image information imshow(_): Displays an image imnoise(_): Adds noise to an image rgb2hsv(_): Converts a RGB image to HSV format hsv2rgb(_): Converts a HSV image to RGB format single(_): Converts data to the datatype single transpose(_): Transposes matrices sort(_): Sorts any matrix or array in ascending order median(_): Determines the median value of any matrix or array cat(_): Concatenates arrays help: See above

×