Using Euclidean Distance in Resizable Filter Window for Noise Reduction
3 views (last 30 days)
Show older comments
Mohammed Abdul Wadood
on 26 Oct 2022
Commented: Mohammed Abdul Wadood
on 3 Jun 2025
Hi.
I am trying to generate an resizable matrix (3×3), (5×5), ..., etc. Used as a kernel that sliding on a an image from right to left and from top to bottom ( I can sliding this kernel by using conv2 function ). But I want to use the euclidean distance to calculate the distances between the central pixel and the rest of pixels inside this window or kernel and then replace the central pixel with the closest pixel to the center according to euclidean distance.
If I define the kernel as the for loop below, how can I create euclidean distance function for this purpose?
n = inpute('inter the value of n');
for i = 1:n
for j=1:n
%%%% using ecladian distinct function to calculate distance between center and other window pixels
end
end
and then using this kernel to remove gaussian noise as shown below
Original_Image = imread('any color image');
Noisy_Image = imnoise(Original_Image, 'gaussian', 0, 0.01);
Noise_R = Noisy_Image(:,:,1);
Noise_G = Noisy_Image(:,:,2);
Noise_B = Noisy_Image(:,:,3);
New_R = conv2((Noise_R), Kernel, 'same');
New_G = conv2((Noise_G), Kernel, 'same');
New_B = conv2((Noise_B), Kernel, 'same');
New_Image = cat(3, New_R, New_G, New_B);
montage({Original_Image, Noisy_Image, New_Image})
thanks a lot
0 Comments
Accepted Answer
Ayush Aniket
on 3 Jun 2025
To replace the central pixel with the closest pixel based on Euclidean distance, you can find the pixel with the minimum distance and assign it to the center. Once you have the kernel, you can apply it to remove Gaussian noise using conv2. Refer the script below:
% Define kernel size
n = 3;
kernel = zeros(n, n);
center = ceil(n/2); % Central pixel index
% Compute Euclidean distance from center
for i = 1:n
for j = 1:n
kernel(i, j) = sqrt((i - center)^2 + (j - center)^2);
end
end
% Normalize kernel values
kernel = kernel / max(kernel(:));
% I am using a random image from internet
Original_Image = imread("https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png");
Noisy_Image = imnoise(Original_Image, 'gaussian', 0, 0.01);
% Apply kernel to each color channel
Noise_R = Noisy_Image(:,:,1);
Noise_G = Noisy_Image(:,:,2);
Noise_B = Noisy_Image(:,:,3);
New_R = conv2(Noise_R, kernel, 'same');
New_G = conv2(Noise_G, kernel, 'same');
New_B = conv2(Noise_B, kernel, 'same');
% Combine channels and display results
New_Image = cat(3, New_R, New_G, New_B);
montage({Original_Image, Noisy_Image, New_Image});
More Answers (0)
See Also
Categories
Find more on Image Data Workflows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!