How can i implement the conservative smoothing filter using the ordfilt2() function
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (3)
sajad farokhi
le 6 Avr 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
le 12 Avr 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 commentaires
Voir également
Catégories
En savoir plus sur Image Filtering and Enhancement dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!