閒來無事,做的一個小東西。效果圖:
既然能檢測裂縫,那是不是……本來想開個車,奈何代碼有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)');