【瑕疵检测】基于matlab瓶盖瑕疵检测【含Matlab源码 730期】

200 阅读4分钟

一、简介

基于matlab瓶盖瑕疵检测

二、源代码

%*********初始化********
clc; clear all; close all;

%*********图像预处理********
I=imread('11.jpg');%读入标准模板图像
J=rgb2gray(I);%转化为灰度图片
subplot(241),imshow(I);title('原图');%显示图片

K=imread('qx1.jpg');
M=rgb2gray(K);
subplot(242),imshow(K);title('缺陷图');
%*********为得到更好的效果,可适当的采用图片增强,直方图均衡化,平滑,锐化等操作********

%********图像配准,以处理有旋转现象的图片********

%***(一)差影法,去噪 最终结果变量 L

L=imabsdiff(J,M);%两张图片相减
subplot(243),imshow(L);title('相减后灰度图L');

%***(二)图像增强两种方法 最终结果变量 L1 L2 使用变量L
L1=imadjust(L,[0.1 1], [0 1]);
subplot(244),imshow(L1);title('灰度变换增强对比度L1');
%subplot(275),imhist(L1);title('直方图灰度变换增强对比度L1');

[L2,T]=histeq(L,64);% 直方图均衡化,图像灰度扩展到0~255,但是只有64个灰度级
%subplot(276),imshow(L2);title('直方图均匀化L2');
%subplot(277),imhist(L2);title('直方图均匀化后的直方图L2');
%subplot(278),plot((0:255)/255,T);title('转移函数曲线L2'); 

%***(三)图像滤波三种方法 ,中值滤波VS均值滤波VS维纳滤波,选取效果最佳的。最终结果变量 B1 B2 B3 
%****中值滤波****
B1=medfilt2(L1,[5 5]);  %中值滤波,窗口大小为5×5
%subplot(279),imshow(B1);title('二维中值滤波效果B1');

%***均值滤波***
M4=[0 1 0; 1 0 1; 0 1 0];
M4=M4/4;% 4邻域平均滤波
B2=filter2(M4,L1);
subplot(2,4,5),imshow(B2);title('均值滤波B2');

%*****Wiener****
[B3 noise]=wiener2(L1, [5 5]);%窗口大小为5×5
%subplot(2,7,11),imshow(B3);title('自适应魏纳滤波B3');   

%***(四)5种不同的梯度增强法进行图像锐化

%***(五)图像的高通滤波

%********图像分割********
%***(六)图像的阈值分割 中间结果变量 C1 C2 最终结果变量 C11 C12 
%subplot(2,7,12),imhist(B1);title('B1滤波后灰度直方图');% 观察灰度直方图,灰度1根据直方图的谷确定阈值,40处有谷,确定阈值T=140
C1=im2bw(B1,0.1/255); %im2bw函数需要将灰度值转换到[0,1]范围内
subplot(2,4,6),imshow(C1);title('0.1 C1阈值分割结果');
C11=double(C1);
%figure,imhist(B1);title('图像的阈值分割直方图');
%figure,imshow(C1);title('图像的阈值结果');

%阈值迭代法分割  
ZMax=max(max(B2));
ZMin=min(min(B2));
TK=(ZMax+ZMin)/2;
bCal=1;
iSize=size(I);    %图像大小
while(bCal)
    iForeground=0;
    iBackground=0;    %定义前景和背景数
    ForegroundSum=0;
    BackgroundSum=0;   %定义前景和背景的灰度总和
    for i=1:iSize(1)
        for j=1:iSize(2)
            tmp=I(i,j);
            if(tmp>=TK)
                iForeground=iForeground + 1;
                ForegroundSum=ForegroundSum + double(tmp);
            else
                iBackground=iBackground + 1;
                BackgroundSum=BackgroundSum + double(tmp);
            end
        end
    end
    ZO = ForegroundSum/iForeground;
    ZB = BackgroundSum/iBackground;    %计算前景和背景的平均值
    TKTmp = uint8((ZO+ZB)/2);
    if(TKTmp==TK)
        bCal = 0;
    else
        TK = TKTmp;
    end            %当前阈值不再变化,说明迭代结束
end
disp(strcat('迭代后的阈值:',num2str(TK)));
C2=im2bw(B2,double(TK)/255);
C12=double(C2);
subplot(2,4,7),imshow(C12);title('阈值迭代法分割结果C12');

%se = strel('line',11,90);
%C111=imdilate(C12,se);
%C121=imerode(C111,se);

%***(七)图像边缘检测 最终结果变量 D1 D2 D3 D4 sobel,canny,prewitt等
%D1=edge(C11,'log',0.003,2); % σ=2
%figure,imshow(D1);title('边缘检测 log σ=2'); 
%D2=edge(C11,'canny',0.2);
%figure,imshow(D2);title('边缘检测 canny算子');
%D3=edge(C121,'log',0.003,2); % σ=2
%figure,imshow(D3);title('迭代法 边缘检测 log σ=2'); 
D4=edge(C12,'canny',0.2);
figure,imshow(D4);title('存在缺陷');
imwrite(D4,'F:\毕业设计\毕业程序设计\缺陷图片1.jpg')%存入图片

[row, column] = size(D4);
num = 0;
for i = 1:row
    for j = 1:column
        if(D4(i,j)==1)
            num = num +1;
            rec(:,num) = [i;j];
        end
    end
    f=imread('qx3.jpg');%读入图片
%subplot(2,2,1);imshow(f)
f1=im2double(f);%将真彩图片转化为double型
I=rgb2gray(f);%将真彩图像转化为灰度图像
%subplot(2,2,2);imshow(I)
%subplot(2,2,3);imhist(I);%显示直方图
p=edge(I,'sobel');
%subplot(2,2,4);imshow(p);
imwrite(I,'C:\Users\lenovo\Desktop\f9ffefca4217d2bbf043243db84a4b86\瑕疵检测\灰度1.jpg')
h4=medfilt2(p,[1,1]);%用3*3的模板进行中值滤波

三、运行结果

在这里插入图片描述
在这里插入图片描述

四、备注

版本:2014a