% grapple2.m applies median filters until no further change occurs G = imread('grapple.gif'); % read the .gif file G = G(300:700,1:401); % only deal with a small part of image clf subplot(3,2,1) imshow(G,[0 1]); % display the image with 2 gray levels title('Original image') drawnow for i=2:6, q = 2*i-1; % size of the window to filter with c = 1; % number of iterations H = G; J = medfilt2(H,[q q],'symmetric'); while sum(sum(H~=J)) > 0, % while changes occur c = c + 1; H = J; J = medfilt2(J,[q q],'symmetric'); % filter again end K(:,:,i) = J; % store the final images subplot(3,2,i) imshow(J,[0 1]); title([int2str(q) ' by ' int2str(q) ' median filter, applied ' int2str(c) ' times']); drawnow end