MATLAB Answers

how measure major and minor axis of ellipse from image used matlab

7 views (last 30 days)
see a picture

Accepted Answer

Image Analyst
Image Analyst on 1 Dec 2019
Try this:
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 color demo image.
folder = pwd;
baseFileName = '6.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 = grayImage(:, :, 2);
end
% Display the original image.
subplot(2, 2, 1);
imshow(grayImage);
axis('on', 'image');
title('Green Channel Image', 'FontSize', fontSize);
impixelinfo;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0.1, 1, 0.9]);
subplot(2, 2, 2);
imhist(grayImage);
grid on;
title('Intensity Histogram', 'FontSize', fontSize);
% It's gray scale. Threshold to create a binary image.
binaryImage = grayImage < 90; % or whatever value works.
subplot(2, 2, 3);
imshow(binaryImage, []);
axis('on', 'image');
title('Binary Image', 'FontSize', fontSize);
% Get rid of partial blobs leaving the field of view.
binaryImage = imclearborder(binaryImage);
% Get rid of blobs less than 500 in area.
binaryImage = bwareaopen(binaryImage, 500);
binaryImage = imfill(binaryImage, 'holes');
[labeledImage, numberOfblobs] = bwlabel(binaryImage);
% Apply a variety of pseudo-colors to the regions.
coloredLabelsImage = label2rgb (labeledImage, 'hsv', 'k', 'shuffle');
% Display the pseudo-colored image.
subplot(2, 2, 4);
imshow(coloredLabelsImage);
axis('on', 'image');
title('Filled Binary Image', 'FontSize', fontSize);
% Make measurements of area.
props = regionprops(binaryImage, 'Area', 'MajorAxisLength', 'MinorAxisLength')
allAreas = [props.Area]
majorAxisLength = [props.MajorAxisLength]
minorAxisLength = [props.MinorAxisLength]
msgbox('Done! lengths are in the command window.');
0000 Screenshot.png
allAreas =
901 869 895 828 938 950 897 982 893 849 876
majorAxisLength =
Columns 1 through 5
42.3096288162948 41.4884349273287 42.8162935858843 41.2903546123474 42.7337289342248
Columns 6 through 10
44.1105808446811 42.0295867943025 38.2652632328308 42.4311889315166 41.8919530940952
Column 11
38.6585845668433
minorAxisLength =
Columns 1 through 5
27.4939460477342 27.0457380137077 27.0387459149836 26.0994881281772 28.1097891482489
Columns 6 through 10
27.8269540727149 27.5096530490088 32.7371049402545 27.2963558415452 26.2322961767191
Column 11
28.9929460861065

  5 Comments

Show 2 older comments
Mushtaq Al-Jubbori
Mushtaq Al-Jubbori on 1 Dec 2019
Please, why there are differences between the values on the picture and values in the workspace?
Please, why does the program not measure minor and major axes on these atached pictures?
Image Analyst
Image Analyst on 1 Dec 2019
In the workspace there will be more significant digits. I chose only to show 2 on the image since that seemed plenty.
For other images, you may need to adjust the threshold. See my File Exchange for a manual/interactive app that lets you visually select the threshold interactively. If changing the threshold doesn't work, try to see what is different between the two, like the focus or contrast or something.

Sign in to comment.

More Answers (0)

Sign in to answer this question.