I=imread('a.jpg'); %读取图片
I1=rgb2gray(I); %转化为灰度图像
I2=edge(I1,'roberts',0.09,'both'); %采用robert算子进行边缘检测
se=[1;1;1]; %线型结构元素
I3=imerode(I2,se); %腐蚀图像
se=strel('rectangle',[25,25]); %正方形结构元素
I4=imclose(I3,se); %图像聚类、填充图像
I5=bwareaopen(I4,2000); %去除聚团灰度值小于2000的部分
[y,x,]=size(I5);
I6=double(I5);
%%%%%%%%%%%%%%%%%%%%%%%%%%
%绘制行曲线图
Y1=zeros(y,1);
for i=1:y
for j=1:x
if(I6(i,j,1)==1)
Y1(i,1)= Y1(i,1)+1;
end
end
end
figure();
subplot(1,3,1);
plot(0:y-1,Y1),title('行像素灰度值累计'),xlabel('行值'),ylabel('像素和');
[temp, MaxY]=max(Y1);
PY1=MaxY;
while ((Y1(PY1,1)>=80)&&(PY1>1))
PY1=PY1-7;
end
PY2=MaxY;
while ((Y1(PY2,1)>=80)&&(PY2<y))
PY2=PY2+7;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%绘制列曲线图
X1=zeros(1,x);
for j=1:x
for i=PY1:PY2
if(I6(i,j,1)==1)
X1(1,j)= X1(1,j)+1;
end
end
end
subplot(1,3,2);
plot(0:x-1,X1),title('列像素灰度值累计'),xlabel('列值'),ylabel('像数和');
PX1=1;
while ((X1(1,PX1)<3)&&(PX1<x))
PX1=PX1+4;
end
PX2=x;
while ((X1(1,PX2)<3)&&(PX2>PX1))
PX2=PX2-4;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%
DW=I(PY1:PY2,PX1:PX2,:);
subplot(1,3,3);
imshow(DW),title('文字定位后图像');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 文字图像转为灰度图像
I1 = rgb2gray(DW);
%% 二值化文字区域图像
I1 = im2bw(I1,graythresh(I1));%二值化图像
I2 = bwareaopen(I1,16);%去除小于16像素的区块
figure();
subplot(1,2,1);
imshow(I2),title('二值化文字区域图像');
%%%%%%
%% 分割字符按行积累量
[y,x]=size(I2);
I3=double(I2);
X1=zeros(1,x);
for j=1:x
for i=1:y
if(I3(i,j,1)==1)
X1(1,j)= X1(1,j)+1;
end
end
end
subplot(1,2,2);
plot(0:x-1,X1),title('文字区域图像列像素点累计'),xlabel('列值'),ylabel('像素和');
%%%%%%%%%%
%% 分割字符
Px0=1;
Px1=1;
figure();
for i=1:5
while ((X1(1,Px0)<3)&&(Px0<x))
Px0=Px0+1;
end
Px1=Px0;
while ((((Px1<x)&&X1(1,Px1)>=9))||((Px1-Px0)<55))
if(Px1<301)
Px1=Px1+1;
else
break;
end
end
Z=I2(:,Px0:Px1,:);
switch strcat('Z',num2str(i))
case 'Z1'
PIN0=Z;
case 'Z2'
PIN1=Z;
case 'Z3'
PIN2=Z;
case 'Z4'
PIN3=Z;
%case 'Z5'
% PIN4=Z;
%case 'Z6'
% PIN5=Z;
otherwise
PIN6=Z;
end
subplot(1,5,i);
imshow(Z);
Px0=Px1;
end
matlab.zip_图像 聚类_图像填充_图像检测 填充_图像聚类
版权申诉
68 浏览量
2022-09-20
21:09:14
上传
评论
收藏 1KB ZIP 举报
朱moyimi
- 粉丝: 64
- 资源: 1万+