clear all
%一,图像的预处理,读入彩色图像将其灰度化
PS=imread('p1.bmp'); %读入JPG彩色图像文件
% imshow(PS) %显示出来
% title(' 1 输入的彩色bmp图像')
% imwrite(rgb2gray(PS),'PicSampleGray.bmp'); %将彩色图片灰度化并保存
% PS=rgb2gray(PS); %灰度化后的数据存入数组
%二,绘制直方图
[m,n]=size(PS); %测量图像尺寸参数
GP=zeros(1,256); %预创建存放灰度出现概率的向量
for k=0:255
GP(k+1)=length(find(PS==k))/(m*n); %计算每级灰度出现的概率,将其存入GP中相应位置
end
% figure,bar(0:255,GP,'g') %绘制直方图
% title(' 2 原图像直方图')
% xlabel('灰度值')
% ylabel('出现概率')
%三,直方图均衡化
S1=zeros(1,256);
for i=1:256
for j=1:i
S1(i)=GP(j)+S1(i); %计算Sk
end
end
S2=round((S1*256)+0.5); %将Sk归到相近级的灰度
for i=1:256
GPeq(i)=sum(GP(find(S2==i))); %计算现有每个灰度级出现的概率
end
% figure,bar(0:255,GPeq,'b') %显示均衡化后的直方图
% title('3 均衡化后的直方图')
% xlabel('灰度值')
% ylabel('出现概率')
%四,图像均衡化
PA=PS;
for i=0:255
PA(find(PS==i))=S2(i+1); %将各个像素归一化后的灰度值赋给这个像素
end
% figure,imshow(PA) %显示均衡化后的图像
% title('4 均衡化后图像')
imwrite(PA,'p2.bmp');
%五,纸张前景提取
% I=imread('p2.bmp');
% bg=imopen(I,strel('disk',15));
% figure,imshow(bg);
% I1=imsubtract(I,bg);
% figure,imshow(I1);
% I2=imadjust(I1,stretchlim(I1),[0 1]);
% figure,imshow(I2);
%
org = imread('p2.bmp'); %读取图片
gray = rgb2gray(org);
gradL=imfilter(gray, [-1,0,1;-2,0,2;-1,0,1]);
gradR=imfilter(gray, [1,0,-1;2,0,-2;1,0,-1]);
for x=1+5:2048
avgG = 0.0;
for y=1:1536
avgG = avgG + double(gradL(y,x));
end
avgG = avgG/1536;
if avgG > 40.0
lEdge = x;
break;
end
end
for x=2048-5:-1:1
avgG = 0.0;
for y=1:1536
avgG = avgG + double(gradR(y,x));
end
avgG = avgG/1536;
if avgG > 40.0
rEdge = x;
break;
end
end
%
% proc = imcrop(gray, [lEdge-10, 1, rEdge-lEdge+10, 1536]); %截取部分图像
% imshow(proc);
gray(:,1:lEdge-20)=0;
gray(:,rEdge+20:end)=0;
imshow(gray);
imwrite(gray,'p3.bmp');
proc=gray;
%图像加强
eq = histeq(proc);
%图像直方图
% imhist(eq);
gradL = imfilter(eq,[-3,0,3;-10,0,10;-3,0,3]); % 0° Sobel算子
gradR = imfilter(eq,[3,0,-3;10,0,-10;3,0,-3]); %180° Sobel算子
%bL 显示效果是比较理想的
bL = im2bw(gradL, graythresh(gradL)); % 转换成二进制图像
bR = im2bw(gradR, graythresh(gradR));
imshow(bL);
bL1=bL(1:200,1:2048);
figure,imshow(bL1);
imwrite(bL1,'p4.bmp');
%六,纸张图像空域高斯滤波
%
%七,使用 Sobel算子 直接边缘检测 同时图像二值化
%
% PB=edge(I2,'sobel');
% figure,imshow(PB)
% title('使用Sobel算子处理后的二值图像')
% imwrite(PB,'PicTow.bmp');
%
%八,边缘融合
%九,形态修复
% %腐蚀亮点
% A=imread('p4.bmp');
% figure,imshow(A);
% SE = strel('rectangle',[3 3]);
%
% B=imerode(A,SE);
% figure,imshow(B);
%
%
% %膨胀边缘
%消除长横条
se1 = strel('rectangle',[1 3]);
%消除短竖条
se2 = strel('rectangle',[4 1]);
bL1 = imerode(bL,se1);
bR1 = imerode(bR,se1);
bL1 = imerode(bL1,se2);
bR1 = imerode(bR1,se2);
se4 = strel('rectangle',[3 1]);
bL1 = imdilate(bL1,se4);
bR1 = imdilate(bR1,se4);
%膨胀
se3 = strel('rectangle',[26 1]);
bL1 = imdilate(bL1,se3);
bR1 = imdilate(bR1,se3);
se4 = strel('square',1); %disk
se5 = strel('diamond',1); %disk
bL1 = imopen(bL1,se4);
bL1 = imclose(bL1,se4);
bL1 = imclose(bL1,se5);
bR1 = imopen(bR1,se4);
bR1 = imclose(bR1,se4);
bR1 = imclose(bR1,se5);
bL1 = bwmorph(bL1,'open');
bR1 = bwmorph(bR1,'open');
bL1 = bwmorph(bL1,'close');
bR1 = bwmorph(bR1,'close');
bL1 = bwmorph(bL1,'hbreak');
bR1 = bwmorph(bR1,'hbreak');
bL1 = bwmorph(bL1,'fill');
bR1 = bwmorph(bR1,'fill');
bL1 = bwmorph(bL1,'clean');
bR1 = bwmorph(bR1,'clean');
bL1 = bwmorph(bL1,'spur');
bR1 = bwmorph(bR1,'spur');
bL1 = bwmorph(bL1,'thin');
bR1 = bwmorph(bR1,'thin');
bL1 = bwmorph(bL1,'hbreak');
bR1 = bwmorph(bR1,'hbreak');
for i = 18:4:26
se6 = strel('rectangle',[i 1]); %disk
se7 = strel('diamond',1);
bL1 = imopen(bL1,se6);
bL1 = imclose(bL1,se6);
bL1 = imclose(bL1,se7);
bR1 = imopen(bR1,se6);
bR1 = imclose(bR1,se6);
bR1 = imclose(bR1,se7);
end
bL1 = bwmorph(bL1,'spur');
bR1 = bwmorph(bR1,'spur');
bL1 = bwmorph(bL1,'hbreak');
bR1 = bwmorph(bR1,'hbreak');
%消除断裂处
se8= strel('rectangle',[30 1]);
% %消除短竖条
% se2 = strel('rectangle',[4 1]);
bL1 = imdilate(bL1,se8);
% figure,imshow(bL1);
%
% imwrite(bL1,'p9.bmp');
%九,计数
A=bL1(1+30:200+30,1:2048);
figure,imshow(A);
k=zeros(200,1);%200行
for i=1:200
k(i,1)=0;
for j=1:2047
if A(i,j)==1 & A(i,j+1)==0
k(i,1)=k(i,1)+1;
end
end
end
k=fix(sum(k)./200)