how to fit ellipse to an image with cylinderical shape
6 views (last 30 days)
Show older comments
Hi
I have to fit an ellipse to an image with a cylinder(see the attached image). after using the edge detection, i could recognize the ellipse shape but i couldn't find a way how to fit an ellipse on to it, or in other words i couldn't extract that part from rest of the image.
any suggestions?
Thanks!
1 Comment
Adam Danz
on 31 Mar 2020
If you share the code that inputs original_image and produces edge_image, I might be able to play around with it later.
Answers (2)
Image Analyst
on 31 Mar 2020
First of all, don't do an edge detection. I don't know why people always think edge detection is the first step in any image processing algorithm. In your case it's definitely NOT. You should do a simple threshold, then scan the image to find the right edge, then get the semi axes from the values. Assuming it's perfectly aligned, try something like (untested)
mask = imbinarize(grayImage);
[rows, columns, numColors] = size(grayImage);
rightColumns = zeros(rows, 1);
for row = 1 : rows
thisRow = mask(row, :);
t = find(thisRow, 1, 'last');
if ~isempty(t)
rightColumns(row) = t;
end
end
% Then get rid of any outliers that you may get if you're not perfectly aligned (for you to do).
% Find top and bottom (first and last elements of right rightColumns).
topRow = find(rightColumns, 1, 'first')
bottomRow = find(rightColumns, 1, 'last')
% Get ellipse semi-axes, a and b
b = (bottomRow - topRow) / 2
nonZeroIndexes = rightColumns > 0;
xCenter = min(rightColumns(nonZeroIndexes)
yCenter = mean([topRow, bottomRow])
a = max(rightColumns(nonZeroIndexes)) - min(rightColumns(nonZeroIndexes))
% Now you have the equation of an ellipse: ((x-xCenter)/a)^2 + ((y-yCenter)/b)^2 = 1
7 Comments
Image Analyst
on 6 Apr 2020
That's a different t. That t is the angle. And they're the wrong equations. I think they should be
t = linspace(0, 360, 500);
x = xCenter + a * cosd(t);
y = yCenter + b * sind(t);
Try that. I've got a meeting with the Mathworks over the next two hours. If it doesn't work, let me know and I'll look at it after my Mathworks board meeting.
Hiro Yoshino
on 30 Mar 2020
I am not an expert - please see the following page and hopefully there are some similar examples for you.
If you are fancy to app, the try this out:
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!