You are on page 1of 5

## signup login tour help stackoverflowcareers

However,recentlyIneedtoportittoOpenCV,buttherearesomefunctionsthatonlyexistinMathematica.Oneofthekeyfunctionisthe

Sincetherearefiveparametersforageneralellipse,Ineedtosamplefivepointstodetermineone.Howevere,themoresamplingpoints
onlyneedthreepointstodetermineoneellipseusingleastsquarefromtheMathematicaapproach.Theresultisquitegood.

updated

Ididsomeedgedetectandmedianblurbeforehandanddrawtheresultontheedgeimage.Myoriginaltestimageislikethis:

Ingeneral,myfinalgoalistodetecttheellipseinasceneoronanobject.Somethinglikethis:

That'swhyIchoosetouseRANSACtofittheellipsefromedgepoints.

## c++ opencv ellipse

AlbertK
21 1 9

"Sincetherearefiveparametersforageneralellipse,Ineedtosamplefivepointstodetermineone.">I

Myideaisthatageneralellipsewouldbeax^2+by^2+cxy+dx+ey+1=0.SoifIwanttosolvethisequation,I
needtohavefivepoint,(x1,y1)~(x5,y5).Substitutethemintothe(x,y)oftheellipseequation,Icansolve
thesimultaneousequationsoffivevariables.That'swhyIsayIneedtosamplefivepointstodetermine
one. AlbertK Apr2'14at17:27

## Thecodesnippetishere AlbertK Apr2'14at17:27

Consideringtheupdatedimage,usethegeneralizedHoughtransform.MSalters Apr2'14at22:43

You'reusingtermsinanunusualway.

f(x,y) .Thisgivesusa (df/dx,df/dy) vectorineachpoint.

## Yetyou'relookingattheimageasifit'safunction y=f(x) andthegradientwouldbe f(x)/dx .

Now,ifyoulookatyourimage,you'llseethatthetwointerpretationsaredefinitelyrelated.Your
imagetheinnerandouter.Theseofcoursecorrespondtothetwonormalvectors,andtherefore
areinoppositedirections.

multipleof45degrees:

MSalters
93.5k 6 71 194

'14at17:45

I'veupdatedmyoriginalpost.Myoriginaltestimageisasolidellipse.AndIdoedgedetecttogettheedge
shouldstillbethenormalvector,isthatright? AlbertK Apr2'14at17:50

stillbequantizedinto45,90,135...degrees? AlbertK Apr2'14at17:52

Ifyoufocusonellipse(noothershape),youcantreatthevalueofthepixelsoftheellipseas
massofthepoints.

ThenyoucancalculatethemomentofinertialIxx,Iyy,Ixytofindouttheangle,theta,whichcan
rotateageneralellipsebacktoacanonicalform(XXc)^2/a+(YYc)^2/b=1.

ThenyoucanfindoutXcandYcbythecenterofmass.

ThenyoucanfindoutaandbbyminXandminY.

update

Thismethodcanapplytofilledellipsetoo.

Morethanoneellipseonasingleimagewillfailunlessyousegmentthemfirst.

Letmeexplainmore,IwilluseCtorepresentcos(theta)andStorepresentsin(theta)

Afterrotationtocanonicalform,thenewXis[eq0]X=xCySandYisY=xS+yCwherexandy
areoriginalpositions.

TherotationwillgiveyouminIYY.

[eq1]

IYY=Sum(m*Y*Y)=Sum{m*(xS+yC)(xS+yC)}=Sum{m(xxSS+yyCC+xySC)=Ixx*S^2+
Iyy*C^2+Ixy*S*C

ForminIYY,d(IYY)/d(theta)=0thatis

2IxxSC2IyySC+Ixy(CCSS)=0

2(IxxIyy)/Ixy=(SSCC)/SC=S/C+C/S=Z+1/Z

Whileprogramming,theLHSisjustanumber,let'ssaidN

Z^2NZ+1=0

SotherearetworootsofZhencetheta,let'ssaidZ1andZ2,onewillmintheIYYandtheother
willmaxtheIYY.

pseudocode

ComputeIxx,Iyy,Ixyforaholloworfilledellipse.

Computetheta1=atan(Z1)andtheta2=atan(Z2)

PutThesetwothetaintoeq1findwhichissmaller.Thenyougettheta.

Gobacktothosenonzeropixels,transferthemtonewXandYbythethetayoufound.

FindcenterofmassXcYcandminXandminYbysort().

byhand

Ifyouneedtheoriginalequationoftheellipse

Justput[eq0]intothecanonicalform

maythe4thbewithu
285 1 2 10

Youcanfairlyeasilyfindthemajoraxis:picktworandompointsontheedge,andseeifyoucanincrease
thedistancebetweenthembyalternatelymovingeitherpointalongtheedge.Themajoraxisisuniquein
thatitmaximizesthedistancebetweenthetwopoints,andthefunctioniscontinuous.Goodstartingpoint
candidatesarethetwoextremesineitherXorYdirection.Breaksdownsomewhatforsmall,roundellipses
duetoroundingofpixelcoordinates.Youcanalsoeasilydeterminethecircumference.Anotherapproachis

I'veupdatedtheoriginalpostforthis.Sorryfortheambiguousdescriptionbefore.SinceIwanttodetectan
ellipseinarathercomplicatedsceneoronanobject,therearepointsthatisnotbelongtotheellipse.That's
whyIuseRANSACtodetectanellipse,ratherthanfitanellipse. AlbertK Apr2'14at17:55

Thanksforthedetailedexplanation!Thismethodseemstobequitegooforanimagethatonlycontainone
ellipse.However,formyapplicationthereareusuallyoneellipseandotherlinesrectanglesandetc.Butstill

Asforyourfinalgoal,youmaytry

findContoursand[fitEllipse]inOpenCV

Thepseudocodewillbe

1)someimageprocess

2)findallcontours

3)fiteachcontoursbyfitEllipse

hereispartofcodeIusebefore

[...imageprocess....yougetabwimage]

vector<vector<Point>>contours;
findContours(bwimage,contours,CV_RETR_LIST,CV_CHAIN_APPROX_NONE);

for(size_ti=0;i<contours.size();i++)
{
size_tcount=contours[i].size();

Matpointsf;
Mat(contours[i]).convertTo(pointsf,CV_32F);
RotatedRectbox=fitEllipse(pointsf);

if(box.size.width>20&&
box.size.height>20&&

box.size.width<80&&
box.size.height<80)
{
if(MAX(box.size.width,box.size.height)>MIN(box.size.width,box.size.height)*30)
continue;
//drawContours(SrcImage,contours,(int)i,Scalar::all(255),1,8);

ellipse(SrcImage,box,Scalar(0,0,255),1,CV_AA);
ellipse(SrcImage,box.center,box.size*0.5f,box.angle,0,360,Scalar(200,255,255),
1,CV_AA);
}
}

imshow("result",SrcImage);

maythe4thbewithu
285 1 2 10

Thislooksgood!ButhowcanIjudgewhethertheellipseIdetectedisgoodenough?Forexample,frommy
originalRANSACmethod,Icanusethenumberofpointsthatisclosetotheellipseasacriteria.Themore
pointsthebetter.Inthemethodyouproposed,howtorankthedetectedellipse?Thanks! AlbertK Apr4
'14at2:20