MATLAB Answers

Measuring the Radius of Circles in bmp files

5 ビュー (過去 30 日間)
Tom
Tom 2019 年 11 月 27 日
Commented: Rena Berman 2019 年 12 月 12 日
I am wondering if there is a way to find a relative radius of a circle given the matrix of the bmp. I am using imread() to convert pictures of various hand drawn circles into a program however I can not find a way to accurately measure the radius of these circles.

  1 件のコメント

Rena Berman
Rena Berman 2019 年 12 月 12 日
(Answers Dev) Restored edit

サインイン to comment.

件の回答 (1)

Image Analyst
Image Analyst 2019 年 11 月 27 日
Yes. But you haven't shown us an image. Chances are you can threshold, then call imfill(), then call regionprops().
if ndims(grayImage) > 1
grayImage = rgb2gray(grayImage); % Convert to gray scale if needed.
end
binaryImage = grayImage < 128; % or whatever value works.
binaryImage = imfill(binaryImage, 'holes');
props = regionprops(binaryImage, 'EquivDiameter')
allRadii = [props.EquivDiameter] / 2;
Attach your image if you need more help.

  3 件のコメント

Tom
Tom 2019 年 11 月 28 日
This doesn't appear to be working, only reruns a matrix of all 1s. Here is an example of an image I am using
Tom
Tom 2019 年 11 月 28 日
And the code I am using. Rgb2gray did not work with this image, however the files are all .bmp
prompt = 'please type in which file to use\n';
str = input(prompt,'s');
pic = imread(str);
pic2 = pic < 150;
pic2 = imfill(pic2, 'holes')
props = regionprops(pic2, 'EquivDiameter');
rad = [props.EquivDiameter]/2;
Image Analyst
Image Analyst 2019 年 11 月 28 日
Tom:
See how important it is to provide images in the beginning to avoid delays. Your image is not a gray scale image or an RGB image. It's already a logical image, which is kind of rare for images to be stored like that. So I put in code to handle that case.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
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;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
%===============================================================================
% Read in a standard MATLAB color demo image.
folder = pwd;
baseFileName = 'circle4.bmp';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
fprintf('This image is RGB. I will change it to gray scale.\n');
grayImage = rgb2gray(grayImage);
end
% Display the original image.
subplot(2, 2, 1);
imshow(grayImage);
axis('on', 'image');
title('Original Image', 'FontSize', fontSize);
impixelinfo;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0.1, 1, 0.9]);
if isa(grayImage, 'logical')
% It's already a binary image.
binaryImage = grayImage;
else
% It's gray scale. Threshold to create a binary image.
binaryImage = grayImage < 128; % or whatever value works.
end
% At this point, make sure the circles are dark on a white background.
% Now invert so that circles are white on a black background.
binaryImage = ~binaryImage;
subplot(2, 2, 2);
imshow(binaryImage, []);
axis('on', 'image');
title('Binary Image', 'FontSize', fontSize);
binaryImage = imfill(binaryImage, 'holes');
subplot(2, 2, 3);
imshow(binaryImage, []);
axis('on', 'image');
title('Filled Binary Image', 'FontSize', fontSize);
% Make measurements of area.
props = regionprops(binaryImage, 'EquivDiameter')
allRadii = [props.EquivDiameter] / 2
msgbox('Done! Radii are in the command window.');
In the command window you'll see:
props =
struct with fields:
EquivDiameter: 89.1062388442497
allRadii =
44.5531194221249

サインイン to comment.

サインイン してこの質問に回答します。


Translated by