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 次查看(过去 30 天)

采纳的回答

Image Analyst
Image Analyst 2021-9-3
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).

更多回答(1 个)

Image Analyst
Image Analyst 2021-9-2
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)

类别

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

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by