How can i implement the conservative smoothing filter using the ordfilt2() function
6 ビュー (過去 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’);
0 件のコメント
回答 (3 件)
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
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);
=======
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Image Filtering and Enhancement についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!