转载请注明来自:黄朝辉的博客
>> finger_noise=imread('finger_noise.jpg');
>> gray_finger_noise=rgb2gray(finger_noise);
%均值滤波
>> gray_finger_avg3=filter2(fspecial('average',3),gray_finger_noise)/255; %模板尺寸为3
>> gray_finger_avg7=filter2(fspecial('average',7),gray_finger_noise)/255; %模板尺寸为7
>> imwrite(gray_finger_avg3,'gray_finger_avg3.jpg');
>> imwrite(gray_finger_avg7,'gray_finger_avg7.jpg');
%第一次中值滤波
>> gray_finger_med=medfilt2(gray_finger_noise);
%第二次中值滤波
>> gray_finger_med2=medfilt2(gray_finger_med);
%第三次中值滤波
>> gray_finger_med3=medfilt2(gray_finger_med2);
>> figure;imshow(gray_finger_noise);
>> figure;imshow(gray_finger_med3);
>> imwrite(gray_finger_med3,'经三次中值滤波.jpg');
>> gray_finger_hist=histeq(gray_finger_med3);
>> imshow(gray_finger_hist);
f=imread('finger_noise.jpg');
count=0;
T=mean2(f);
done=false;
while ~done
count=count+1;
g=f>T;
Tnext=0.5*(mean(f(g))+mean(f(~g)));
done=abs(T-Tnext)<0.5;
T=Tnext;
end
g=im2bw(f,T/255);
imshow(g);
建立openoperation.m文件,内容为:
function rst=openoperation(src,struct_element)
% 开操作
tmp_erode=imerode(src,struct_element);
rst=imdilate(tmp_erode,struct_element);
开始处理
finger_noise = imread('finger_noise.jpg');
subplot(1,5,1);
imshow(finger_noise);
title('原图');
struct_element1=[1 1 1;1 1 1;1 1 1];
finger1=openoperation(finger_noise,struct_element1);
subplot(1,5,2);
imshow(finger1);
imwrite(finger1, 'finger1.jpg');
title('图1');
struct_element2=[1 1 ;1 1];
finger2=openoperation(finger_noise,struct_element2);
subplot(1,5,3);
imshow(finger2);
imwrite(finger2, 'finger2.jpg');
title('图2');
struct_element3=[1 0 1;0 1 0;1 0 1];
finger3=openoperation(finger_noise,struct_element3);
subplot(1,5,4);
imshow(finger3);
imwrite(finger3, 'finger3.jpg');
title('图3');
struct_element4=[1 1 1;1 0 0;1 0 0];
finger4=openoperation(finger_noise,struct_element4);
subplot(1,5,5);
imshow(finger4);
imwrite(finger4, 'finger4.jpg');
title('图4');
finger1.jpg 结构元素为:struct_element1=[1 1 1;1 1 1;1 1 1];
finger2.jpg 结构元素为:struct_element2=[1 1 ;1 1];
finger3.jpg 结构元素为:struct_element3=[1 0 1;0 1 0;1 0 1];
finger4.jpg 结构元素为:struct_element4=[1 1 1;1 0 0;1 0 0];
效果比较:
图片 | 结构元素 | 效果 |
---|---|---|
finger1.jpg | [1 1 1;1 1 1;1 1 1] | 较好 |
finger2.jpg | [1 1 ;1 1] | 差 |
finger3.jpg | [1 0 1;0 1 0;1 0 1] | 较好 |
finger4.jpg | [1 1 1;1 0 0;1 0 0] | 中等 |
f=imread('finger_noise.jpg');
f=rgb2gray(f);
struct_element1=[1 1 1;1 1 1;1 1 1];
%开操作
f=openoperation(f,struct_element1);
subplot(2,2,1);
imshow(f);
title('(a)原始图像(经形态学处理)');
subplot(2,2,2);
f=double(f);
hv=fspecial('prewitt');
hh=hv.';
gv=abs((imfilter(f,hv,'replicate')));
gh=abs(imfilter(f,hh,'replicate'));
g=sqrt(gv.^2+gh.^2);
g2=imopen(imclose(g,ones(5,5)),ones(5,5));
subplot(2,2,2);
L=watershed(g2); %分水岭算法
wr=L==0; %取出边缘
imshow(wr);
title('(b)分水岭');
f(wr)=255;
subplot(2,2,3);
imshow(uint8(f));
title('(c)分割结果');
rm=imregionalmin(g);
subplot(2,2,4);
imshow(rm);
title('(d)局部最小值');