%%n为你想得到的IMF的个数
c = x('% copy of the input signal (as a row vector)
N = length(x)-
% loop to decompose the input signal into n successive IMFs
imf = []% Matrix which will contain the successive IMF, and the residuefor t=1:n
% loop on successive IMFs
%-------------------------------------------------------------------------
% inner loop to find each imf
h = c% at the beginning of the sifting process, h is the signal
SD = 1% Standard deviation which will be used to stop the sifting process
while SD >0.3 % while the standard deviation is higher than 0.3 (typical value) %%筛选停止准则
% find local max/min points
d = diff(h)% approximate derivative %%求各点导数
maxmin = []% to store the optima (min and max without distinction so far)
for i=1:N-2
if d(i)==0% we are on a zero %%导数为0的点,即”驻点“,但驻点不一定都是极值点,如y=x^3的x=0处
if sign(d(i-1))~=sign(d(i+1)) % it is a maximum %%如果驻点两侧的导数异号(如一边正,一边负),那么该点为极值点
maxmin = [maxmin, i]%%找到极值点在信号中的坐标(不分极大值和极小值点)
end
elseif sign(d(i))~=sign(d(i+1)) % we are straddling a zero so%%如y=|x|在x=0处是极值点,但该点倒数不存在,所以不能用上面的判
断方法
maxmin = [maxmin, i+1] % define zero as at i+1 (not i) %%这里提供了另一类极值点的判断方法
end
end
if size(maxmin,2) <2 % then it is the residue %%判断信号是不是已经符合残余分量定义
break
end
% divide maxmin into maxes and mins %% 分离极大值点和极小值点
if maxmin(1)>maxmin(2) % first one is a max not a min
maxes = maxmin(1:2:length(maxmin))
mins = maxmin(2:2:length(maxmin))
else% is the other way around
maxes = maxmin(2:2:length(maxmin))
mins = maxmin(1:2:length(maxmin))
end% make endpoints both maxes and mins
maxes = [1 maxes N]
mins = [1 mins N]
%------------------------------------------------------------------------- % spline interpolate to get max and min envelopesform imf
maxenv = spline(maxes,h(maxes),1:N) %%用样条函数插值拟合所有的极大值点
minenv = spline(mins, h(mins),1:N)%%用样条函数插值拟合所有的极小值点
m = (maxenv + minenv)/2% mean of max and min enveloppes %%求上下包络的均值
prevh = h% copy of the previous value of h before modifying it %%h为分解前的信号
h = h - m% substract mean to h %% 减去包络均值
% calculate standard deviation
eps = 0.0000001% to avoid zero values
SD = sum ( ((prevh - h).^2) ./ (prevh.^2 + eps) )%% 计算停止准则
end
imf = [imfh]% store the extracted IMF in the matrix imf
% if size(maxmin,2)<2, then h is the residue
% stop criterion of the algo. if we reach the end before n
if size(maxmin,2) <2
break
end
c = c - h% substract the extracted IMF from the signal
end
return
clcclear allclose 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
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)