{"id":33141,"date":"2025-06-23T06:48:26","date_gmt":"2025-06-23T06:48:26","guid":{"rendered":"https:\/\/gaviki.com\/blog\/?p=33141"},"modified":"2025-06-23T06:48:27","modified_gmt":"2025-06-23T06:48:27","slug":"an-noisy-image-can-be-denoised-using-several-different-filters","status":"publish","type":"post","link":"https:\/\/gaviki.com\/blog\/an-noisy-image-can-be-denoised-using-several-different-filters\/","title":{"rendered":"An noisy image can be denoised using several different filters"},"content":{"rendered":"\n<p>An noisy image can be denoised using several different filters. In class, we discussed both the averaging and the median filter. \u2022 Write a Matlab script to denoise the images peppersNoisel.tiff and peppersNoise2.tiff using both the median filter and the averaging filter. Examine the effect of using different filter window sizes including 3 \u00d7 3 pixels and 5 \u00d7 5 pixels. From your results, what are the advantages or disadvantages of each filter? What are the advantages or disadvantages of changing the filter size? Include your denoised images in your report. \u2022 In many cases, edges must be extracted from a noisy image. If this is necessary, it is important to denoise the image first. Write a Matlab script that generates an edgemap from the 3 \u00d7 3 averaging filtered and median filtered versions of peppersNoisel.tiff. Create the edgemap by first calculating the image&#8217;s gradient using the Sobel filters<\/p>\n\n\n\n<p>(4) to approximate the gradient in the row and column directions, then by comparing the magnitude of the gradient to a threshold. Use the same threshold for both images when generating the edgemap. Please comment your code and append it to your report. What difference do you see between the edgemaps of each filtered image? Which filter has better edge preserving properties?<\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">The Correct Answer and Explanation is:<\/mark><\/strong><\/p>\n\n\n\n<p>Below is the correct MATLAB script for denoising the images <code>peppersNoisel.tiff<\/code> and <code>peppersNoise2.tiff<\/code> using both median and averaging filters with different window sizes (3\u00d73 and 5\u00d75). It also generates edgemaps after filtering <code>peppersNoisel.tiff<\/code> using Sobel filters for edge detection. The code is fully commented for clarity.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">MATLAB Code:<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">matlabCopyEdit<code>% Read the noisy images\nimage1 = imread('peppersNoisel.tiff');\nimage2 = imread('peppersNoise2.tiff');\n\n% Display original noisy images\nfigure, imshow(image1), title('Original PeppersNoisel.tiff');\nfigure, imshow(image2), title('Original PeppersNoise2.tiff');\n\n% Apply 3x3 median filter\nmed3_image1 = medfilt2(image1, [3 3]);\nmed3_image2 = medfilt2(image2, [3 3]);\n\n% Apply 5x5 median filter\nmed5_image1 = medfilt2(image1, [5 5]);\nmed5_image2 = medfilt2(image2, [5 5]);\n\n% Apply 3x3 averaging filter\navg3_filter = fspecial('average', [3 3]);\navg3_image1 = imfilter(image1, avg3_filter, 'replicate');\navg3_image2 = imfilter(image2, avg3_filter, 'replicate');\n\n% Apply 5x5 averaging filter\navg5_filter = fspecial('average', [5 5]);\navg5_image1 = imfilter(image1, avg5_filter, 'replicate');\navg5_image2 = imfilter(image2, avg5_filter, 'replicate');\n\n% Display filtered images\nfigure, imshow(med3_image1), title('Median Filter 3x3 - PeppersNoisel');\nfigure, imshow(med5_image1), title('Median Filter 5x5 - PeppersNoisel');\nfigure, imshow(avg3_image1), title('Averaging Filter 3x3 - PeppersNoisel');\nfigure, imshow(avg5_image1), title('Averaging Filter 5x5 - PeppersNoisel');\n\nfigure, imshow(med3_image2), title('Median Filter 3x3 - PeppersNoise2');\nfigure, imshow(med5_image2), title('Median Filter 5x5 - PeppersNoise2');\nfigure, imshow(avg3_image2), title('Averaging Filter 3x3 - PeppersNoise2');\nfigure, imshow(avg5_image2), title('Averaging Filter 5x5 - PeppersNoise2');\n\n%% Edge Detection using Sobel filters on PeppersNoisel after filtering\n\n% Apply 3x3 median filter\nmed3_image = medfilt2(image1, [3 3]);\n% Apply 3x3 averaging filter\navg3_filter = fspecial('average', [3 3]);\navg3_image = imfilter(image1, avg3_filter, 'replicate');\n\n% Compute gradients using Sobel filters\nsobel_x = fspecial('sobel'); % Horizontal gradient filter\nsobel_y = sobel_x';          % Vertical gradient filter\n\n% Gradient of median filtered image\ngrad_x_med = imfilter(double(med3_image), sobel_x, 'replicate');\ngrad_y_med = imfilter(double(med3_image), sobel_y, 'replicate');\ngradient_mag_med = sqrt(grad_x_med.^2 + grad_y_med.^2);\n\n% Gradient of averaging filtered image\ngrad_x_avg = imfilter(double(avg3_image), sobel_x, 'replicate');\ngrad_y_avg = imfilter(double(avg3_image), sobel_y, 'replicate');\ngradient_mag_avg = sqrt(grad_x_avg.^2 + grad_y_avg.^2);\n\n% Threshold for edge detection\nthreshold = 50;\n\n% Generate edge maps\nedges_med = gradient_mag_med &gt; threshold;\nedges_avg = gradient_mag_avg &gt; threshold;\n\n% Display edge maps\nfigure, imshow(edges_med), title('Edge Map - Median Filtered Image');\nfigure, imshow(edges_avg), title('Edge Map - Averaging Filtered Image');\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation <\/h3>\n\n\n\n<p>The noisy images were denoised using two common techniques: median filtering and averaging filtering. The median filter replaces each pixel with the median value in its neighborhood, which is effective in removing salt-and-pepper noise while preserving edges. The averaging filter computes the mean of the surrounding pixels, which smoothens the image but tends to blur edges.<\/p>\n\n\n\n<p>Both filters were applied using 3\u00d73 and 5\u00d75 window sizes. The 3\u00d73 filters are less aggressive, preserving more image detail, while 5\u00d75 filters provide stronger noise reduction at the cost of more smoothing and potential loss of fine details.<\/p>\n\n\n\n<p>For edge detection, it is essential to reduce noise first because noise can create false edges. Sobel filters were used to compute image gradients in both horizontal and vertical directions. The magnitude of the gradient highlights regions with significant intensity changes, which typically correspond to edges. A threshold of 50 was applied to identify the edges.<\/p>\n\n\n\n<p>The edge map from the median filtered image showed better edge preservation. Median filtering effectively suppresses noise while maintaining sharp edges, making the edges more distinct. In contrast, the averaging filter smoothed the image but also blurred the edges, making them less prominent in the edge map.<\/p>\n\n\n\n<p>Overall, the median filter is better for edge preservation and noise removal in images with salt-and-pepper noise. The averaging filter is suitable for reducing Gaussian-like noise but may lead to loss of edge sharpness. Increasing the filter size boosts noise reduction but can degrade image detail and edges, so the filter size should be chosen based on the specific application.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"852\" height=\"1024\" src=\"https:\/\/gaviki.com\/blog\/wp-content\/uploads\/2025\/06\/learnexams-banner8-502.jpeg\" alt=\"\" class=\"wp-image-33146\" srcset=\"https:\/\/gaviki.com\/blog\/wp-content\/uploads\/2025\/06\/learnexams-banner8-502.jpeg 852w, https:\/\/gaviki.com\/blog\/wp-content\/uploads\/2025\/06\/learnexams-banner8-502-250x300.jpeg 250w, https:\/\/gaviki.com\/blog\/wp-content\/uploads\/2025\/06\/learnexams-banner8-502-768x923.jpeg 768w\" sizes=\"auto, (max-width: 852px) 100vw, 852px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>An noisy image can be denoised using several different filters. In class, we discussed both the averaging and the median filter. \u2022 Write a Matlab script to denoise the images peppersNoisel.tiff and peppersNoise2.tiff using both the median filter and the averaging filter. Examine the effect of using different filter window sizes including 3 \u00d7 3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-33141","post","type-post","status-publish","format-standard","hentry","category-quiz-questions"],"_links":{"self":[{"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/posts\/33141","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/comments?post=33141"}],"version-history":[{"count":1,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/posts\/33141\/revisions"}],"predecessor-version":[{"id":33147,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/posts\/33141\/revisions\/33147"}],"wp:attachment":[{"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/media?parent=33141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/categories?post=33141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/tags?post=33141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}