Detection of ellipses;

100 views (last 30 days)
Sibel Cakir
Sibel Cakir on 7 Nov 2016
Answered: Image Analyst on 29 Sep 2021
I need to detect ellipses for example in the attached picture. Is there any tool, which can automatically
-detect ellipses or - bring parts of curves together and merge them into ellipses? (e.g.: forms which look like ellipses should be detetected and merged into an ellipse; fitting to an ellipse)
Thank you very much in advance!

Accepted Answer

Hannes Daepp
Hannes Daepp on 11 Nov 2016
I understand that you would like to detect ellipses in the figure using a MATLAB function. While MATLAB offers "imfindcircles" and "hough", it does not have any built-in tools to detect ellipses.
You could try using the "regionprops" function to measure properties of image regions, as done in the following blog post: http://blogs.mathworks.com/steve/2015/08/17/ellipse-visualization-and-regionprops/?s_tid=srchtitle
You may also find some relevant files by searching the File Exchange. For example, the following file uses a Hough transform to detect ellipses: http://www.mathworks.com/matlabcentral/fileexchange/33970-ellipse-detection-using-1d-hough-transform?s_tid=srchtitle
Finally, you can find a complete list of functions in the Image Processing Toolbox at the following link in the documentation: http://www.mathworks.com/help/images/functionlist.html
  1 Comment
Sibel Cakir
Sibel Cakir on 14 Nov 2016
Thank you very much for so many advices. I will read all of them.

Sign in to comment.

More Answers (2)

Razeem Ahmad
Razeem Ahmad on 29 Sep 2021
This code should help.
function bestFits = ellipseDetection(img, params)
% ellipseDetection: Ellipse detection
% default values
if nargin==1; params=[]; end
% - parameters to contrain the search
if ~isfield(params,'minMajorAxis'); params.minMajorAxis = 10; end
if ~isfield(params,'maxMajorAxis'); params.maxMajorAxis = 200; end
if ~isfield(params,'rotation'); params.rotation = 0; end
if ~isfield(params,'rotationSpan'); params.rotationSpan = 0; end
if ~isfield(params,'minAspectRatio'); params.minAspectRatio = 0.1; end
if ~isfield(params,'randomize'); params.randomize = 2; end
% - others
if ~isfield(params,'numBest'); params.numBest = 3; end
if ~isfield(params,'uniformWeights'); params.uniformWeights = true; end
if ~isfield(params,'smoothStddev'); params.smoothStddev = 1; end
eps = 0.0001;
bestFits = zeros(params.numBest,6);
params.rotationSpan = min(params.rotationSpan, 90);
H = fspecial('gaussian', [params.smoothStddev*6 1], params.smoothStddev);
[Y,X]=find(img);
Y = single(Y); X = single(X);
N = length(Y);
fprintf('Possible major axes: %d * %d = %d\n', N, N, N*N);
% compute pairwise distances between points (memory intensive!) and filter
% TODO: do this block-wise, just appending the filtered results (I,J)
distsSq = bsxfun(@minus,X,X').^2 + bsxfun(@minus,Y,Y').^2;
[I,J] = find(distsSq>=params.minMajorAxis^2 & distsSq<=params.maxMajorAxis^2);
idx = I<J;
I = uint32(I(idx)); J = uint32(J(idx));
fprintf('..after distance constraint: %d\n', length(I));
% compute pairwise angles and filter
if params.rotationSpan>0
tangents = (Y(I)-Y(J)) ./ (X(I)-X(J));
tanLo = tand(params.rotation-params.rotationSpan);
tanHi = tand(params.rotation+params.rotationSpan);
if tanLo<tanHi
idx = tangents > tanLo & tangents < tanHi;
else
idx = tangents > tanLo | tangents < tanHi;
end
I = I(idx); J = J(idx);
fprintf('..after angular constraint: %d\n', length(I));
else
fprintf('..angular constraint not used\n');
end
npairs = length(I);
% compute random choice and filter
if params.randomize>0
perm = randperm(npairs);
pairSubset = perm(1:min(npairs,N*params.randomize));
clear perm;
fprintf('..after randomization: %d\n', length(pairSubset));
else
pairSubset = 1:npairs;
end
% check out all hypotheses
for p=pairSubset
x1=X(I(p)); y1=Y(I(p));
x2=X(J(p)); y2=Y(J(p));
%compute center & major axis
x0=(x1+x2)/2; y0=(y1+y2)/2;
aSq = distsSq(I(p),J(p))/4;
thirdPtDistsSq = (X-x0).^2 + (Y-y0).^2;
K = thirdPtDistsSq <= aSq;
%get minor ax propositions for all other points
fSq = (X(K)-x2).^2 + (Y(K)-y2).^2;
cosTau = (aSq + thirdPtDistsSq(K) - fSq) ./ (2*sqrt(aSq*thirdPtDistsSq(K)));
cosTau = min(1,max(-1,cosTau));
sinTauSq = 1 - cosTau.^2;
b = sqrt( (aSq * thirdPtDistsSq(K) .* sinTauSq) ./ (aSq - thirdPtDistsSq(K) .* cosTau.^2 + eps) );
%proper bins for b
idxs = ceil(b+eps);
if params.uniformWeights
weights = 1;
else
weights = img(sub2ind(size(img),Y(K),X(K)));
end
accumulator = accumarray(idxs, weights, [params.maxMajorAxis 1]);
accumulator = conv(accumulator,H,'same');
accumulator(1:ceil(sqrt(aSq)*params.minAspectRatio)) = 0;
[score, idx] = max(accumulator);
%keeping only the params.numBest best hypothesis (no non-maxima suppresion)
if (bestFits(end,end) < score)
bestFits(end,:) = [x0 y0 sqrt(aSq) idx atand((y1-y2)/(x1-x2)) score];
if params.numBest>1
[~,si]=sort(bestFits(:,end),'descend');
bestFits = bestFits(si,:);
end
end
end
end

Image Analyst
Image Analyst on 29 Sep 2021
There is a paper I'm attaching on detecting ellipses.
I have not coded it up but if you do, please attach your code here.

Community Treasure Hunt

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

Start Hunting!