matlab练习程序(TopHat操作)

clear all;
close all;
clc;

img=imread('lena.jpg');
img=im2bw(img,graythresh(img));
imshow(img)

[height width]=size(img);

img_re=zeros(height,width);
temp=[];
for i=2:height-1                 %腐蚀
    for j=2:width-1
        temp=img(i-1:i+1,j-1:j+1);
        img_re(i,j)=min(temp(:));     
    end
end

for i=2:height-1                %膨胀  
    for j=2:width-1
        temp=img_re(i-1:i+1,j-1:j+1);
        img_re(i,j)=max(temp(:));     
    end
end

img=double(img);
img_re=img-img_re;  %原图像减开操作后的图像,还有一种是闭操作后的图像减原图像
figure,imshow(mat2gray(img_re));
原文地址:https://www.cnblogs.com/tiandsp/p/2576918.html