Ellipse angle dimension and axis

1 view (last 30 days)
elis02
elis02 on 26 May 2021
Commented: Image Analyst on 2 Oct 2021
Hi
I need help with fitting automatically to ellipse. (
I have a photo of a spot from a camera. (added A=imread of the file in the matlab.mat file, can see the photo as imagesc(double(A)))
Each pixel has some kind of dimention (for example 150um , 150e-6)
How can i find the main axis of the ellipse in the photo? (need also to align it to pixel size so angle is a must)
Also, made fourier on it
Y=fftshift(fft2(double(A)))
and when looking at the fourier image, how can i do the same fit there around 0? (in the fourier we also have an angled shape).
*note that i need to have automatic finding of the result. not by hand as each photo will be different.

Answers (1)

Image Analyst
Image Analyst on 29 Sep 2021
Edited: Image Analyst on 29 Sep 2021
Try this. You might also want to take a look at bwferet().
% Demo by Image Analyst
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 = 'matlab.mat';
s = load(fileName)
grayImage = s.A;
% 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
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
impixelinfo;
axis('on', 'image');
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
hold on
drawnow;
grayImage(grayImage == 1) = 255;
lowThreshold = 3445;
highThreshold = inf;
%[lowThreshold, highThreshold] = threshold(lowThreshold, highThreshold, grayImage);
mask = grayImage >= lowThreshold & grayImage <= highThreshold;
% Display the image.
subplot(2, 2, 2);
imshow(mask, []);
impixelinfo;
axis('on', 'image');
title('Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Take largest blob.
mask = bwareafilt(mask, 1);
% Fill holes
mask = imfill(mask, 'holes');
% Display the image.
subplot(2, 2, 3);
imshow(mask, []);
impixelinfo;
axis('on', 'image');
title('Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Make measurements.
props = regionprops(mask, 'Centroid', 'Orientation', 'MajorAxisLength');
xCenter = props.Centroid(1);
yCenter = props.Centroid(2);
hold on;
plot(xCenter, yCenter, 'r+', 'LineWidth', 2);
% Find endpoints of line along major axis.
radius = props.MajorAxisLength / 2;
x1 = xCenter - radius * cosd(props.Orientation)
y1 = yCenter + radius * sind(props.Orientation)
x2 = xCenter + radius * cosd(props.Orientation)
y2 = yCenter - radius * sind(props.Orientation)
plot([x1, x2], [y1, y2], 'r-', 'lineWidth', 2)
  3 Comments
elis02
elis02 on 2 Oct 2021
Edited: elis02 on 2 Oct 2021
Hi, thanks. I'm on vacation without my PC so didn't try yet. However, you are doing this not on the Fourier but rather on the original photo. If make it on the Fourier, it will cancel some of the noise. According to what I see you used almost the same as I did. My question is what happens if you use a crop, will the angle be different? (It shouldn't be ofcourse but it is on my script -also use regionprop) .
Image Analyst
Image Analyst on 2 Oct 2021
It should work on the Fourer transform image also. Not sure how identifying the oval and drawing a line along the major axis would cancel some of the noise though. If you erased the transform outside the oval then inverse transformed, it would reduce high frequencies (which may be noise). To do that on the real part you'd do
ftReal(~mask) = 0; % Erase outside the mask on the real image part of the Fourier transform.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!