How to tell the number of defective tablets using matlab?

3 views (last 30 days)
I'm working on a project, where I have to display the number of defected tablets. Till now I'm done with the preprocessing like:
1. rgb2gray
2. gray to binary image
3. bwmorph (erode and remove)
This is the tablet blister I'm working with:
This the resultant image after the pre-processing operations on the original image:
Here's a section of my code which tells the difference between the ideal blister and the defective one.
% Calculate the Normalized Histogram of Image 1 and Image 2
hn1 = imhist(Imageg1)./numel(Imageg1);
hn2 = imhist(Imageg2)./numel(Imageg2);
% Calculate the histogram error/ Difference
f1 = sum((hn1 - hn2).^2);
set(handles.text3,'String',f1)
if sum( abs( I1(:) - I2(:) ) ) == 0.0
h=msgbox(' No Defect Found');
else
h=msgbox(' Defect Found');
end
Is there any way I can tell how many tablets are defective?
Thanks in advance!

Accepted Answer

Image Analyst
Image Analyst on 12 May 2015
This kind of "missing part" image analysis is trivial in machine vision. The usual method, because it's the easiest, is to just have a template and look at the mean intensity inside the templates mask. The intensity is either what is should be, or, if part of the pill is missing, it will be less.
labeledImage = bwlabel(binaryImage);
measurements = regionprops(labeledImage, grayImage, 'MeanIntensity');
allIntensities = [measurements.MeanIntensity];
goodOnes = allIntensities > someThreshold;
  3 Comments
Image Analyst
Image Analyst on 12 May 2015
Sure, just get the centroids. Here's some untested code.
measurements = regionprops(labeledImage, grayImage, 'MeanIntensity', 'Centroid');
allIntensities = [measurements.MeanIntensity];
allCentroids = [measurements.Centroid];
xCentroids = allCentroids(1:2:end);
yCentroids = allCentroids(2:2:end);
badOnes = find(allIntensities < someThreshold); % Indexes of bad pills
% Place a red x at the template centroid location of the bad ones.
for k = 1 : length(badOnes)
x = xCentroids(badONes(k));
y = yCentroids(badONes(k));
text(x, y, 'X', 'FontSize', 30);
end
Remember, the template is a one time mask that you create based on the known locations of a perfect pack of pills, it is not something you create from each image. If you created it from each image, then what would it find if there were no pills at any locations? So you see if has to be a "perfect" template.
Image Analyst
Image Analyst on 7 Jan 2021
Please post these in your original question rather than here. Also explain why you're not able to have good control over your image capture conditions such as uniformity of lighting and placement of the sample. If you could do those two things it would make it MUCH easier to solve your problem.

Sign in to comment.

More Answers (2)

Walter Roberson
Walter Roberson on 12 May 2015
De-speckle. Then regionprops(). Look at the eccentricity to figure out if the junk is being examined as the eccentricity of the blisters should be relatively low. When you have isolated the blisters then look at their FilledArea

omran
omran on 22 Dec 2023
Hello, I am a university student and I am doing the same project, but I could not succeed. If your code works, can you help me?

Community Treasure Hunt

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

Start Hunting!