I have a binary image of a bubble. I would like to: 1. Fit a circle onto the bubble (white pixels region); and 2. Mark the centre of the bubble; in the following order.

7 views (last 30 days)
  1 Comment
Syamsundar Menon Murali Mohan
*Please note that what I mean by circle is, to fit an equivalent circle that can fit within the region of the bubble; as the bubble is not a perfect circle

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 3 Sep 2021
Try this:
% Demo to find three different circles ralted to a blob.
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 = 20;
markerSize = 40;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
fileName = 'image.jpeg';
grayImage = imread(fileName);
% 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.
% Extract the red channel (so the magenta lines will be white).
grayImage = grayImage(:, :, 1);
end
binaryImage = logical(grayImage);
% Crop off white frame.
binaryImage = imclearborder(binaryImage);
% Take largest blob only.
binaryImage = bwareafilt(binaryImage, 1);
% Crop the image
binaryImage = binaryImage(760:840, 290:390);
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 1, 1);
imshow(binaryImage, []);
impixelinfo;
axis('on', 'image');
title('Original Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
hold on
drawnow;
%--------------------------------------------------------------------------------------------------------
% FIND THE CIRCLE THAT CAN FIT COMPLETELY INSIDE.
% Do the distance transform
edtImage = bwdist(~binaryImage);
% Show results
subplot(2, 1, 2);
imshow(edtImage, []);
title('EDT Image', 'FontSize', fontSize);
axis('on', 'image');
impixelinfo;
drawnow;
g = gcf;
g.WindowState = 'maximized'
% Find the max
innerRadius = max(edtImage(:))
[ctrRows, ctrColumns] = find(edtImage == innerRadius)
% Put a inner circle there
subplot(2, 1, 1);
viscircles([ctrColumns, ctrRows], innerRadius);
%----------------------------------------------------------
% FIND THE EQUIVALENT CIRCULAR DIAMETER.
% This is the diameter of a circle that has the same area as our blob.
props = regionprops(binaryImage, 'EquivDiameter', 'Centroid');
% Display the ECD circle
viscircles([props.Centroid(1), props.Centroid(2)], props.EquivDiameter/2);
%----------------------------------------------------------
% FIND THE OUTER CIRCULAR DIAMETER.
% This is the diameter of a circle that will completely enclose our blob.
boundaries = bwboundaries(binaryImage, 'noholes');
xb = boundaries{1}(:, 2);
yb = boundaries{1}(:, 1);
% Show boundary
hold on;
plot(xb, yb, 'g-', 'LineWidth', 2);
% John D'Errico (2021). A suite of minimal bounding objects (https://www.mathworks.com/matlabcentral/fileexchange/34767-a-suite-of-minimal-bounding-objects), MATLAB Central File Exchange. Retrieved September 3, 2021.
[center, radius] = minboundcircle(xb, yb)
% Display the outer circle.
hold on;
viscircles(center, radius);
There are three different kinds of circles. One that fits inside, one that bounds everything on the outside of the blob, and the middle one that is the equivalent circular diameter (diameter of a circle that has the same area as the blob).

More Answers (1)

Image Analyst
Image Analyst on 2 Sep 2021
If it has to fit within, use bwdist to find the radius and centroid. Then place your circle. Here's a start:
edtImage = bwdist(mask); % or maybe bwdist(~mask)
radius = max(edtImage)
% Find centroid
[row, col] = edtImage == radius
% Plot circle
viscircle([col(:), row(:)], radius);
plot(col, row, 'r+', 'LineWidth', 2, 'MarkerSize', 30)
  1 Comment
Syamsundar Menon Murali Mohan
My radius value is a single row vector, hence why the [row,col] does not work.
I was hoping to fit the circle somewhat around the boundary of the bubble

Sign in to comment.

Categories

Find more on Image Processing and Computer Vision in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!