闲来无事,做的一个小东西。效果图:
既然能检测裂缝,那是不是……本来想开个车,奈何代码有bug,又不想改,就算了吧
代码如下:
srcImg=imread('3.jpg'); %图片读取
subplot(3,4,1);
imshow(srcImg);
title('原始图像');
srcImg=rgb2gray(srcImg); %灰度图
subplot(3,4,2);
imshow(srcImg);
cropImg = imcrop(srcImg); %裁剪图片
title('灰度图');
subplot(3,4,3);
imshow(cropImg);
title('裁剪图像');
adjImg=imadjust(cropImg,[0.40,0.75],[0,1]); %区域增强
subplot(3,4,4);
imshow(adjImg);
title('区域增强');
H = fspecial('gaussian',2,2); %滤波去噪
filImg = imfilter(adjImg,H,'replicate');
subplot(3,4,5);
imshow(filImg);
title('滤波去噪');
bwImgBig=im2bw(filImg,70/255); %对灰度图像进行两次不同大小阈值的二值化,再把两次产生的二值化图像进行而至重构。
subplot(3,4,6);
imshow(~bwImgBig);
title('大阈值');
bwImgSmall=im2bw(filImg,30/255);
subplot(3,4,7);
imshow(~bwImgSmall);
title('小阈值');
recImg=imreconstruct(~bwImgSmall,~bwImgBig); %去除图像边框干扰
[m,n]=size(recImg);
recImg(1,1:n)=0;
recImg(m,1:n)=0;
recImg(1:m,1)=0;
recImg(1:m,n)=0;
subplot(3,4,8);
imshow(recImg);
title('去除图像边框干扰');
%去除小于10的区域
recImg=bwareaopen(recImg,10); %Bwareaopen函数是去除像素点少于十的连通区域
subplot(3,4,9);
imshow(recImg);
title('去除小于10的区域' );
%mark
[r,c]=size(recImg);
mark1=21;
col1=recImg(mark1,:);
col1=find(col1==1);
col1=round(length(col1)/2)+col1(1);
subplot(3,4,10);
imshow(recImg);
hold on;
plot(col1,mark1, 'o','MarkerSize',20,'MarkerEdgeColor','r');
mark2=round(r/2);
col2=recImg(mark2,:);
col2=find(col2==1);
col2=round(length(col2)/2)+col2(1);
plot(col2,mark2, 'o','MarkerSize',20,'MarkerEdgeColor','r');
mark3=r-20;
col3=recImg(mark3,:);
col3=find(col3==1);
col3=round(length(col3)/2)+col3(1);
plot(col3,mark3, 'o','MarkerSize',20,'MarkerEdgeColor','r');
%计算宽度
[r,c]=size(recImg);
num=length(find(recImg==1));
avgPixel=num/m;
%普通对话框
h=dialog('name','宽度(pixel)','position',[700 500 200 70]);
uicontrol('parent',h,'style','text','string',avgPixel,'position',[10 40 120 20],'fontsize',12);
uicontrol('parent',h,'style','pushbutton','position',[80 10 50 20],'string','确定','callback','delete(gcbf)');