所以我有一张半导体晶片的图像,它有一个缺陷,我需要使用 Matlab 检测它。我应该只检测它而不是它的背景。我还需要测量它的周长和面积。
到目前为止,我有这段代码,我将原始图像转换为二进制图像,然后对其使用膨胀,然后尝试获取其轮廓。在获取周长和面积时,我不仅会收到缺陷的结果,还会收到不是我想要的图像其余部分的结果。我怎样才能只提取缺陷,所以我只能得到它的面积和参数。
fig3 = imread('figure3.png');
imshow(fig3);
title('Original image', 'FontSize', 18);
%Gray image
fig3Gray = rgb2gray(fig3);
%Binary image
BW3 = imbinarize(fig3Gray,0.5);
imshow(BW3);
title('Binary image', 'FontSize', 18);
se3 = strel('square',5);
%Dilation image
dilated3 = imdilate(BW3,sr);
imshow(dilated3);
title('Dilated image', 'FontSize', 18);
minus3 = ~(BW3-dilated3);
imshow(minus3);
title('Contour image', 'FontSize', 18);
imshowpair(minus3,BW3,'montage');
%Perimeter and Area calculation
Coon3 = bwconncomp(~BW3)
ANS3 = length(Coon3.PixelIdxList{1});
OUTPUT3 = regionprops(Coon3,'Perimeter','Area');
P3 = OUTPUT3.Perimeter
Area3 = OUTPUT3.Area
Run Code Online (Sandbox Code Playgroud)
让我们从读取图像并将其转换为二进制开始。请注意,我降低了阈值以消除不需要的细节。
clear; close all; clc
fig3 = imread('XEQ59.png');
imshow(fig3);
title('Original image', 'FontSize', 18);
%Gray image
fig3Gray = rgb2gray(fig3);
%Binary image
BW3 = imbinarize(fig3Gray, 0.2); % lowered threshold
figure; imshow(BW3)
title('binary image')
Run Code Online (Sandbox Code Playgroud)
现在我们继续寻找缺陷的坐标。为此,我们定义了一个结构元素来定义所需的形状se。
我们正在寻找图像中与 匹配的部分se。对于要匹配的给定坐标,周围区域必须恰好为se。
请注意,这里的灰度值被忽略,它们可以是白色或黑色。
se手动定义,其中1代表白色,-1代表黑色,0代表忽略的像素。
% hit-miss
se = [1, 1, -1*ones(1,5), ones(1, 3); ...
ones(6,1), -1*ones(6), zeros(6,2), ones(6,1); ...
ones(3,2), zeros(3,1), -1*ones(3,6), ones(3,1)];
figure; imshow(uint8(255/2*(se+1)), 'InitialMagnification', 3000)
title('structuring element')
Run Code Online (Sandbox Code Playgroud)
应用命中-未命中操作来找到缺陷的位置:
pos = bwhitmiss(BW3, se);
figure; imshow(pos)
title('position of defect')
input('Press enter to continue...')
Run Code Online (Sandbox Code Playgroud)
既然我们有了位置,我们就增加那个特定的像素位置,直到它不再增长,以获得缺陷。
% get the defect
close all; clc
def = pos;
last_def = zeros(size(def));
while ~isequal(def, last_def)
last_def = def;
def = ~BW3 & imdilate(def, ones(3));
imshow(def)
title('defect')
pause(0.1)
end
Run Code Online (Sandbox Code Playgroud)
计算面积和周长:
% area
area = sum(def(:))
% perimeter
vert = imdilate(def, [1; 1; 1]) - def;
horz = imdilate(def, [1 1 1]) - def;
perimeter = sum(vert(:)) + sum(horz(:))
Run Code Online (Sandbox Code Playgroud)
area =
102
perimeter =
54
Run Code Online (Sandbox Code Playgroud)
这个问题比你之前的问题要困难得多。
请阅读评论:
clear
fig3 = imread('figure3.png');
fig3Gray = rgb2gray(fig3);
fig3Gray = im2double(fig3Gray); %Convert from uint8 to double (MATLAB math works in double).
%figure;imshow(fig3Gray);
%Apply median filter with large radius.
Med = medfilt2(fig3Gray, [51, 51], 'symmetric');
%figure;imshow(Med);
D = abs(fig3Gray - Med);
%figure;imshow(D);impixelinfo
BW = imbinarize(D, 0.3);
%figure;imshow(BW);impixelinfo
Coon = bwconncomp(BW);
fig3GrayMasked = fig3Gray;
%Cover the tall clusters and the long clusters.
for i = 1:length(Coon)
C = Coon.PixelIdxList{i}; %Cluster coordinates.
[Y, X] = ind2sub(size(fig3Gray), C); %Convert to x,y indices.
is_tall = (max(Y) - min(Y)) > 50; %true if cluster is tall.
is_wide = (max(X) - min(X)) > 50; %true if cluster is wide.
%Replace tall and long clusters by pixels from median image.
if ((is_tall) || (is_wide))
fig3GrayMasked(C) = Med(C);
end
end
%figure;imshow(fig3GrayMasked);impixelinfo
%Second iteration: search largest cluster on fig3GrayMasked image.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Med = medfilt2(fig3GrayMasked, [51, 51], 'symmetric');
D = abs(fig3GrayMasked - Med);
%figure;imshow(D);impixelinfo
BW = imbinarize(D, 0.3);
%figure;imshow(BW);impixelinfo
Coon = bwconncomp(BW);
%Find index of largest cluster in list of clusters Coon.PixelIdxList
[~, i] = max(cellfun(@numel, Coon.PixelIdxList));
%Get the indices of the largest cluster
C = Coon.PixelIdxList{i};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Paint cluster in yellow color (just for fun).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
BW = zeros(size(BW), 'logical');
BW(C) = 1;
Y = im2uint8(cat(3, ones(size(BW)), ones(size(BW)), zeros(size(BW))));
fig3(cat(3, BW, BW, BW)) = Y(cat(3, BW, BW, BW));
figure;imshow(fig3)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Run Code Online (Sandbox Code Playgroud)