代码只贴出了边缘检测以后,至于切割出单个的字母,可根据验证码的实际情况进行切割。
主要是聚类分割、中值滤波去噪,二值化,边缘检测。
clc,clear;
[I,map]=imread('0.jpg');
figure(1);
subplot(231);imshow(I,map);title('原始图像');
[y,x,z]=size(I);
d1=zeros(y,x);
d2=d1;
myI=double(I);
I0=zeros(y,x);
for i=1:x
for j=1:y
d1(j,i)=sqrt((myI(j,i,1)-10)^2+(myI(j,i,2)-10)^2+(myI(j,i,3)-10)^2) ;
d2(j,i)=sqrt((myI(j,i,1)-180)^2+(myI(j,i,2)-180)^2+(myI(j,i,3)-180)^2) ;
if d1(j,i)>= d2(j,i)
I0(j,i)=1;
end
end
end
subplot(232);imshow(I0);title('聚类分割后的图像');
I2=medfilt2(I0,[2 2]);
subplot(233);imshow(I2);title('b 中值滤波');
I2=im2bw(I2,0.5);
subplot(234);imshow(I2);title('二值化');
eal = 'canny';
BW=edge(I2,eal,[0.093 0.095],1);
subplot(235);imshow(BW);title(strcat(eal,'边缘检测后图像'));