How can i implement the conservative smoothing filter using the ordfilt2() function
8 views (last 30 days)
Show older comments
so I'm running this set of commands and i got stuck on how to implement the ordfilt2() command. i'm running a ranking filter (presicely the conservative smoothing filter). what i intend to do? first I'm using a 3x3 neighbourhood. The filter ranks values of the neighbourhood (leaving out the pixel in the second row-third column i.e the target pixel), it then compares if this pixel value is greater than the maximum value in the neighbourhood. if yes, it replaces the value in this pixel with the maximum value. otherwise it compares with the minimum value. if lesser than the minimum, it replaces with the minimum value.
I'm having afeeling the 'for' loop will be useful here. but can anyone give me a helping hand?
A=imread(‘cameraman.tif’); figure, imshow(A); %Display original image title(‘original image’); S=imnoise(A,‘salt & pepper’,0.03); %Add 3% (0.03) salt and pepper noise Es_cons=ordfilt2(S,9,[111;101;111],’symmetric’);
0 Comments
Answers (3)
sajad farokhi
on 6 Apr 2018
Use the following function:
function y1 = conserv(xng) % example: %a=im2double(imread('rice.png'));b= conserv(a); % Copyright: DIGITAL IMAGE PROCESSING An Algorithmic Approach with MATLAB ® by % Uvais Qidwai and C. H. Chen [r,c] = size(xng) ; x1 = zeros(r+2,c+2) ; x1(2:r+1,2:c+1,:) = xng(:,:) ; [r,c] = size(x1) ; y1 = x1 ; for i = 2 : r-1 for j = 2 : c-1 nh = [x1(i-1,j-1) x1(i-1,j) x1(i-1,j+1) x1(i,j-1) x1(i,j) x1(i,j+1) x1(i+1,j-1) x1(i+1,j) x1(i+1,j+1)] ; cp = x1(i,j) ; mx = max(nh) ; mn = min(nh) ; if (cp > mx) cp = mx ; elseif (cp < mn) cp = mn ; end end y1(i,j)= cp ; end end
Sorinel Oprisan
on 12 Apr 2020
Here is a working version. The previous version did not work because it included the central pixel x(i,j) in the neighbourhood to be checked (which basically nullifies the idea of the algorithm).
====
function y1 = conserv(xng)
% example:
%a=im2double(imread('rice.png'));b= conserv(a);
% Copyright: DIGITAL IMAGE PROCESSING
% An Algorithmic Approach with MATLAB ® by
% Uvais Qidwai and C. H. Chen
[r,c] = size(xng);
x1 = zeros(r+2,c+2) ;
x1(2:r+1,2:c+1) = xng(:,:);
[r,c] = size(x1);
y1 = x1 ;
for i = 2 : r-1
for j = 2 : c-1
nh = [x1(i-1,j-1) x1(i-1,j) x1(i-1,j+1) ...
x1(i,j-1) x1(i,j+1) x1(i+1,j-1) ...
x1(i+1,j) x1(i+1,j+1)] ;
cp = x1(i,j) ;
mx = max(nh) ;
mn = min(nh) ;
if (cp > mx)
cp = mx ;
elseif (cp < mn)
cp = mn ;
end
y1(i,j)= cp ;
end
end
y1=y1(2:end-1,2:end-1);
=======
0 Comments
See Also
Categories
Find more on Image Filtering and Enhancement 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!