How to isolate/find a black spot/dot on a pharmaceutical tablet

1 次查看(过去 30 天)
Hi,
I would like to isolate the black dot on the tablet here with black background (example of picture is attached). I would like to find the location of the black dot and maybe the system can give me a value I can use to see any difference?
Anyone who knows which approach to use through Image Processing? It would be preferable if you could show me which algorithms to use.
Thank you!

回答(1 个)

Image Analyst
Image Analyst 2020-12-29
Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 22;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
folder = pwd;
baseFileName = 'black dot.jpg';
grayImage = imread(baseFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = rgb2gray(grayImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
% grayImage = grayImage(:, :, 2); % Take green channel.
end
%--------------------------------------------------------------------------------------------------------
% Display the image with the original, linear colormap.
subplot(2, 2, 1);
imshow(grayImage);
axis('on', 'image');
title('Original Color Map', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
colorbar;
hFig = gcf;
hFig.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.
drawnow;
set(gca,'ColorScale','log')
%--------------------------------------------------------------------------------------------------------
% Show the histogram:
subplot(2, 2, 2);
imhist(grayImage);
grid on;
title('Histogram', 'FontSize', fontSize, 'Interpreter', 'None');
%--------------------------------------------------------------------------------------------------------
% BINARIZE THE IMAGE
threshold = 180;
xline(threshold, 'Color', 'r', 'LineWidth', 2);
mask = grayImage <= 180;
%--------------------------------------------------------------------------------------------------------
% Display the mask.
subplot(2, 2, 3);
imshow(mask);
axis('on', 'image');
title('Mask', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
drawnow;
% Get the surrounding background area.
background = bwareafilt(mask, 1);
% Erase it from the mask.
mask(background) = false;
%--------------------------------------------------------------------------------------------------------
% Display the mask.
% Label each blob with 8-connectivity, so we can make measurements of it
[labeledImage, numberOfBlobs] = bwlabel(mask, 8);
% Apply a variety of pseudo-colors to the regions.
coloredLabelsImage = label2rgb (labeledImage, 'hsv', 'k', 'shuffle');
% Display the pseudo-colored image.
subplot(2, 2, 4);
imshow(coloredLabelsImage);
axis('on', 'image');
caption = sprintf('%d distinct blobs', numberOfBlobs);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
drawnow;
%--------------------------------------------------------------------------------------------------------
% Measure area and locations
props = regionprops(mask, 'Area', 'Centroid');
allAreas = [props.Area]
xy = vertcat(props.Centroid) % get (x,y) of each centroid.
You can see it found 13 dark defects with that threshold. You can filter out some blobs you don't want based on their properties, like location, intensity, shape, or whatever.
  5 个评论
Brian Pravin
Brian Pravin 2021-1-5
Hi, is there any way that we can find the defect tablets in a tablet strip? Assuming there is a missing tablet in the strip. Is there any way to find there is a mssing tablet in the strip?
Image Analyst
Image Analyst 2021-1-5
Yes. Just segment the image to find the circular areas - perhaps use stdfilt() to find smooth areas. Then measure their properties such as mean brightness, standard deviation, or something. See what range you get for normal, and abnormal, circles. Something should be different between the two. Start a new question for this if you need more help.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Image Processing and Computer Vision 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by