Hi, I am the beginer for doing simple image subtration to obtain defect image. Is it possible to make it only show the defect image if nothing difference wont show?

1 回表示 (過去 30 日間)
clc
clear
close all
warning off;
x=imread('origin.jpg');
y=imread('capture.jpg');
[g, c, d]=size(x);
y=imresize(y,[g,c]);
subplot(1,3,1);
imshow(x);
title('origin image');
subplot(1,3,2);
imshow(y);
title('capture image');
subplot(1,3,3);
imshow(x-y);
title('defect occur if difference colour shown');

採用された回答

yanqi liu
yanqi liu 2022 年 1 月 17 日
yes,sir
Is it possible to make it only show the defect image if nothing difference wont show?
may be add some judge rule,such as
clc
clear
close all
warning off;
x=imread('origin.jpg');
y=imread('capture.jpg');
[g, c, d]=size(x);
y=imresize(y,[g,c]);
subplot(1,3,1);
imshow(x);
title('origin image');
subplot(1,3,2);
imshow(y);
title('capture image');
bw = find((x-y) > 1);
if numel(bw) > 1
subplot(1,3,3);
imshow(x-y);
title('defect occur if difference colour shown');
end
  2 件のコメント
CHEE HOON SEOW
CHEE HOON SEOW 2022 年 1 月 17 日
OMGGGG!!!! It's works!!!! Thank you!!!!
Image Analyst
Image Analyst 2022 年 1 月 17 日
Yes, but beware that (x-y) will clip to zero and not go negative.

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2022 年 1 月 17 日
I suggest you use imabsdiff().
diffImage = imabsdiff(x, y);
threshold = 5; % Images must be at least 5 gray levels different to be considered a defect.
mask = diffImage >= threshold;
imshow(mask)
% Get area of defects
props = regionprops(mask, 'Area')
allAreas = [props.Area]
if ~isempty(props)
fprintf('Found %d defect areas.\n', length(props))
else
fprintf('Found no defect areas.\n')
end
  7 件のコメント
CHEE HOON SEOW
CHEE HOON SEOW 2022 年 1 月 24 日
This is the original code written from you
% yanqui's code first:
clc
clear
close all
warning off;
x=imread('origin.jpg');
y=imread('capture.jpg');
% My changes below:
diffImage = imabsdiff(x, y);
threshold = 5; % Images must be at least 5 gray levels different to be considered a defect.
mask = diffImage >= threshold;
imshow(mask)
% Get area of defects
props = regionprops(mask, 'Area')
allAreas = [props.Area]
if ~isempty(props)
fprintf('Found %d defect areas.\n', length(props))
else
fprintf('Found no defect areas.\n')
end
I am not sure why getting error message and search on it told that the image need to be convert it as grayscale, therefore i try it using rgb2gray() function to convert and do the imabsdiff function, it works
now, I am trying to add in the for loop to make the y image become variable and change the image according inside the loop for doing image substraction.
after that i still getting error, the matlab shown error on function rgb2gray() and told that in error message and i try to change it to im2gray() error again.
due to i have 100+ of image for the y so that i am trying to add in the for loop but it turn out errors
Image Analyst
Image Analyst 2022 年 1 月 24 日
You keep forgetting to attach your original images. I'll check back tomorrow.

サインインしてコメントする。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by