How can i implement the conservative smoothing filter using the ordfilt2() function

3 次查看(过去 30 天)
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’);

回答(3 个)

Abhishek Ballaney
Abhishek Ballaney 2018-2-23
https://in.mathworks.com/help/images/ref/ordfilt2.html
  1 个评论
Gideon Oluniran
Gideon Oluniran 2018-2-23
yh thanks. I went through that page earlier. that's why I have this line in my codes: Es_cons=ordfilt2(S,9,[111;101;111],’symmetric’). this certainly ranks values in the 3x3 neighbourhood; excluding the pixel at row 2 -column 2 (i.e. [111;101;111]), and picks the maximum value in the rank to replace the target pixel.My question is.....how do I tell matlab to compare the value of the pixel at row 2 -column 2 in the 3x3 neighbourhood and compare with the maximum and minimum values in the ranked neighbourhood values. if it's greater than the maximum, it's replaced by the maximum value. if it's lesser than the minimum value, it's replaced by the minimum value. else it's value remains unchanged.

请先登录,再进行评论。


sajad farokhi
sajad farokhi 2018-4-6
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
Sorinel Oprisan 2020-4-12
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);
=======

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by