图像处理_条纹检测

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

1.图片”条纹.bmp”中包一定间隔的条纹,要求测量条纹的间隔(以像素为尺度)

和方向(假设单个像元为方形,及长度和宽度一致)。

matlab程序

% 条纹处理2010年6月25日

%% 打开图片

[FileName,PathName] = uigetfile({'*.bmp','BMP文件(*.bmp)';'*.*', ...

'All Files (*.*)'}, 'Select the Target-file');

file=[PathName,FileName];

if isequal(FileName,0)

return;

else

g=imread (file);

figure(1); %原图

imshow(g);

I=rgb2gray(g); %灰度化

J=I;

[row,col]=size(I);

Bi=im2bw(J,graythresh(J)); %二值化graythresh(J):Otsu方法确定阈值[J8,n8]=bwlabel(Bi,8); %贴标签

%% 去除边沿非整条纹

for i = 1:row

if J8(i,1)~=0

temp= J8(i,1);

[r,c]=find(J8(i,:)==temp);

J8(i,c)=0;

end

if J8(i,col)~=0

temp= J8(i,col);

[r,c]=find(J8(i,:)==temp);

J8(i,c)=0;

end

end

figure(2);

imshow(J8);

%% 条纹中线

JL=zeros(size(J8));

for i = 1:row

for j=1:n8

[r,c]=find(J8(i,:)==j);

if(~isempty(c))

JL(i,round(sum(c)/length(c)))=j;

end

end

end

figure(3);

imshow(JL);

%% 计算倾角间距

i=1;

for j=1:n8

[r,c]=find(JL==j);

if(~isempty(c))

p(i,:)=polyfit(c,r,1); %拟合

i=i+1;

end

end

Slope=sum(p(:,1))/length(p(:,1)); %斜率

if Slope<0

Inclination=-atan(Slope)/pi*180; %倾角

else

Inclination=180-atan(Slope)/pi*180;

end

myL=p(:,2);

gap=0;

lhalf=round(length(p(:,2))/2);

for i=lhalf+1:length(p(:,2))

gap=gap+myL(i)-myL(i-lhalf);

end

gap=(gap/(i*lhalf))*sin(Inclination/180*pi);

str1=['条纹间隔:' num2str(gap) '像素\n'];

str2=['条纹与水平夹角:' num2str(Inclination) '度']; msgbox([str1;str2]);

end

%% 保存图片

% [file,path] = uiputfile({'*.bmp','BMP文件(*.bmp)';'*.*', ... % 'All Files (*.*)'},'Save file name');

% PF=[path,file];

% if isequal(file,0)

% return;

% else

% imwrite(JL,PF,'bmp');

% end

相关文档
最新文档