MATLAB Answers

0

How to trigger alarm once even if all 5 functions detect defects?

Asked by Shu Yi Ho on 31 Jul 2019
Latest activity Answered by Sahithi Kanumarlapudi on 9 Aug 2019
The image will run through all 5 functions. If defect is detected, alarm will be triggerred.
The issue now if all 5 functions detect the defect, the alarm will be triggered 5 times.
How do I make it only trigger once and at the same time the image will run through all 5 functions.
%% CLEAR AND SETUP WORKSPACE
clc;
close all;
clear;
%% ACCESS ALL IMAGES IN THE FOLDER
imageFolder = dir('C:\Users\85042513\MATLAB\Projects\BAT CVE\Images\10OK\*.jpg');
numfiles = length(imageFolder);
ori_roi = cell(numfiles, 1);
i = 1;
filename = strcat('C:\Users\85042513\MATLAB\Projects\BAT CVE\Images\10OK\',imageFolder(i).name);
originalImage = imread(filename);
%% REGION OF INTEREST
rect = [375.5 581.5 795 363]; % fixed ROI
% Might be floating point, so round:
rect = int32(rect);
% Make sure it's never zero because an index of 0 is not allowed.
rect(rect == 0) = 1;
ori_roi{i} = originalImage( rect(2) : (rect(2)+rect(4)) , rect(1) : (rect(1)+rect(3)) , : ); % store roi in matrix
%% SHARED VARIABLES
numOKcases = 0;
numNGcases = 0;
%% LOOP THROUGH ALL IMAGES IN THE FOLDER, SELECT ROI ONCE & RUN ALL THE 5 FUNCTIONS
for i=2:length(imageFolder)
tic
filename = strcat('C:\Users\85042513\MATLAB\Projects\BAT CVE\Images\10OK\',imageFolder(i).name);
originalImage = imread(filename);
ori_roi{i} = originalImage( rect(2) : (rect(2)+rect(4)) , rect(1) : (rect(1)+rect(3)) , : ); % store roi in matrix
fprintf("Image name: %s\n",imageFolder(i).name);
%% LOAD ORIGINAL IMAGE
subplot(1,3,1);
imshow(ori_roi{i});
title('Original Image');
%% CONVERT TO BINARY IMAGE
normalizedThresholdValue = 0.4; % In range 0 to 1.
binaryImage = im2bw(ori_roi{i}, normalizedThresholdValue);
binaryImage = bwmorph(binaryImage,'thin', 1);
%% FUNCTION 1: NOT EMPTY
white_pix = sum(sum(binaryImage));
if white_pix > 1
%using the built-in function clock to get date and time
time = clock;
%Divide correspondingly
year = time(1);
month = time(2);
day = time(3);
hours = time(4);
minutes = time(5);
seconds = time(6);
seconds = round(seconds);
fprintf('Date: %d/%d/%d ',day,month,year)
fprintf('Time: %d:%d:%d\n',hours,minutes,seconds)
fprintf('FUNCTION 1 running.\n');
%% FUNCTION 2: NUMBER OF CIRCLES
Rmin = 20;
Rmax = 40;
subplot(1,3,2);
imshow(binaryImage);
title('Number of Circles Detected');
[centers, radii] = imfindcircles(binaryImage,[Rmin Rmax],'Sensitivity',0.85,'Edge',0.03);
viscircles(centers, radii,'EdgeColor','b');
numCircles = length(centers);
fprintf('FUNCTION 2: %d cigarettes are detected.\n',numCircles);
fprintf("Maximum Radius: %f\n", max(radii));
fprintf("Minimum Radius: %f\n", min(radii));
if numCircles == 0
numOKcases = numOKcases + 1;
elseif numCircles < 80
[a,fs]= audioread('Sound/WarningAlarm.mp3');
sound(a(1:300000,1),fs);
numNGcases = numNGcases + 1;
fprintf(2,'FUNCTION 2 Error\n');
else
numOKcases = numOKcases + 1;
end
%% FUNCTION 3: TOTAL AREA OF WHITE BLOBS
white_pix = sum(sum(binaryImage));
if white_pix < 180000
[a,fs]= audioread('Sound/WarningAlarm.mp3');
sound(a(1:300000,1),fs);
numNGcases = numNGcases + 1;
fprintf(2,'FUNCTION 3 Error\n');
else
numOKcases = numOKcases + 1;
end
fprintf('FUNCTION 3: The total area of the white pixel is %d pixel.\n',white_pix);
%% FUNCTION 4: NUMBER OF BLACK BLOBS
% Code in function 4 is shared with function 5
se = strel('disk', 9);
grayImage = rgb2gray(ori_roi{i});
grayImage = imdilate(grayImage, se);
% Black Blob Gray Image
thresholdValue = 140;
blackBlobBinaryImage = grayImage < thresholdValue; % Dark objects will be chosen if you use <
% Identify individual blobs by seeing which pixels are connected to each other.
labeledImage = bwlabel(blackBlobBinaryImage, 8); % Label each blob so we can make measurements of it
% Get all the blob properties. Can only pass in grayImage in version R2008a and later.
blobMeasurements = regionprops(labeledImage, grayImage, 'all');
numberOfBlobs = size(blobMeasurements, 1);
subplot(1,3,3);
imshow(grayImage);
title('Black Blobs Detected');
hold on;
boundaries = bwboundaries(blackBlobBinaryImage);
numberOfBoundaries = size(boundaries, 1);
for k = 1 : numberOfBoundaries
thisBoundary = boundaries{k};
plot(thisBoundary(:,2), thisBoundary(:,1), 'g', 'LineWidth', 2);
end
hold off;
% Print header line in the command window.
%fprintf('FUNCTION 4 & 5:\n');
%fprintf(1,'Black Blob # Area\n');
% Loop over all blobs printing their measurements to the command window.
for k = 1 : numberOfBlobs
thisBlobsPixels = blobMeasurements(k).PixelIdxList; % Get list of pixels in current blob.
blobArea = blobMeasurements(k).Area; % Get area.
%fprintf(1,'#%.2d %17.1f\n', k, blobArea);
end
fprintf('FUNCTION 4: There are %d black blobs.\n', numberOfBlobs);
if numberOfBlobs > 40
[a,fs]= audioread('Sound/WarningAlarm.mp3');
sound(a(1:300000,1),fs);
numNGcases = numNGcases + 1;
fprintf(2,'FUNCTION 4 Error\n');
else
numOKcases = numOKcases + 1;
end
%% FUNCTION 5: AREA OF BLACK BLOBS (INDIVIDUAL)
blackBlobAreas = [blobMeasurements.Area]; % Put all blob areas into one list (vector).
fprintf('FUNCTION 5\n');
fprintf('Maximum Black Blob Area: %d\n', max(blackBlobAreas));
fprintf('Minimum Black Blob Area: %d\n', min(blackBlobAreas));
if max(blackBlobAreas) > 3000
[a,fs]= audioread('Sound/WarningAlarm.mp3');
sound(a(1:300000,1),fs);
numNGcases = numNGcases + 1;
fprintf(2,'FUNCTION 5 Error\n');
else
numOKcases = numOKcases + 1;
end
else
fprintf('FUNCTION 1: It is empty.\n');
end
toc
fprintf("\n\n");
end
fprintf('The total number of OK cases: %d\nThe total number of NG cases: %d\n\n\n\n\n',numOKcases ,numNGcases);

  1 Comment

sounded_alarm = false;
[...]
elseif numCircles < 80
[a,fs]= audioread('Sound/WarningAlarm.mp3');
sound(a(1:300000,1),fs);
numNGcases = numNGcases + 1;
fprintf(2,'FUNCTION 2 Error\n');
sounded_alarm = true;
[...]
if white_pix < 180000
if ~sounded_alarm
[a,fs]= audioread('Sound/WarningAlarm.mp3');
sound(a(1:300000,1),fs);
sounded_alarm = true;
end
numNGcases = numNGcases + 1;
fprintf(2,'FUNCTION 3 Error\n');
and so on.

Sign in to comment.

Tags

1 Answer

Answer by Sahithi Kanumarlapudi on 9 Aug 2019

Try using a Boolean variable which acts as a flag and set it to 1 only when a defect is detected. After performing all five checks trigger the alarm only if this flag is set to 1. Make sure that the initial value of this variable is set to 0.

  0 Comments

Sign in to comment.