请高手解释下这个matlab程序
这是个提取图像上文字上的程序,我对matlab不怎么懂,请高手详细解释下每句的意思,最好详细点,本人是初学者。比较长,先说声谢谢了。clc;clearall;closea...
这是个提取图像上文字上的程序,我对matlab不怎么懂,请高手详细解释下每句的意思,最好详细点,本人是初学者。比较长,先说声谢谢了。
clc;clear all;close all;
[FileName,PathName,FilterIndex]=uigetfile('C:\Users\zhaobo\Desktop\001.jpg');
im=[PathName FileName];
I1=imread(im);
figure,imshow(I1)
gray=rgb2gray(I1);
%figure,imshow(gray)
he=double(im2bw(gray,200/256));
figure,imshow(he)
BW = edge(he);
%BW = edge(he,'zerocross');
BW=double(im2bw(BW,200/256));
figure,imshow(BW)
%figure,imhist(gray)
[r_max c_max]=size(gray);
m=zeros(r_max,c_max);
for i=1:r_max
n=BW(i,:);
p=findpeaks(n);
count=sum(p);
if (count>8)
m(i,:)=n;
end
end
%figure,imshow(m)
mm=m;
for i=1:c_max
n=BW(:,i);
p=findpeaks(n);
count=sum(n);
if (count>5)||(count<3)
mm(:,i)=zeros(1:200,1);
end
end
mm=double(im2bw(mm,200/256));
B=ones(3);
D=imdilate(mm,B);
C=imerode(D,B,3);
补充locate方程......
function [up, down, left, right]=locate(g)
[m,n]=size(g);
for countm=1:1:m, %%%%%%up
for countn=1:1:n,
if g(countm,countn)==1,
up=countm;
break;
end
end
if countn~=n,
break
end
countn=1;
end
for countm=m:-1:1,%%%%%down
for countn=1:1:n,
if g(countm,countn)==1,
down=countm;
break;
end
end
if countn~=n,
break
end
countn=1;
end
for countn=1:1:n, %%%%%%left
for countm=1:1:m,
if g(countm,countn)==1,
left=countn;
break;
end
end
if countm~=m,
break
end
countm=1;
end
for countn=n:-1:1, %%%%%%right
for countm=1:1:m,
if g(countm,countn)==1,
right=countn;
break;
end
end
if countm~=m,
break
end
countm=1;
end
%C = medfilt2(C);
C=double(im2bw(C,200/256));
%figure,imshow(C)
[up, down, left, right]=locate(C);
sel=imcrop(gray,[left-15,up-5,right-left+25,down-up+10]);
figure,imshow(sel)
不好意思,最后一段,就是%C = medfilt2(C);
C=double(im2bw(C,200/256));
%figure,imshow(C)
[up, down, left, right]=locate(C);
sel=imcrop(gray,[left-15,up-5,right-left+25,down-up+10]);
figure,imshow(sel)
是在主程序里,复制时候搞错了 展开
clc;clear all;close all;
[FileName,PathName,FilterIndex]=uigetfile('C:\Users\zhaobo\Desktop\001.jpg');
im=[PathName FileName];
I1=imread(im);
figure,imshow(I1)
gray=rgb2gray(I1);
%figure,imshow(gray)
he=double(im2bw(gray,200/256));
figure,imshow(he)
BW = edge(he);
%BW = edge(he,'zerocross');
BW=double(im2bw(BW,200/256));
figure,imshow(BW)
%figure,imhist(gray)
[r_max c_max]=size(gray);
m=zeros(r_max,c_max);
for i=1:r_max
n=BW(i,:);
p=findpeaks(n);
count=sum(p);
if (count>8)
m(i,:)=n;
end
end
%figure,imshow(m)
mm=m;
for i=1:c_max
n=BW(:,i);
p=findpeaks(n);
count=sum(n);
if (count>5)||(count<3)
mm(:,i)=zeros(1:200,1);
end
end
mm=double(im2bw(mm,200/256));
B=ones(3);
D=imdilate(mm,B);
C=imerode(D,B,3);
补充locate方程......
function [up, down, left, right]=locate(g)
[m,n]=size(g);
for countm=1:1:m, %%%%%%up
for countn=1:1:n,
if g(countm,countn)==1,
up=countm;
break;
end
end
if countn~=n,
break
end
countn=1;
end
for countm=m:-1:1,%%%%%down
for countn=1:1:n,
if g(countm,countn)==1,
down=countm;
break;
end
end
if countn~=n,
break
end
countn=1;
end
for countn=1:1:n, %%%%%%left
for countm=1:1:m,
if g(countm,countn)==1,
left=countn;
break;
end
end
if countm~=m,
break
end
countm=1;
end
for countn=n:-1:1, %%%%%%right
for countm=1:1:m,
if g(countm,countn)==1,
right=countn;
break;
end
end
if countm~=m,
break
end
countm=1;
end
%C = medfilt2(C);
C=double(im2bw(C,200/256));
%figure,imshow(C)
[up, down, left, right]=locate(C);
sel=imcrop(gray,[left-15,up-5,right-left+25,down-up+10]);
figure,imshow(sel)
不好意思,最后一段,就是%C = medfilt2(C);
C=double(im2bw(C,200/256));
%figure,imshow(C)
[up, down, left, right]=locate(C);
sel=imcrop(gray,[left-15,up-5,right-left+25,down-up+10]);
figure,imshow(sel)
是在主程序里,复制时候搞错了 展开
2个回答
展开全部
clc;clear all;close all;
[FileName,PathName,FilterIndex]=uigetfile('C:\Users\zhaobo\Desktop\001.jpg');
im=[PathName FileName];%%获取文件路径和文件名
I1=imread(im);%%根据路径和文件名读取图片到I1
figure,imshow(I1)%%显示I1
gray=rgb2gray(I1);%%灰度化
%figure,imshow(gray)
he=double(im2bw(gray,200/256));%用200/256这个阈值对灰度化图像进行阈值分割得到二值图像;然后转化为double类型
figure,imshow(he)
BW = edge(he);%%边缘检测得到二值图像边缘,也就是文字边缘
%BW = edge(he,'zerocross');
BW=double(im2bw(BW,200/256));%%重新二值化,转化为double
figure,imshow(BW)
%figure,imhist(gray)
[r_max c_max]=size(gray);%%获取图片高宽
m=zeros(r_max,c_max);%%建立图片等大小全零矩阵
for i=1:r_max
n=BW(i,:);
p=findpeaks(n);%%找出每一行的峰值
count=sum(p);%%峰值求和
if (count>8)
m(i,:)=n;%%保存有峰值的行到m
end
end
%figure,imshow(m)
mm=m;
for i=1:c_max%%此段和上面原理一样,求列的峰值
n=BW(:,i);
p=findpeaks(n);
count=sum(n);
if (count>5)||(count<3)
mm(:,i)=zeros(1:200,1);%%存在峰值的列保存到mm中
end
end
mm=double(im2bw(mm,200/256));%%对列峰值进行二值化
B=ones(3);%%B为[1 1 1]的转置
D=imdilate(mm,B);%%利用结构元素B膨胀
C=imerode(D,B,3);%%利用3行3列的结构元素腐蚀,膨胀腐蚀的过程是为了连通断线,使文字区域变成整块
补充locate方程......
function [up, down, left, right]=locate(g)
[m,n]=size(g);
for countm=1:1:m, %%%%%%up%%寻找文字形成的方块的最高点坐标
for countn=1:1:n,
if g(countm,countn)==1,
up=countm;%%记录最高点
break;
end
end
if countn~=n,
break
end
countn=1;
end
for countm=m:-1:1,%%%%%down
for countn=1:1:n,
if g(countm,countn)==1,%%寻找最底点的坐标
down=countm;
break;
end
end
if countn~=n,
break
end
countn=1;
end
for countn=1:1:n, %%%%%%left
for countm=1:1:m,
if g(countm,countn)==1,
left=countn;%%寻找最左坐标
break;
end
end
if countm~=m,
break
end
countm=1;
end
for countn=n:-1:1, %%%%%%right
for countm=1:1:m,
if g(countm,countn)==1,%%寻找最右坐标
right=countn;
break;
end
end
if countm~=m,
break
end
countm=1;
end
[FileName,PathName,FilterIndex]=uigetfile('C:\Users\zhaobo\Desktop\001.jpg');
im=[PathName FileName];%%获取文件路径和文件名
I1=imread(im);%%根据路径和文件名读取图片到I1
figure,imshow(I1)%%显示I1
gray=rgb2gray(I1);%%灰度化
%figure,imshow(gray)
he=double(im2bw(gray,200/256));%用200/256这个阈值对灰度化图像进行阈值分割得到二值图像;然后转化为double类型
figure,imshow(he)
BW = edge(he);%%边缘检测得到二值图像边缘,也就是文字边缘
%BW = edge(he,'zerocross');
BW=double(im2bw(BW,200/256));%%重新二值化,转化为double
figure,imshow(BW)
%figure,imhist(gray)
[r_max c_max]=size(gray);%%获取图片高宽
m=zeros(r_max,c_max);%%建立图片等大小全零矩阵
for i=1:r_max
n=BW(i,:);
p=findpeaks(n);%%找出每一行的峰值
count=sum(p);%%峰值求和
if (count>8)
m(i,:)=n;%%保存有峰值的行到m
end
end
%figure,imshow(m)
mm=m;
for i=1:c_max%%此段和上面原理一样,求列的峰值
n=BW(:,i);
p=findpeaks(n);
count=sum(n);
if (count>5)||(count<3)
mm(:,i)=zeros(1:200,1);%%存在峰值的列保存到mm中
end
end
mm=double(im2bw(mm,200/256));%%对列峰值进行二值化
B=ones(3);%%B为[1 1 1]的转置
D=imdilate(mm,B);%%利用结构元素B膨胀
C=imerode(D,B,3);%%利用3行3列的结构元素腐蚀,膨胀腐蚀的过程是为了连通断线,使文字区域变成整块
补充locate方程......
function [up, down, left, right]=locate(g)
[m,n]=size(g);
for countm=1:1:m, %%%%%%up%%寻找文字形成的方块的最高点坐标
for countn=1:1:n,
if g(countm,countn)==1,
up=countm;%%记录最高点
break;
end
end
if countn~=n,
break
end
countn=1;
end
for countm=m:-1:1,%%%%%down
for countn=1:1:n,
if g(countm,countn)==1,%%寻找最底点的坐标
down=countm;
break;
end
end
if countn~=n,
break
end
countn=1;
end
for countn=1:1:n, %%%%%%left
for countm=1:1:m,
if g(countm,countn)==1,
left=countn;%%寻找最左坐标
break;
end
end
if countm~=m,
break
end
countm=1;
end
for countn=n:-1:1, %%%%%%right
for countm=1:1:m,
if g(countm,countn)==1,%%寻找最右坐标
right=countn;
break;
end
end
if countm~=m,
break
end
countm=1;
end
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询