How can I detect bubbles on this image using Matlab Image Processing Toolbox?
Hello,
I've been trying to detect the bubbles on this image:

I have tried several different methods, both taken from the Matlab help and from this same forum. Below is an example of what I have used:
clearvars;
clc;
imtool close all;
close all;
%% Import image
I = imread('DSC_0438.jpg'); % picture with bubbles
% From previous experience, it is futile to manipulate the image when it
% has been binarized; most of the information should be discriminated while
% it is still a graysccale image
%% Convert image to grayscale
I = rgb2gray(I);
I2 = adapthisteq(I);
%% Remove background from image
background1 = imopen(I,strel('diamond',20));
I3 = I2 - background1;
%% Binarize image
I4 = imbinarize(I3,'adaptive');
I4 = bwmorph(I4,'majority');
se = strel('disk',6);
I4 = imclose(I4,se);
cc = bwconncomp(I4);
%% Filter out small pixel clusters by size and remove non-circular shapes
% Taken from the Tips section of the regionprops help
% The discriminating area ranges (100 and 1000) are equivalent to a
% bubble diameter of 44 and 139 microns, respectively. The maximum
% allowable eccentricity is 0.5 (0 is a circle and 1 is a line)
min_area = 100;
max_area = 10000;
max_eccentricity_global = 0.5;
stats = regionprops(cc,'Area','BoundingBox','Eccentricity','Centroid',...
'MajorAxisLength','MinorAxisLength');
areas = [stats.Area]';
eccentricities = [stats.Eccentricity]';
idx = find(areas > min_area & areas < max_area & ...
eccentricities < max_eccentricity_global);
I5 = ismember(labelmatrix(cc),idx);
%% Find the detected bubbles' centroids, diameters and radii
% Taken from the Examples section of the regionprops help
cc2 = bwconncomp(I5);
stats2 = regionprops('table',cc2,'Centroid','MajorAxisLength',...
'MinorAxisLength');
centers = stats2.Centroid;
diameters = mean([stats2.MajorAxisLength stats2.MinorAxisLength],2);
radii = diameters/2;
diameters_um = diameters .* 3.9; %3.9 um is the pixel size of the D7200
mean_diameter_um = mean(diameters_um);
%% Draw circles around the detected bubbles and overlap with original image
figure
subplot(2,2,1)
imshow(I)
title('Original image')
subplot(2,2,2)
imshow(I)
hold on
viscircles(centers,radii);
hold off
title('Bubble detection')
subplot(2,2,3)
imshowpair(I4,I5)
title('Comparison of discriminated areas')
Basically, what I get is this:
I tried playing with the eccentricity and the thresholds I define for the areas and for the binarization, as well with the bwmorph function to eliminate noise, but I still get a lot of false positives.
I know that the bubbles on the binary photo look like (portions of) rings (see below)
I was wondering if I could define a neighborhood matrix for the strel function to keep those and eliminate the rest, but I am not sure how to implement it. Any help would be appreciated.
Regards,
Julio
8 Comments
Answers (1)
0 votes
7 Comments
Categories
Find more on Image Segmentation in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





