Measuring the Radius of Circles in bmp files
1 回表示 (過去 30 日間)
古いコメントを表示
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 件)
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 件のコメント
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
参考
カテゴリ
Help Center および File Exchange で Image Data Workflows についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!