How can i implement the conservative smoothing filter using the ordfilt2() function
Ältere Kommentare anzeigen
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’);
Antworten (3)
Abhishek Ballaney
am 23 Feb. 2018
0 Stimmen
https://in.mathworks.com/help/images/ref/ordfilt2.html
1 Kommentar
Gideon Oluniran
am 23 Feb. 2018
sajad farokhi
am 6 Apr. 2018
0 Stimmen
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
1 Kommentar
Gideon Oluniran
am 7 Apr. 2018
Sorinel Oprisan
am 12 Apr. 2020
0 Stimmen
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);
=======
Kategorien
Mehr zu Image Category Classification finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!