calib3d.hpp 169 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  16. // Third party copyrights are property of their respective owners.
  17. //
  18. // Redistribution and use in source and binary forms, with or without modification,
  19. // are permitted provided that the following conditions are met:
  20. //
  21. // * Redistribution's of source code must retain the above copyright notice,
  22. // this list of conditions and the following disclaimer.
  23. //
  24. // * Redistribution's in binary form must reproduce the above copyright notice,
  25. // this list of conditions and the following disclaimer in the documentation
  26. // and/or other materials provided with the distribution.
  27. //
  28. // * The name of the copyright holders may not be used to endorse or promote products
  29. // derived from this software without specific prior written permission.
  30. //
  31. // This software is provided by the copyright holders and contributors "as is" and
  32. // any express or implied warranties, including, but not limited to, the implied
  33. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  34. // In no event shall the Intel Corporation or contributors be liable for any direct,
  35. // indirect, incidental, special, exemplary, or consequential damages
  36. // (including, but not limited to, procurement of substitute goods or services;
  37. // loss of use, data, or profits; or business interruption) however caused
  38. // and on any theory of liability, whether in contract, strict liability,
  39. // or tort (including negligence or otherwise) arising in any way out of
  40. // the use of this software, even if advised of the possibility of such damage.
  41. //
  42. //M*/
  43. #ifndef OPENCV_CALIB3D_HPP
  44. #define OPENCV_CALIB3D_HPP
  45. #include "opencv2/core.hpp"
  46. #include "opencv2/features2d.hpp"
  47. #include "opencv2/core/affine.hpp"
  48. /**
  49. @defgroup calib3d Camera Calibration and 3D Reconstruction
  50. The functions in this section use a so-called pinhole camera model. The view of a scene
  51. is obtained by projecting a scene's 3D point \f$P_w\f$ into the image plane using a perspective
  52. transformation which forms the corresponding pixel \f$p\f$. Both \f$P_w\f$ and \f$p\f$ are
  53. represented in homogeneous coordinates, i.e. as 3D and 2D homogeneous vector respectively. You will
  54. find a brief introduction to projective geometry, homogeneous vectors and homogeneous
  55. transformations at the end of this section's introduction. For more succinct notation, we often drop
  56. the 'homogeneous' and say vector instead of homogeneous vector.
  57. The distortion-free projective transformation given by a pinhole camera model is shown below.
  58. \f[s \; p = A \begin{bmatrix} R|t \end{bmatrix} P_w,\f]
  59. where \f$P_w\f$ is a 3D point expressed with respect to the world coordinate system,
  60. \f$p\f$ is a 2D pixel in the image plane, \f$A\f$ is the camera intrinsic matrix,
  61. \f$R\f$ and \f$t\f$ are the rotation and translation that describe the change of coordinates from
  62. world to camera coordinate systems (or camera frame) and \f$s\f$ is the projective transformation's
  63. arbitrary scaling and not part of the camera model.
  64. The camera intrinsic matrix \f$A\f$ (notation used as in @cite Zhang2000 and also generally notated
  65. as \f$K\f$) projects 3D points given in the camera coordinate system to 2D pixel coordinates, i.e.
  66. \f[p = A P_c.\f]
  67. The camera intrinsic matrix \f$A\f$ is composed of the focal lengths \f$f_x\f$ and \f$f_y\f$, which are
  68. expressed in pixel units, and the principal point \f$(c_x, c_y)\f$, that is usually close to the
  69. image center:
  70. \f[A = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1},\f]
  71. and thus
  72. \f[s \vecthree{u}{v}{1} = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1} \vecthree{X_c}{Y_c}{Z_c}.\f]
  73. The matrix of intrinsic parameters does not depend on the scene viewed. So, once estimated, it can
  74. be re-used as long as the focal length is fixed (in case of a zoom lens). Thus, if an image from the
  75. camera is scaled by a factor, all of these parameters need to be scaled (multiplied/divided,
  76. respectively) by the same factor.
  77. The joint rotation-translation matrix \f$[R|t]\f$ is the matrix product of a projective
  78. transformation and a homogeneous transformation. The 3-by-4 projective transformation maps 3D points
  79. represented in camera coordinates to 2D poins in the image plane and represented in normalized
  80. camera coordinates \f$x' = X_c / Z_c\f$ and \f$y' = Y_c / Z_c\f$:
  81. \f[Z_c \begin{bmatrix}
  82. x' \\
  83. y' \\
  84. 1
  85. \end{bmatrix} = \begin{bmatrix}
  86. 1 & 0 & 0 & 0 \\
  87. 0 & 1 & 0 & 0 \\
  88. 0 & 0 & 1 & 0
  89. \end{bmatrix}
  90. \begin{bmatrix}
  91. X_c \\
  92. Y_c \\
  93. Z_c \\
  94. 1
  95. \end{bmatrix}.\f]
  96. The homogeneous transformation is encoded by the extrinsic parameters \f$R\f$ and \f$t\f$ and
  97. represents the change of basis from world coordinate system \f$w\f$ to the camera coordinate sytem
  98. \f$c\f$. Thus, given the representation of the point \f$P\f$ in world coordinates, \f$P_w\f$, we
  99. obtain \f$P\f$'s representation in the camera coordinate system, \f$P_c\f$, by
  100. \f[P_c = \begin{bmatrix}
  101. R & t \\
  102. 0 & 1
  103. \end{bmatrix} P_w,\f]
  104. This homogeneous transformation is composed out of \f$R\f$, a 3-by-3 rotation matrix, and \f$t\f$, a
  105. 3-by-1 translation vector:
  106. \f[\begin{bmatrix}
  107. R & t \\
  108. 0 & 1
  109. \end{bmatrix} = \begin{bmatrix}
  110. r_{11} & r_{12} & r_{13} & t_x \\
  111. r_{21} & r_{22} & r_{23} & t_y \\
  112. r_{31} & r_{32} & r_{33} & t_z \\
  113. 0 & 0 & 0 & 1
  114. \end{bmatrix},
  115. \f]
  116. and therefore
  117. \f[\begin{bmatrix}
  118. X_c \\
  119. Y_c \\
  120. Z_c \\
  121. 1
  122. \end{bmatrix} = \begin{bmatrix}
  123. r_{11} & r_{12} & r_{13} & t_x \\
  124. r_{21} & r_{22} & r_{23} & t_y \\
  125. r_{31} & r_{32} & r_{33} & t_z \\
  126. 0 & 0 & 0 & 1
  127. \end{bmatrix}
  128. \begin{bmatrix}
  129. X_w \\
  130. Y_w \\
  131. Z_w \\
  132. 1
  133. \end{bmatrix}.\f]
  134. Combining the projective transformation and the homogeneous transformation, we obtain the projective
  135. transformation that maps 3D points in world coordinates into 2D points in the image plane and in
  136. normalized camera coordinates:
  137. \f[Z_c \begin{bmatrix}
  138. x' \\
  139. y' \\
  140. 1
  141. \end{bmatrix} = \begin{bmatrix} R|t \end{bmatrix} \begin{bmatrix}
  142. X_w \\
  143. Y_w \\
  144. Z_w \\
  145. 1
  146. \end{bmatrix} = \begin{bmatrix}
  147. r_{11} & r_{12} & r_{13} & t_x \\
  148. r_{21} & r_{22} & r_{23} & t_y \\
  149. r_{31} & r_{32} & r_{33} & t_z
  150. \end{bmatrix}
  151. \begin{bmatrix}
  152. X_w \\
  153. Y_w \\
  154. Z_w \\
  155. 1
  156. \end{bmatrix},\f]
  157. with \f$x' = X_c / Z_c\f$ and \f$y' = Y_c / Z_c\f$. Putting the equations for instrincs and extrinsics together, we can write out
  158. \f$s \; p = A \begin{bmatrix} R|t \end{bmatrix} P_w\f$ as
  159. \f[s \vecthree{u}{v}{1} = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1}
  160. \begin{bmatrix}
  161. r_{11} & r_{12} & r_{13} & t_x \\
  162. r_{21} & r_{22} & r_{23} & t_y \\
  163. r_{31} & r_{32} & r_{33} & t_z
  164. \end{bmatrix}
  165. \begin{bmatrix}
  166. X_w \\
  167. Y_w \\
  168. Z_w \\
  169. 1
  170. \end{bmatrix}.\f]
  171. If \f$Z_c \ne 0\f$, the transformation above is equivalent to the following,
  172. \f[\begin{bmatrix}
  173. u \\
  174. v
  175. \end{bmatrix} = \begin{bmatrix}
  176. f_x X_c/Z_c + c_x \\
  177. f_y Y_c/Z_c + c_y
  178. \end{bmatrix}\f]
  179. with
  180. \f[\vecthree{X_c}{Y_c}{Z_c} = \begin{bmatrix}
  181. R|t
  182. \end{bmatrix} \begin{bmatrix}
  183. X_w \\
  184. Y_w \\
  185. Z_w \\
  186. 1
  187. \end{bmatrix}.\f]
  188. The following figure illustrates the pinhole camera model.
  189. ![Pinhole camera model](pics/pinhole_camera_model.png)
  190. Real lenses usually have some distortion, mostly radial distortion, and slight tangential distortion.
  191. So, the above model is extended as:
  192. \f[\begin{bmatrix}
  193. u \\
  194. v
  195. \end{bmatrix} = \begin{bmatrix}
  196. f_x x'' + c_x \\
  197. f_y y'' + c_y
  198. \end{bmatrix}\f]
  199. where
  200. \f[\begin{bmatrix}
  201. x'' \\
  202. y''
  203. \end{bmatrix} = \begin{bmatrix}
  204. x' \frac{1 + k_1 r^2 + k_2 r^4 + k_3 r^6}{1 + k_4 r^2 + k_5 r^4 + k_6 r^6} + 2 p_1 x' y' + p_2(r^2 + 2 x'^2) + s_1 r^2 + s_2 r^4 \\
  205. y' \frac{1 + k_1 r^2 + k_2 r^4 + k_3 r^6}{1 + k_4 r^2 + k_5 r^4 + k_6 r^6} + p_1 (r^2 + 2 y'^2) + 2 p_2 x' y' + s_3 r^2 + s_4 r^4 \\
  206. \end{bmatrix}\f]
  207. with
  208. \f[r^2 = x'^2 + y'^2\f]
  209. and
  210. \f[\begin{bmatrix}
  211. x'\\
  212. y'
  213. \end{bmatrix} = \begin{bmatrix}
  214. X_c/Z_c \\
  215. Y_c/Z_c
  216. \end{bmatrix},\f]
  217. if \f$Z_c \ne 0\f$.
  218. The distortion parameters are the radial coefficients \f$k_1\f$, \f$k_2\f$, \f$k_3\f$, \f$k_4\f$, \f$k_5\f$, and \f$k_6\f$
  219. ,\f$p_1\f$ and \f$p_2\f$ are the tangential distortion coefficients, and \f$s_1\f$, \f$s_2\f$, \f$s_3\f$, and \f$s_4\f$,
  220. are the thin prism distortion coefficients. Higher-order coefficients are not considered in OpenCV.
  221. The next figures show two common types of radial distortion: barrel distortion
  222. (\f$ 1 + k_1 r^2 + k_2 r^4 + k_3 r^6 \f$ monotonically decreasing)
  223. and pincushion distortion (\f$ 1 + k_1 r^2 + k_2 r^4 + k_3 r^6 \f$ monotonically increasing).
  224. Radial distortion is always monotonic for real lenses,
  225. and if the estimator produces a non-monotonic result,
  226. this should be considered a calibration failure.
  227. More generally, radial distortion must be monotonic and the distortion function must be bijective.
  228. A failed estimation result may look deceptively good near the image center
  229. but will work poorly in e.g. AR/SFM applications.
  230. The optimization method used in OpenCV camera calibration does not include these constraints as
  231. the framework does not support the required integer programming and polynomial inequalities.
  232. See [issue #15992](https://github.com/opencv/opencv/issues/15992) for additional information.
  233. ![](pics/distortion_examples.png)
  234. ![](pics/distortion_examples2.png)
  235. In some cases, the image sensor may be tilted in order to focus an oblique plane in front of the
  236. camera (Scheimpflug principle). This can be useful for particle image velocimetry (PIV) or
  237. triangulation with a laser fan. The tilt causes a perspective distortion of \f$x''\f$ and
  238. \f$y''\f$. This distortion can be modeled in the following way, see e.g. @cite Louhichi07.
  239. \f[\begin{bmatrix}
  240. u \\
  241. v
  242. \end{bmatrix} = \begin{bmatrix}
  243. f_x x''' + c_x \\
  244. f_y y''' + c_y
  245. \end{bmatrix},\f]
  246. where
  247. \f[s\vecthree{x'''}{y'''}{1} =
  248. \vecthreethree{R_{33}(\tau_x, \tau_y)}{0}{-R_{13}(\tau_x, \tau_y)}
  249. {0}{R_{33}(\tau_x, \tau_y)}{-R_{23}(\tau_x, \tau_y)}
  250. {0}{0}{1} R(\tau_x, \tau_y) \vecthree{x''}{y''}{1}\f]
  251. and the matrix \f$R(\tau_x, \tau_y)\f$ is defined by two rotations with angular parameter
  252. \f$\tau_x\f$ and \f$\tau_y\f$, respectively,
  253. \f[
  254. R(\tau_x, \tau_y) =
  255. \vecthreethree{\cos(\tau_y)}{0}{-\sin(\tau_y)}{0}{1}{0}{\sin(\tau_y)}{0}{\cos(\tau_y)}
  256. \vecthreethree{1}{0}{0}{0}{\cos(\tau_x)}{\sin(\tau_x)}{0}{-\sin(\tau_x)}{\cos(\tau_x)} =
  257. \vecthreethree{\cos(\tau_y)}{\sin(\tau_y)\sin(\tau_x)}{-\sin(\tau_y)\cos(\tau_x)}
  258. {0}{\cos(\tau_x)}{\sin(\tau_x)}
  259. {\sin(\tau_y)}{-\cos(\tau_y)\sin(\tau_x)}{\cos(\tau_y)\cos(\tau_x)}.
  260. \f]
  261. In the functions below the coefficients are passed or returned as
  262. \f[(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \tau_x, \tau_y]]]])\f]
  263. vector. That is, if the vector contains four elements, it means that \f$k_3=0\f$ . The distortion
  264. coefficients do not depend on the scene viewed. Thus, they also belong to the intrinsic camera
  265. parameters. And they remain the same regardless of the captured image resolution. If, for example, a
  266. camera has been calibrated on images of 320 x 240 resolution, absolutely the same distortion
  267. coefficients can be used for 640 x 480 images from the same camera while \f$f_x\f$, \f$f_y\f$,
  268. \f$c_x\f$, and \f$c_y\f$ need to be scaled appropriately.
  269. The functions below use the above model to do the following:
  270. - Project 3D points to the image plane given intrinsic and extrinsic parameters.
  271. - Compute extrinsic parameters given intrinsic parameters, a few 3D points, and their
  272. projections.
  273. - Estimate intrinsic and extrinsic camera parameters from several views of a known calibration
  274. pattern (every view is described by several 3D-2D point correspondences).
  275. - Estimate the relative position and orientation of the stereo camera "heads" and compute the
  276. *rectification* transformation that makes the camera optical axes parallel.
  277. <B> Homogeneous Coordinates </B><br>
  278. Homogeneous Coordinates are a system of coordinates that are used in projective geometry. Their use
  279. allows to represent points at infinity by finite coordinates and simplifies formulas when compared
  280. to the cartesian counterparts, e.g. they have the advantage that affine transformations can be
  281. expressed as linear homogeneous transformation.
  282. One obtains the homogeneous vector \f$P_h\f$ by appending a 1 along an n-dimensional cartesian
  283. vector \f$P\f$ e.g. for a 3D cartesian vector the mapping \f$P \rightarrow P_h\f$ is:
  284. \f[\begin{bmatrix}
  285. X \\
  286. Y \\
  287. Z
  288. \end{bmatrix} \rightarrow \begin{bmatrix}
  289. X \\
  290. Y \\
  291. Z \\
  292. 1
  293. \end{bmatrix}.\f]
  294. For the inverse mapping \f$P_h \rightarrow P\f$, one divides all elements of the homogeneous vector
  295. by its last element, e.g. for a 3D homogeneous vector one gets its 2D cartesian counterpart by:
  296. \f[\begin{bmatrix}
  297. X \\
  298. Y \\
  299. W
  300. \end{bmatrix} \rightarrow \begin{bmatrix}
  301. X / W \\
  302. Y / W
  303. \end{bmatrix},\f]
  304. if \f$W \ne 0\f$.
  305. Due to this mapping, all multiples \f$k P_h\f$, for \f$k \ne 0\f$, of a homogeneous point represent
  306. the same point \f$P_h\f$. An intuitive understanding of this property is that under a projective
  307. transformation, all multiples of \f$P_h\f$ are mapped to the same point. This is the physical
  308. observation one does for pinhole cameras, as all points along a ray through the camera's pinhole are
  309. projected to the same image point, e.g. all points along the red ray in the image of the pinhole
  310. camera model above would be mapped to the same image coordinate. This property is also the source
  311. for the scale ambiguity s in the equation of the pinhole camera model.
  312. As mentioned, by using homogeneous coordinates we can express any change of basis parameterized by
  313. \f$R\f$ and \f$t\f$ as a linear transformation, e.g. for the change of basis from coordinate system
  314. 0 to coordinate system 1 becomes:
  315. \f[P_1 = R P_0 + t \rightarrow P_{h_1} = \begin{bmatrix}
  316. R & t \\
  317. 0 & 1
  318. \end{bmatrix} P_{h_0}.\f]
  319. @note
  320. - Many functions in this module take a camera intrinsic matrix as an input parameter. Although all
  321. functions assume the same structure of this parameter, they may name it differently. The
  322. parameter's description, however, will be clear in that a camera intrinsic matrix with the structure
  323. shown above is required.
  324. - A calibration sample for 3 cameras in a horizontal position can be found at
  325. opencv_source_code/samples/cpp/3calibration.cpp
  326. - A calibration sample based on a sequence of images can be found at
  327. opencv_source_code/samples/cpp/calibration.cpp
  328. - A calibration sample in order to do 3D reconstruction can be found at
  329. opencv_source_code/samples/cpp/build3dmodel.cpp
  330. - A calibration example on stereo calibration can be found at
  331. opencv_source_code/samples/cpp/stereo_calib.cpp
  332. - A calibration example on stereo matching can be found at
  333. opencv_source_code/samples/cpp/stereo_match.cpp
  334. - (Python) A camera calibration sample can be found at
  335. opencv_source_code/samples/python/calibrate.py
  336. @{
  337. @defgroup calib3d_fisheye Fisheye camera model
  338. Definitions: Let P be a point in 3D of coordinates X in the world reference frame (stored in the
  339. matrix X) The coordinate vector of P in the camera reference frame is:
  340. \f[Xc = R X + T\f]
  341. where R is the rotation matrix corresponding to the rotation vector om: R = rodrigues(om); call x, y
  342. and z the 3 coordinates of Xc:
  343. \f[x = Xc_1 \\ y = Xc_2 \\ z = Xc_3\f]
  344. The pinhole projection coordinates of P is [a; b] where
  345. \f[a = x / z \ and \ b = y / z \\ r^2 = a^2 + b^2 \\ \theta = atan(r)\f]
  346. Fisheye distortion:
  347. \f[\theta_d = \theta (1 + k_1 \theta^2 + k_2 \theta^4 + k_3 \theta^6 + k_4 \theta^8)\f]
  348. The distorted point coordinates are [x'; y'] where
  349. \f[x' = (\theta_d / r) a \\ y' = (\theta_d / r) b \f]
  350. Finally, conversion into pixel coordinates: The final pixel coordinates vector [u; v] where:
  351. \f[u = f_x (x' + \alpha y') + c_x \\
  352. v = f_y y' + c_y\f]
  353. @defgroup calib3d_c C API
  354. @}
  355. */
  356. namespace cv
  357. {
  358. //! @addtogroup calib3d
  359. //! @{
  360. //! type of the robust estimation algorithm
  361. enum { LMEDS = 4, //!< least-median of squares algorithm
  362. RANSAC = 8, //!< RANSAC algorithm
  363. RHO = 16 //!< RHO algorithm
  364. };
  365. enum SolvePnPMethod {
  366. SOLVEPNP_ITERATIVE = 0,
  367. SOLVEPNP_EPNP = 1, //!< EPnP: Efficient Perspective-n-Point Camera Pose Estimation @cite lepetit2009epnp
  368. SOLVEPNP_P3P = 2, //!< Complete Solution Classification for the Perspective-Three-Point Problem @cite gao2003complete
  369. SOLVEPNP_DLS = 3, //!< **Broken implementation. Using this flag will fallback to EPnP.** \n
  370. //!< A Direct Least-Squares (DLS) Method for PnP @cite hesch2011direct
  371. SOLVEPNP_UPNP = 4, //!< **Broken implementation. Using this flag will fallback to EPnP.** \n
  372. //!< Exhaustive Linearization for Robust Camera Pose and Focal Length Estimation @cite penate2013exhaustive
  373. SOLVEPNP_AP3P = 5, //!< An Efficient Algebraic Solution to the Perspective-Three-Point Problem @cite Ke17
  374. SOLVEPNP_IPPE = 6, //!< Infinitesimal Plane-Based Pose Estimation @cite Collins14 \n
  375. //!< Object points must be coplanar.
  376. SOLVEPNP_IPPE_SQUARE = 7, //!< Infinitesimal Plane-Based Pose Estimation @cite Collins14 \n
  377. //!< This is a special case suitable for marker pose estimation.\n
  378. //!< 4 coplanar object points must be defined in the following order:
  379. //!< - point 0: [-squareLength / 2, squareLength / 2, 0]
  380. //!< - point 1: [ squareLength / 2, squareLength / 2, 0]
  381. //!< - point 2: [ squareLength / 2, -squareLength / 2, 0]
  382. //!< - point 3: [-squareLength / 2, -squareLength / 2, 0]
  383. #ifndef CV_DOXYGEN
  384. SOLVEPNP_MAX_COUNT //!< Used for count
  385. #endif
  386. };
  387. enum { CALIB_CB_ADAPTIVE_THRESH = 1,
  388. CALIB_CB_NORMALIZE_IMAGE = 2,
  389. CALIB_CB_FILTER_QUADS = 4,
  390. CALIB_CB_FAST_CHECK = 8
  391. };
  392. enum { CALIB_CB_SYMMETRIC_GRID = 1,
  393. CALIB_CB_ASYMMETRIC_GRID = 2,
  394. CALIB_CB_CLUSTERING = 4
  395. };
  396. enum { CALIB_USE_INTRINSIC_GUESS = 0x00001,
  397. CALIB_FIX_ASPECT_RATIO = 0x00002,
  398. CALIB_FIX_PRINCIPAL_POINT = 0x00004,
  399. CALIB_ZERO_TANGENT_DIST = 0x00008,
  400. CALIB_FIX_FOCAL_LENGTH = 0x00010,
  401. CALIB_FIX_K1 = 0x00020,
  402. CALIB_FIX_K2 = 0x00040,
  403. CALIB_FIX_K3 = 0x00080,
  404. CALIB_FIX_K4 = 0x00800,
  405. CALIB_FIX_K5 = 0x01000,
  406. CALIB_FIX_K6 = 0x02000,
  407. CALIB_RATIONAL_MODEL = 0x04000,
  408. CALIB_THIN_PRISM_MODEL = 0x08000,
  409. CALIB_FIX_S1_S2_S3_S4 = 0x10000,
  410. CALIB_TILTED_MODEL = 0x40000,
  411. CALIB_FIX_TAUX_TAUY = 0x80000,
  412. CALIB_USE_QR = 0x100000, //!< use QR instead of SVD decomposition for solving. Faster but potentially less precise
  413. CALIB_FIX_TANGENT_DIST = 0x200000,
  414. // only for stereo
  415. CALIB_FIX_INTRINSIC = 0x00100,
  416. CALIB_SAME_FOCAL_LENGTH = 0x00200,
  417. // for stereo rectification
  418. CALIB_ZERO_DISPARITY = 0x00400,
  419. CALIB_USE_LU = (1 << 17), //!< use LU instead of SVD decomposition for solving. much faster but potentially less precise
  420. CALIB_USE_EXTRINSIC_GUESS = (1 << 22) //!< for stereoCalibrate
  421. };
  422. //! the algorithm for finding fundamental matrix
  423. enum { FM_7POINT = 1, //!< 7-point algorithm
  424. FM_8POINT = 2, //!< 8-point algorithm
  425. FM_LMEDS = 4, //!< least-median algorithm. 7-point algorithm is used.
  426. FM_RANSAC = 8 //!< RANSAC algorithm. It needs at least 15 points. 7-point algorithm is used.
  427. };
  428. enum HandEyeCalibrationMethod
  429. {
  430. CALIB_HAND_EYE_TSAI = 0, //!< A New Technique for Fully Autonomous and Efficient 3D Robotics Hand/Eye Calibration @cite Tsai89
  431. CALIB_HAND_EYE_PARK = 1, //!< Robot Sensor Calibration: Solving AX = XB on the Euclidean Group @cite Park94
  432. CALIB_HAND_EYE_HORAUD = 2, //!< Hand-eye Calibration @cite Horaud95
  433. CALIB_HAND_EYE_ANDREFF = 3, //!< On-line Hand-Eye Calibration @cite Andreff99
  434. CALIB_HAND_EYE_DANIILIDIS = 4 //!< Hand-Eye Calibration Using Dual Quaternions @cite Daniilidis98
  435. };
  436. /** @brief Converts a rotation matrix to a rotation vector or vice versa.
  437. @param src Input rotation vector (3x1 or 1x3) or rotation matrix (3x3).
  438. @param dst Output rotation matrix (3x3) or rotation vector (3x1 or 1x3), respectively.
  439. @param jacobian Optional output Jacobian matrix, 3x9 or 9x3, which is a matrix of partial
  440. derivatives of the output array components with respect to the input array components.
  441. \f[\begin{array}{l} \theta \leftarrow norm(r) \\ r \leftarrow r/ \theta \\ R = \cos(\theta) I + (1- \cos{\theta} ) r r^T + \sin(\theta) \vecthreethree{0}{-r_z}{r_y}{r_z}{0}{-r_x}{-r_y}{r_x}{0} \end{array}\f]
  442. Inverse transformation can be also done easily, since
  443. \f[\sin ( \theta ) \vecthreethree{0}{-r_z}{r_y}{r_z}{0}{-r_x}{-r_y}{r_x}{0} = \frac{R - R^T}{2}\f]
  444. A rotation vector is a convenient and most compact representation of a rotation matrix (since any
  445. rotation matrix has just 3 degrees of freedom). The representation is used in the global 3D geometry
  446. optimization procedures like @ref calibrateCamera, @ref stereoCalibrate, or @ref solvePnP .
  447. @note More information about the computation of the derivative of a 3D rotation matrix with respect to its exponential coordinate
  448. can be found in:
  449. - A Compact Formula for the Derivative of a 3-D Rotation in Exponential Coordinates, Guillermo Gallego, Anthony J. Yezzi @cite Gallego2014ACF
  450. @note Useful information on SE(3) and Lie Groups can be found in:
  451. - A tutorial on SE(3) transformation parameterizations and on-manifold optimization, Jose-Luis Blanco @cite blanco2010tutorial
  452. - Lie Groups for 2D and 3D Transformation, Ethan Eade @cite Eade17
  453. - A micro Lie theory for state estimation in robotics, Joan Solà, Jérémie Deray, Dinesh Atchuthan @cite Sol2018AML
  454. */
  455. CV_EXPORTS_W void Rodrigues( InputArray src, OutputArray dst, OutputArray jacobian = noArray() );
  456. /** @example samples/cpp/tutorial_code/features2D/Homography/pose_from_homography.cpp
  457. An example program about pose estimation from coplanar points
  458. Check @ref tutorial_homography "the corresponding tutorial" for more details
  459. */
  460. /** @brief Finds a perspective transformation between two planes.
  461. @param srcPoints Coordinates of the points in the original plane, a matrix of the type CV_32FC2
  462. or vector\<Point2f\> .
  463. @param dstPoints Coordinates of the points in the target plane, a matrix of the type CV_32FC2 or
  464. a vector\<Point2f\> .
  465. @param method Method used to compute a homography matrix. The following methods are possible:
  466. - **0** - a regular method using all the points, i.e., the least squares method
  467. - **RANSAC** - RANSAC-based robust method
  468. - **LMEDS** - Least-Median robust method
  469. - **RHO** - PROSAC-based robust method
  470. @param ransacReprojThreshold Maximum allowed reprojection error to treat a point pair as an inlier
  471. (used in the RANSAC and RHO methods only). That is, if
  472. \f[\| \texttt{dstPoints} _i - \texttt{convertPointsHomogeneous} ( \texttt{H} * \texttt{srcPoints} _i) \|_2 > \texttt{ransacReprojThreshold}\f]
  473. then the point \f$i\f$ is considered as an outlier. If srcPoints and dstPoints are measured in pixels,
  474. it usually makes sense to set this parameter somewhere in the range of 1 to 10.
  475. @param mask Optional output mask set by a robust method ( RANSAC or LMEDS ). Note that the input
  476. mask values are ignored.
  477. @param maxIters The maximum number of RANSAC iterations.
  478. @param confidence Confidence level, between 0 and 1.
  479. The function finds and returns the perspective transformation \f$H\f$ between the source and the
  480. destination planes:
  481. \f[s_i \vecthree{x'_i}{y'_i}{1} \sim H \vecthree{x_i}{y_i}{1}\f]
  482. so that the back-projection error
  483. \f[\sum _i \left ( x'_i- \frac{h_{11} x_i + h_{12} y_i + h_{13}}{h_{31} x_i + h_{32} y_i + h_{33}} \right )^2+ \left ( y'_i- \frac{h_{21} x_i + h_{22} y_i + h_{23}}{h_{31} x_i + h_{32} y_i + h_{33}} \right )^2\f]
  484. is minimized. If the parameter method is set to the default value 0, the function uses all the point
  485. pairs to compute an initial homography estimate with a simple least-squares scheme.
  486. However, if not all of the point pairs ( \f$srcPoints_i\f$, \f$dstPoints_i\f$ ) fit the rigid perspective
  487. transformation (that is, there are some outliers), this initial estimate will be poor. In this case,
  488. you can use one of the three robust methods. The methods RANSAC, LMeDS and RHO try many different
  489. random subsets of the corresponding point pairs (of four pairs each, collinear pairs are discarded), estimate the homography matrix
  490. using this subset and a simple least-squares algorithm, and then compute the quality/goodness of the
  491. computed homography (which is the number of inliers for RANSAC or the least median re-projection error for
  492. LMeDS). The best subset is then used to produce the initial estimate of the homography matrix and
  493. the mask of inliers/outliers.
  494. Regardless of the method, robust or not, the computed homography matrix is refined further (using
  495. inliers only in case of a robust method) with the Levenberg-Marquardt method to reduce the
  496. re-projection error even more.
  497. The methods RANSAC and RHO can handle practically any ratio of outliers but need a threshold to
  498. distinguish inliers from outliers. The method LMeDS does not need any threshold but it works
  499. correctly only when there are more than 50% of inliers. Finally, if there are no outliers and the
  500. noise is rather small, use the default method (method=0).
  501. The function is used to find initial intrinsic and extrinsic matrices. Homography matrix is
  502. determined up to a scale. Thus, it is normalized so that \f$h_{33}=1\f$. Note that whenever an \f$H\f$ matrix
  503. cannot be estimated, an empty one will be returned.
  504. @sa
  505. getAffineTransform, estimateAffine2D, estimateAffinePartial2D, getPerspectiveTransform, warpPerspective,
  506. perspectiveTransform
  507. */
  508. CV_EXPORTS_W Mat findHomography( InputArray srcPoints, InputArray dstPoints,
  509. int method = 0, double ransacReprojThreshold = 3,
  510. OutputArray mask=noArray(), const int maxIters = 2000,
  511. const double confidence = 0.995);
  512. /** @overload */
  513. CV_EXPORTS Mat findHomography( InputArray srcPoints, InputArray dstPoints,
  514. OutputArray mask, int method = 0, double ransacReprojThreshold = 3 );
  515. /** @brief Computes an RQ decomposition of 3x3 matrices.
  516. @param src 3x3 input matrix.
  517. @param mtxR Output 3x3 upper-triangular matrix.
  518. @param mtxQ Output 3x3 orthogonal matrix.
  519. @param Qx Optional output 3x3 rotation matrix around x-axis.
  520. @param Qy Optional output 3x3 rotation matrix around y-axis.
  521. @param Qz Optional output 3x3 rotation matrix around z-axis.
  522. The function computes a RQ decomposition using the given rotations. This function is used in
  523. decomposeProjectionMatrix to decompose the left 3x3 submatrix of a projection matrix into a camera
  524. and a rotation matrix.
  525. It optionally returns three rotation matrices, one for each axis, and the three Euler angles in
  526. degrees (as the return value) that could be used in OpenGL. Note, there is always more than one
  527. sequence of rotations about the three principal axes that results in the same orientation of an
  528. object, e.g. see @cite Slabaugh . Returned tree rotation matrices and corresponding three Euler angles
  529. are only one of the possible solutions.
  530. */
  531. CV_EXPORTS_W Vec3d RQDecomp3x3( InputArray src, OutputArray mtxR, OutputArray mtxQ,
  532. OutputArray Qx = noArray(),
  533. OutputArray Qy = noArray(),
  534. OutputArray Qz = noArray());
  535. /** @brief Decomposes a projection matrix into a rotation matrix and a camera intrinsic matrix.
  536. @param projMatrix 3x4 input projection matrix P.
  537. @param cameraMatrix Output 3x3 camera intrinsic matrix \f$\cameramatrix{A}\f$.
  538. @param rotMatrix Output 3x3 external rotation matrix R.
  539. @param transVect Output 4x1 translation vector T.
  540. @param rotMatrixX Optional 3x3 rotation matrix around x-axis.
  541. @param rotMatrixY Optional 3x3 rotation matrix around y-axis.
  542. @param rotMatrixZ Optional 3x3 rotation matrix around z-axis.
  543. @param eulerAngles Optional three-element vector containing three Euler angles of rotation in
  544. degrees.
  545. The function computes a decomposition of a projection matrix into a calibration and a rotation
  546. matrix and the position of a camera.
  547. It optionally returns three rotation matrices, one for each axis, and three Euler angles that could
  548. be used in OpenGL. Note, there is always more than one sequence of rotations about the three
  549. principal axes that results in the same orientation of an object, e.g. see @cite Slabaugh . Returned
  550. tree rotation matrices and corresponding three Euler angles are only one of the possible solutions.
  551. The function is based on RQDecomp3x3 .
  552. */
  553. CV_EXPORTS_W void decomposeProjectionMatrix( InputArray projMatrix, OutputArray cameraMatrix,
  554. OutputArray rotMatrix, OutputArray transVect,
  555. OutputArray rotMatrixX = noArray(),
  556. OutputArray rotMatrixY = noArray(),
  557. OutputArray rotMatrixZ = noArray(),
  558. OutputArray eulerAngles =noArray() );
  559. /** @brief Computes partial derivatives of the matrix product for each multiplied matrix.
  560. @param A First multiplied matrix.
  561. @param B Second multiplied matrix.
  562. @param dABdA First output derivative matrix d(A\*B)/dA of size
  563. \f$\texttt{A.rows*B.cols} \times {A.rows*A.cols}\f$ .
  564. @param dABdB Second output derivative matrix d(A\*B)/dB of size
  565. \f$\texttt{A.rows*B.cols} \times {B.rows*B.cols}\f$ .
  566. The function computes partial derivatives of the elements of the matrix product \f$A*B\f$ with regard to
  567. the elements of each of the two input matrices. The function is used to compute the Jacobian
  568. matrices in stereoCalibrate but can also be used in any other similar optimization function.
  569. */
  570. CV_EXPORTS_W void matMulDeriv( InputArray A, InputArray B, OutputArray dABdA, OutputArray dABdB );
  571. /** @brief Combines two rotation-and-shift transformations.
  572. @param rvec1 First rotation vector.
  573. @param tvec1 First translation vector.
  574. @param rvec2 Second rotation vector.
  575. @param tvec2 Second translation vector.
  576. @param rvec3 Output rotation vector of the superposition.
  577. @param tvec3 Output translation vector of the superposition.
  578. @param dr3dr1 Optional output derivative of rvec3 with regard to rvec1
  579. @param dr3dt1 Optional output derivative of rvec3 with regard to tvec1
  580. @param dr3dr2 Optional output derivative of rvec3 with regard to rvec2
  581. @param dr3dt2 Optional output derivative of rvec3 with regard to tvec2
  582. @param dt3dr1 Optional output derivative of tvec3 with regard to rvec1
  583. @param dt3dt1 Optional output derivative of tvec3 with regard to tvec1
  584. @param dt3dr2 Optional output derivative of tvec3 with regard to rvec2
  585. @param dt3dt2 Optional output derivative of tvec3 with regard to tvec2
  586. The functions compute:
  587. \f[\begin{array}{l} \texttt{rvec3} = \mathrm{rodrigues} ^{-1} \left ( \mathrm{rodrigues} ( \texttt{rvec2} ) \cdot \mathrm{rodrigues} ( \texttt{rvec1} ) \right ) \\ \texttt{tvec3} = \mathrm{rodrigues} ( \texttt{rvec2} ) \cdot \texttt{tvec1} + \texttt{tvec2} \end{array} ,\f]
  588. where \f$\mathrm{rodrigues}\f$ denotes a rotation vector to a rotation matrix transformation, and
  589. \f$\mathrm{rodrigues}^{-1}\f$ denotes the inverse transformation. See Rodrigues for details.
  590. Also, the functions can compute the derivatives of the output vectors with regards to the input
  591. vectors (see matMulDeriv ). The functions are used inside stereoCalibrate but can also be used in
  592. your own code where Levenberg-Marquardt or another gradient-based solver is used to optimize a
  593. function that contains a matrix multiplication.
  594. */
  595. CV_EXPORTS_W void composeRT( InputArray rvec1, InputArray tvec1,
  596. InputArray rvec2, InputArray tvec2,
  597. OutputArray rvec3, OutputArray tvec3,
  598. OutputArray dr3dr1 = noArray(), OutputArray dr3dt1 = noArray(),
  599. OutputArray dr3dr2 = noArray(), OutputArray dr3dt2 = noArray(),
  600. OutputArray dt3dr1 = noArray(), OutputArray dt3dt1 = noArray(),
  601. OutputArray dt3dr2 = noArray(), OutputArray dt3dt2 = noArray() );
  602. /** @brief Projects 3D points to an image plane.
  603. @param objectPoints Array of object points expressed wrt. the world coordinate frame. A 3xN/Nx3
  604. 1-channel or 1xN/Nx1 3-channel (or vector\<Point3f\> ), where N is the number of points in the view.
  605. @param rvec The rotation vector (@ref Rodrigues) that, together with tvec, performs a change of
  606. basis from world to camera coordinate system, see @ref calibrateCamera for details.
  607. @param tvec The translation vector, see parameter description above.
  608. @param cameraMatrix Camera intrinsic matrix \f$\cameramatrix{A}\f$ .
  609. @param distCoeffs Input vector of distortion coefficients
  610. \f$\distcoeffs\f$ . If the vector is empty, the zero distortion coefficients are assumed.
  611. @param imagePoints Output array of image points, 1xN/Nx1 2-channel, or
  612. vector\<Point2f\> .
  613. @param jacobian Optional output 2Nx(10+\<numDistCoeffs\>) jacobian matrix of derivatives of image
  614. points with respect to components of the rotation vector, translation vector, focal lengths,
  615. coordinates of the principal point and the distortion coefficients. In the old interface different
  616. components of the jacobian are returned via different output parameters.
  617. @param aspectRatio Optional "fixed aspect ratio" parameter. If the parameter is not 0, the
  618. function assumes that the aspect ratio (\f$f_x / f_y\f$) is fixed and correspondingly adjusts the
  619. jacobian matrix.
  620. The function computes the 2D projections of 3D points to the image plane, given intrinsic and
  621. extrinsic camera parameters. Optionally, the function computes Jacobians -matrices of partial
  622. derivatives of image points coordinates (as functions of all the input parameters) with respect to
  623. the particular parameters, intrinsic and/or extrinsic. The Jacobians are used during the global
  624. optimization in @ref calibrateCamera, @ref solvePnP, and @ref stereoCalibrate. The function itself
  625. can also be used to compute a re-projection error, given the current intrinsic and extrinsic
  626. parameters.
  627. @note By setting rvec = tvec = \f$[0, 0, 0]\f$, or by setting cameraMatrix to a 3x3 identity matrix,
  628. or by passing zero distortion coefficients, one can get various useful partial cases of the
  629. function. This means, one can compute the distorted coordinates for a sparse set of points or apply
  630. a perspective transformation (and also compute the derivatives) in the ideal zero-distortion setup.
  631. */
  632. CV_EXPORTS_W void projectPoints( InputArray objectPoints,
  633. InputArray rvec, InputArray tvec,
  634. InputArray cameraMatrix, InputArray distCoeffs,
  635. OutputArray imagePoints,
  636. OutputArray jacobian = noArray(),
  637. double aspectRatio = 0 );
  638. /** @example samples/cpp/tutorial_code/features2D/Homography/homography_from_camera_displacement.cpp
  639. An example program about homography from the camera displacement
  640. Check @ref tutorial_homography "the corresponding tutorial" for more details
  641. */
  642. /** @brief Finds an object pose from 3D-2D point correspondences.
  643. This function returns the rotation and the translation vectors that transform a 3D point expressed in the object
  644. coordinate frame to the camera coordinate frame, using different methods:
  645. - P3P methods (@ref SOLVEPNP_P3P, @ref SOLVEPNP_AP3P): need 4 input points to return a unique solution.
  646. - @ref SOLVEPNP_IPPE Input points must be >= 4 and object points must be coplanar.
  647. - @ref SOLVEPNP_IPPE_SQUARE Special case suitable for marker pose estimation.
  648. Number of input points must be 4. Object points must be defined in the following order:
  649. - point 0: [-squareLength / 2, squareLength / 2, 0]
  650. - point 1: [ squareLength / 2, squareLength / 2, 0]
  651. - point 2: [ squareLength / 2, -squareLength / 2, 0]
  652. - point 3: [-squareLength / 2, -squareLength / 2, 0]
  653. - for all the other flags, number of input points must be >= 4 and object points can be in any configuration.
  654. @param objectPoints Array of object points in the object coordinate space, Nx3 1-channel or
  655. 1xN/Nx1 3-channel, where N is the number of points. vector\<Point3d\> can be also passed here.
  656. @param imagePoints Array of corresponding image points, Nx2 1-channel or 1xN/Nx1 2-channel,
  657. where N is the number of points. vector\<Point2d\> can be also passed here.
  658. @param cameraMatrix Input camera intrinsic matrix \f$\cameramatrix{A}\f$ .
  659. @param distCoeffs Input vector of distortion coefficients
  660. \f$\distcoeffs\f$. If the vector is NULL/empty, the zero distortion coefficients are
  661. assumed.
  662. @param rvec Output rotation vector (see @ref Rodrigues ) that, together with tvec, brings points from
  663. the model coordinate system to the camera coordinate system.
  664. @param tvec Output translation vector.
  665. @param useExtrinsicGuess Parameter used for #SOLVEPNP_ITERATIVE. If true (1), the function uses
  666. the provided rvec and tvec values as initial approximations of the rotation and translation
  667. vectors, respectively, and further optimizes them.
  668. @param flags Method for solving a PnP problem:
  669. - **SOLVEPNP_ITERATIVE** Iterative method is based on a Levenberg-Marquardt optimization. In
  670. this case the function finds such a pose that minimizes reprojection error, that is the sum
  671. of squared distances between the observed projections imagePoints and the projected (using
  672. @ref projectPoints ) objectPoints .
  673. - **SOLVEPNP_P3P** Method is based on the paper of X.S. Gao, X.-R. Hou, J. Tang, H.-F. Chang
  674. "Complete Solution Classification for the Perspective-Three-Point Problem" (@cite gao2003complete).
  675. In this case the function requires exactly four object and image points.
  676. - **SOLVEPNP_AP3P** Method is based on the paper of T. Ke, S. Roumeliotis
  677. "An Efficient Algebraic Solution to the Perspective-Three-Point Problem" (@cite Ke17).
  678. In this case the function requires exactly four object and image points.
  679. - **SOLVEPNP_EPNP** Method has been introduced by F. Moreno-Noguer, V. Lepetit and P. Fua in the
  680. paper "EPnP: Efficient Perspective-n-Point Camera Pose Estimation" (@cite lepetit2009epnp).
  681. - **SOLVEPNP_DLS** **Broken implementation. Using this flag will fallback to EPnP.** \n
  682. Method is based on the paper of J. Hesch and S. Roumeliotis.
  683. "A Direct Least-Squares (DLS) Method for PnP" (@cite hesch2011direct).
  684. - **SOLVEPNP_UPNP** **Broken implementation. Using this flag will fallback to EPnP.** \n
  685. Method is based on the paper of A. Penate-Sanchez, J. Andrade-Cetto,
  686. F. Moreno-Noguer. "Exhaustive Linearization for Robust Camera Pose and Focal Length
  687. Estimation" (@cite penate2013exhaustive). In this case the function also estimates the parameters \f$f_x\f$ and \f$f_y\f$
  688. assuming that both have the same value. Then the cameraMatrix is updated with the estimated
  689. focal length.
  690. - **SOLVEPNP_IPPE** Method is based on the paper of T. Collins and A. Bartoli.
  691. "Infinitesimal Plane-Based Pose Estimation" (@cite Collins14). This method requires coplanar object points.
  692. - **SOLVEPNP_IPPE_SQUARE** Method is based on the paper of Toby Collins and Adrien Bartoli.
  693. "Infinitesimal Plane-Based Pose Estimation" (@cite Collins14). This method is suitable for marker pose estimation.
  694. It requires 4 coplanar object points defined in the following order:
  695. - point 0: [-squareLength / 2, squareLength / 2, 0]
  696. - point 1: [ squareLength / 2, squareLength / 2, 0]
  697. - point 2: [ squareLength / 2, -squareLength / 2, 0]
  698. - point 3: [-squareLength / 2, -squareLength / 2, 0]
  699. The function estimates the object pose given a set of object points, their corresponding image
  700. projections, as well as the camera intrinsic matrix and the distortion coefficients, see the figure below
  701. (more precisely, the X-axis of the camera frame is pointing to the right, the Y-axis downward
  702. and the Z-axis forward).
  703. ![](pnp.jpg)
  704. Points expressed in the world frame \f$ \bf{X}_w \f$ are projected into the image plane \f$ \left[ u, v \right] \f$
  705. using the perspective projection model \f$ \Pi \f$ and the camera intrinsic parameters matrix \f$ \bf{A} \f$:
  706. \f[
  707. \begin{align*}
  708. \begin{bmatrix}
  709. u \\
  710. v \\
  711. 1
  712. \end{bmatrix} &=
  713. \bf{A} \hspace{0.1em} \Pi \hspace{0.2em} ^{c}\bf{T}_w
  714. \begin{bmatrix}
  715. X_{w} \\
  716. Y_{w} \\
  717. Z_{w} \\
  718. 1
  719. \end{bmatrix} \\
  720. \begin{bmatrix}
  721. u \\
  722. v \\
  723. 1
  724. \end{bmatrix} &=
  725. \begin{bmatrix}
  726. f_x & 0 & c_x \\
  727. 0 & f_y & c_y \\
  728. 0 & 0 & 1
  729. \end{bmatrix}
  730. \begin{bmatrix}
  731. 1 & 0 & 0 & 0 \\
  732. 0 & 1 & 0 & 0 \\
  733. 0 & 0 & 1 & 0
  734. \end{bmatrix}
  735. \begin{bmatrix}
  736. r_{11} & r_{12} & r_{13} & t_x \\
  737. r_{21} & r_{22} & r_{23} & t_y \\
  738. r_{31} & r_{32} & r_{33} & t_z \\
  739. 0 & 0 & 0 & 1
  740. \end{bmatrix}
  741. \begin{bmatrix}
  742. X_{w} \\
  743. Y_{w} \\
  744. Z_{w} \\
  745. 1
  746. \end{bmatrix}
  747. \end{align*}
  748. \f]
  749. The estimated pose is thus the rotation (`rvec`) and the translation (`tvec`) vectors that allow transforming
  750. a 3D point expressed in the world frame into the camera frame:
  751. \f[
  752. \begin{align*}
  753. \begin{bmatrix}
  754. X_c \\
  755. Y_c \\
  756. Z_c \\
  757. 1
  758. \end{bmatrix} &=
  759. \hspace{0.2em} ^{c}\bf{T}_w
  760. \begin{bmatrix}
  761. X_{w} \\
  762. Y_{w} \\
  763. Z_{w} \\
  764. 1
  765. \end{bmatrix} \\
  766. \begin{bmatrix}
  767. X_c \\
  768. Y_c \\
  769. Z_c \\
  770. 1
  771. \end{bmatrix} &=
  772. \begin{bmatrix}
  773. r_{11} & r_{12} & r_{13} & t_x \\
  774. r_{21} & r_{22} & r_{23} & t_y \\
  775. r_{31} & r_{32} & r_{33} & t_z \\
  776. 0 & 0 & 0 & 1
  777. \end{bmatrix}
  778. \begin{bmatrix}
  779. X_{w} \\
  780. Y_{w} \\
  781. Z_{w} \\
  782. 1
  783. \end{bmatrix}
  784. \end{align*}
  785. \f]
  786. @note
  787. - An example of how to use solvePnP for planar augmented reality can be found at
  788. opencv_source_code/samples/python/plane_ar.py
  789. - If you are using Python:
  790. - Numpy array slices won't work as input because solvePnP requires contiguous
  791. arrays (enforced by the assertion using cv::Mat::checkVector() around line 55 of
  792. modules/calib3d/src/solvepnp.cpp version 2.4.9)
  793. - The P3P algorithm requires image points to be in an array of shape (N,1,2) due
  794. to its calling of cv::undistortPoints (around line 75 of modules/calib3d/src/solvepnp.cpp version 2.4.9)
  795. which requires 2-channel information.
  796. - Thus, given some data D = np.array(...) where D.shape = (N,M), in order to use a subset of
  797. it as, e.g., imagePoints, one must effectively copy it into a new array: imagePoints =
  798. np.ascontiguousarray(D[:,:2]).reshape((N,1,2))
  799. - The methods **SOLVEPNP_DLS** and **SOLVEPNP_UPNP** cannot be used as the current implementations are
  800. unstable and sometimes give completely wrong results. If you pass one of these two
  801. flags, **SOLVEPNP_EPNP** method will be used instead.
  802. - The minimum number of points is 4 in the general case. In the case of **SOLVEPNP_P3P** and **SOLVEPNP_AP3P**
  803. methods, it is required to use exactly 4 points (the first 3 points are used to estimate all the solutions
  804. of the P3P problem, the last one is used to retain the best solution that minimizes the reprojection error).
  805. - With **SOLVEPNP_ITERATIVE** method and `useExtrinsicGuess=true`, the minimum number of points is 3 (3 points
  806. are sufficient to compute a pose but there are up to 4 solutions). The initial solution should be close to the
  807. global solution to converge.
  808. - With **SOLVEPNP_IPPE** input points must be >= 4 and object points must be coplanar.
  809. - With **SOLVEPNP_IPPE_SQUARE** this is a special case suitable for marker pose estimation.
  810. Number of input points must be 4. Object points must be defined in the following order:
  811. - point 0: [-squareLength / 2, squareLength / 2, 0]
  812. - point 1: [ squareLength / 2, squareLength / 2, 0]
  813. - point 2: [ squareLength / 2, -squareLength / 2, 0]
  814. - point 3: [-squareLength / 2, -squareLength / 2, 0]
  815. */
  816. CV_EXPORTS_W bool solvePnP( InputArray objectPoints, InputArray imagePoints,
  817. InputArray cameraMatrix, InputArray distCoeffs,
  818. OutputArray rvec, OutputArray tvec,
  819. bool useExtrinsicGuess = false, int flags = SOLVEPNP_ITERATIVE );
  820. /** @brief Finds an object pose from 3D-2D point correspondences using the RANSAC scheme.
  821. @param objectPoints Array of object points in the object coordinate space, Nx3 1-channel or
  822. 1xN/Nx1 3-channel, where N is the number of points. vector\<Point3d\> can be also passed here.
  823. @param imagePoints Array of corresponding image points, Nx2 1-channel or 1xN/Nx1 2-channel,
  824. where N is the number of points. vector\<Point2d\> can be also passed here.
  825. @param cameraMatrix Input camera intrinsic matrix \f$\cameramatrix{A}\f$ .
  826. @param distCoeffs Input vector of distortion coefficients
  827. \f$\distcoeffs\f$. If the vector is NULL/empty, the zero distortion coefficients are
  828. assumed.
  829. @param rvec Output rotation vector (see @ref Rodrigues ) that, together with tvec, brings points from
  830. the model coordinate system to the camera coordinate system.
  831. @param tvec Output translation vector.
  832. @param useExtrinsicGuess Parameter used for @ref SOLVEPNP_ITERATIVE. If true (1), the function uses
  833. the provided rvec and tvec values as initial approximations of the rotation and translation
  834. vectors, respectively, and further optimizes them.
  835. @param iterationsCount Number of iterations.
  836. @param reprojectionError Inlier threshold value used by the RANSAC procedure. The parameter value
  837. is the maximum allowed distance between the observed and computed point projections to consider it
  838. an inlier.
  839. @param confidence The probability that the algorithm produces a useful result.
  840. @param inliers Output vector that contains indices of inliers in objectPoints and imagePoints .
  841. @param flags Method for solving a PnP problem (see @ref solvePnP ).
  842. The function estimates an object pose given a set of object points, their corresponding image
  843. projections, as well as the camera intrinsic matrix and the distortion coefficients. This function finds such
  844. a pose that minimizes reprojection error, that is, the sum of squared distances between the observed
  845. projections imagePoints and the projected (using @ref projectPoints ) objectPoints. The use of RANSAC
  846. makes the function resistant to outliers.
  847. @note
  848. - An example of how to use solvePNPRansac for object detection can be found at
  849. opencv_source_code/samples/cpp/tutorial_code/calib3d/real_time_pose_estimation/
  850. - The default method used to estimate the camera pose for the Minimal Sample Sets step
  851. is #SOLVEPNP_EPNP. Exceptions are:
  852. - if you choose #SOLVEPNP_P3P or #SOLVEPNP_AP3P, these methods will be used.
  853. - if the number of input points is equal to 4, #SOLVEPNP_P3P is used.
  854. - The method used to estimate the camera pose using all the inliers is defined by the
  855. flags parameters unless it is equal to #SOLVEPNP_P3P or #SOLVEPNP_AP3P. In this case,
  856. the method #SOLVEPNP_EPNP will be used instead.
  857. */
  858. CV_EXPORTS_W bool solvePnPRansac( InputArray objectPoints, InputArray imagePoints,
  859. InputArray cameraMatrix, InputArray distCoeffs,
  860. OutputArray rvec, OutputArray tvec,
  861. bool useExtrinsicGuess = false, int iterationsCount = 100,
  862. float reprojectionError = 8.0, double confidence = 0.99,
  863. OutputArray inliers = noArray(), int flags = SOLVEPNP_ITERATIVE );
  864. /** @brief Finds an object pose from 3 3D-2D point correspondences.
  865. @param objectPoints Array of object points in the object coordinate space, 3x3 1-channel or
  866. 1x3/3x1 3-channel. vector\<Point3f\> can be also passed here.
  867. @param imagePoints Array of corresponding image points, 3x2 1-channel or 1x3/3x1 2-channel.
  868. vector\<Point2f\> can be also passed here.
  869. @param cameraMatrix Input camera intrinsic matrix \f$\cameramatrix{A}\f$ .
  870. @param distCoeffs Input vector of distortion coefficients
  871. \f$\distcoeffs\f$. If the vector is NULL/empty, the zero distortion coefficients are
  872. assumed.
  873. @param rvecs Output rotation vectors (see @ref Rodrigues ) that, together with tvecs, brings points from
  874. the model coordinate system to the camera coordinate system. A P3P problem has up to 4 solutions.
  875. @param tvecs Output translation vectors.
  876. @param flags Method for solving a P3P problem:
  877. - **SOLVEPNP_P3P** Method is based on the paper of X.S. Gao, X.-R. Hou, J. Tang, H.-F. Chang
  878. "Complete Solution Classification for the Perspective-Three-Point Problem" (@cite gao2003complete).
  879. - **SOLVEPNP_AP3P** Method is based on the paper of T. Ke and S. Roumeliotis.
  880. "An Efficient Algebraic Solution to the Perspective-Three-Point Problem" (@cite Ke17).
  881. The function estimates the object pose given 3 object points, their corresponding image
  882. projections, as well as the camera intrinsic matrix and the distortion coefficients.
  883. @note
  884. The solutions are sorted by reprojection errors (lowest to highest).
  885. */
  886. CV_EXPORTS_W int solveP3P( InputArray objectPoints, InputArray imagePoints,
  887. InputArray cameraMatrix, InputArray distCoeffs,
  888. OutputArrayOfArrays rvecs, OutputArrayOfArrays tvecs,
  889. int flags );
  890. /** @brief Refine a pose (the translation and the rotation that transform a 3D point expressed in the object coordinate frame
  891. to the camera coordinate frame) from a 3D-2D point correspondences and starting from an initial solution.
  892. @param objectPoints Array of object points in the object coordinate space, Nx3 1-channel or 1xN/Nx1 3-channel,
  893. where N is the number of points. vector\<Point3d\> can also be passed here.
  894. @param imagePoints Array of corresponding image points, Nx2 1-channel or 1xN/Nx1 2-channel,
  895. where N is the number of points. vector\<Point2d\> can also be passed here.
  896. @param cameraMatrix Input camera intrinsic matrix \f$\cameramatrix{A}\f$ .
  897. @param distCoeffs Input vector of distortion coefficients
  898. \f$\distcoeffs\f$. If the vector is NULL/empty, the zero distortion coefficients are
  899. assumed.
  900. @param rvec Input/Output rotation vector (see @ref Rodrigues ) that, together with tvec, brings points from
  901. the model coordinate system to the camera coordinate system. Input values are used as an initial solution.
  902. @param tvec Input/Output translation vector. Input values are used as an initial solution.
  903. @param criteria Criteria when to stop the Levenberg-Marquard iterative algorithm.
  904. The function refines the object pose given at least 3 object points, their corresponding image
  905. projections, an initial solution for the rotation and translation vector,
  906. as well as the camera intrinsic matrix and the distortion coefficients.
  907. The function minimizes the projection error with respect to the rotation and the translation vectors, according
  908. to a Levenberg-Marquardt iterative minimization @cite Madsen04 @cite Eade13 process.
  909. */
  910. CV_EXPORTS_W void solvePnPRefineLM( InputArray objectPoints, InputArray imagePoints,
  911. InputArray cameraMatrix, InputArray distCoeffs,
  912. InputOutputArray rvec, InputOutputArray tvec,
  913. TermCriteria criteria = TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 20, FLT_EPSILON));
  914. /** @brief Refine a pose (the translation and the rotation that transform a 3D point expressed in the object coordinate frame
  915. to the camera coordinate frame) from a 3D-2D point correspondences and starting from an initial solution.
  916. @param objectPoints Array of object points in the object coordinate space, Nx3 1-channel or 1xN/Nx1 3-channel,
  917. where N is the number of points. vector\<Point3d\> can also be passed here.
  918. @param imagePoints Array of corresponding image points, Nx2 1-channel or 1xN/Nx1 2-channel,
  919. where N is the number of points. vector\<Point2d\> can also be passed here.
  920. @param cameraMatrix Input camera intrinsic matrix \f$\cameramatrix{A}\f$ .
  921. @param distCoeffs Input vector of distortion coefficients
  922. \f$\distcoeffs\f$. If the vector is NULL/empty, the zero distortion coefficients are
  923. assumed.
  924. @param rvec Input/Output rotation vector (see @ref Rodrigues ) that, together with tvec, brings points from
  925. the model coordinate system to the camera coordinate system. Input values are used as an initial solution.
  926. @param tvec Input/Output translation vector. Input values are used as an initial solution.
  927. @param criteria Criteria when to stop the Levenberg-Marquard iterative algorithm.
  928. @param VVSlambda Gain for the virtual visual servoing control law, equivalent to the \f$\alpha\f$
  929. gain in the Damped Gauss-Newton formulation.
  930. The function refines the object pose given at least 3 object points, their corresponding image
  931. projections, an initial solution for the rotation and translation vector,
  932. as well as the camera intrinsic matrix and the distortion coefficients.
  933. The function minimizes the projection error with respect to the rotation and the translation vectors, using a
  934. virtual visual servoing (VVS) @cite Chaumette06 @cite Marchand16 scheme.
  935. */
  936. CV_EXPORTS_W void solvePnPRefineVVS( InputArray objectPoints, InputArray imagePoints,
  937. InputArray cameraMatrix, InputArray distCoeffs,
  938. InputOutputArray rvec, InputOutputArray tvec,
  939. TermCriteria criteria = TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 20, FLT_EPSILON),
  940. double VVSlambda = 1);
  941. /** @brief Finds an object pose from 3D-2D point correspondences.
  942. This function returns a list of all the possible solutions (a solution is a <rotation vector, translation vector>
  943. couple), depending on the number of input points and the chosen method:
  944. - P3P methods (@ref SOLVEPNP_P3P, @ref SOLVEPNP_AP3P): 3 or 4 input points. Number of returned solutions can be between 0 and 4 with 3 input points.
  945. - @ref SOLVEPNP_IPPE Input points must be >= 4 and object points must be coplanar. Returns 2 solutions.
  946. - @ref SOLVEPNP_IPPE_SQUARE Special case suitable for marker pose estimation.
  947. Number of input points must be 4 and 2 solutions are returned. Object points must be defined in the following order:
  948. - point 0: [-squareLength / 2, squareLength / 2, 0]
  949. - point 1: [ squareLength / 2, squareLength / 2, 0]
  950. - point 2: [ squareLength / 2, -squareLength / 2, 0]
  951. - point 3: [-squareLength / 2, -squareLength / 2, 0]
  952. - for all the other flags, number of input points must be >= 4 and object points can be in any configuration.
  953. Only 1 solution is returned.
  954. @param objectPoints Array of object points in the object coordinate space, Nx3 1-channel or
  955. 1xN/Nx1 3-channel, where N is the number of points. vector\<Point3d\> can be also passed here.
  956. @param imagePoints Array of corresponding image points, Nx2 1-channel or 1xN/Nx1 2-channel,
  957. where N is the number of points. vector\<Point2d\> can be also passed here.
  958. @param cameraMatrix Input camera intrinsic matrix \f$\cameramatrix{A}\f$ .
  959. @param distCoeffs Input vector of distortion coefficients
  960. \f$\distcoeffs\f$. If the vector is NULL/empty, the zero distortion coefficients are
  961. assumed.
  962. @param rvecs Vector of output rotation vectors (see @ref Rodrigues ) that, together with tvecs, brings points from
  963. the model coordinate system to the camera coordinate system.
  964. @param tvecs Vector of output translation vectors.
  965. @param useExtrinsicGuess Parameter used for #SOLVEPNP_ITERATIVE. If true (1), the function uses
  966. the provided rvec and tvec values as initial approximations of the rotation and translation
  967. vectors, respectively, and further optimizes them.
  968. @param flags Method for solving a PnP problem:
  969. - **SOLVEPNP_ITERATIVE** Iterative method is based on a Levenberg-Marquardt optimization. In
  970. this case the function finds such a pose that minimizes reprojection error, that is the sum
  971. of squared distances between the observed projections imagePoints and the projected (using
  972. projectPoints ) objectPoints .
  973. - **SOLVEPNP_P3P** Method is based on the paper of X.S. Gao, X.-R. Hou, J. Tang, H.-F. Chang
  974. "Complete Solution Classification for the Perspective-Three-Point Problem" (@cite gao2003complete).
  975. In this case the function requires exactly four object and image points.
  976. - **SOLVEPNP_AP3P** Method is based on the paper of T. Ke, S. Roumeliotis
  977. "An Efficient Algebraic Solution to the Perspective-Three-Point Problem" (@cite Ke17).
  978. In this case the function requires exactly four object and image points.
  979. - **SOLVEPNP_EPNP** Method has been introduced by F.Moreno-Noguer, V.Lepetit and P.Fua in the
  980. paper "EPnP: Efficient Perspective-n-Point Camera Pose Estimation" (@cite lepetit2009epnp).
  981. - **SOLVEPNP_DLS** **Broken implementation. Using this flag will fallback to EPnP.** \n
  982. Method is based on the paper of Joel A. Hesch and Stergios I. Roumeliotis.
  983. "A Direct Least-Squares (DLS) Method for PnP" (@cite hesch2011direct).
  984. - **SOLVEPNP_UPNP** **Broken implementation. Using this flag will fallback to EPnP.** \n
  985. Method is based on the paper of A.Penate-Sanchez, J.Andrade-Cetto,
  986. F.Moreno-Noguer. "Exhaustive Linearization for Robust Camera Pose and Focal Length
  987. Estimation" (@cite penate2013exhaustive). In this case the function also estimates the parameters \f$f_x\f$ and \f$f_y\f$
  988. assuming that both have the same value. Then the cameraMatrix is updated with the estimated
  989. focal length.
  990. - **SOLVEPNP_IPPE** Method is based on the paper of T. Collins and A. Bartoli.
  991. "Infinitesimal Plane-Based Pose Estimation" (@cite Collins14). This method requires coplanar object points.
  992. - **SOLVEPNP_IPPE_SQUARE** Method is based on the paper of Toby Collins and Adrien Bartoli.
  993. "Infinitesimal Plane-Based Pose Estimation" (@cite Collins14). This method is suitable for marker pose estimation.
  994. It requires 4 coplanar object points defined in the following order:
  995. - point 0: [-squareLength / 2, squareLength / 2, 0]
  996. - point 1: [ squareLength / 2, squareLength / 2, 0]
  997. - point 2: [ squareLength / 2, -squareLength / 2, 0]
  998. - point 3: [-squareLength / 2, -squareLength / 2, 0]
  999. @param rvec Rotation vector used to initialize an iterative PnP refinement algorithm, when flag is SOLVEPNP_ITERATIVE
  1000. and useExtrinsicGuess is set to true.
  1001. @param tvec Translation vector used to initialize an iterative PnP refinement algorithm, when flag is SOLVEPNP_ITERATIVE
  1002. and useExtrinsicGuess is set to true.
  1003. @param reprojectionError Optional vector of reprojection error, that is the RMS error
  1004. (\f$ \text{RMSE} = \sqrt{\frac{\sum_{i}^{N} \left ( \hat{y_i} - y_i \right )^2}{N}} \f$) between the input image points
  1005. and the 3D object points projected with the estimated pose.
  1006. The function estimates the object pose given a set of object points, their corresponding image
  1007. projections, as well as the camera intrinsic matrix and the distortion coefficients, see the figure below
  1008. (more precisely, the X-axis of the camera frame is pointing to the right, the Y-axis downward
  1009. and the Z-axis forward).
  1010. ![](pnp.jpg)
  1011. Points expressed in the world frame \f$ \bf{X}_w \f$ are projected into the image plane \f$ \left[ u, v \right] \f$
  1012. using the perspective projection model \f$ \Pi \f$ and the camera intrinsic parameters matrix \f$ \bf{A} \f$:
  1013. \f[
  1014. \begin{align*}
  1015. \begin{bmatrix}
  1016. u \\
  1017. v \\
  1018. 1
  1019. \end{bmatrix} &=
  1020. \bf{A} \hspace{0.1em} \Pi \hspace{0.2em} ^{c}\bf{T}_w
  1021. \begin{bmatrix}
  1022. X_{w} \\
  1023. Y_{w} \\
  1024. Z_{w} \\
  1025. 1
  1026. \end{bmatrix} \\
  1027. \begin{bmatrix}
  1028. u \\
  1029. v \\
  1030. 1
  1031. \end{bmatrix} &=
  1032. \begin{bmatrix}
  1033. f_x & 0 & c_x \\
  1034. 0 & f_y & c_y \\
  1035. 0 & 0 & 1
  1036. \end{bmatrix}
  1037. \begin{bmatrix}
  1038. 1 & 0 & 0 & 0 \\
  1039. 0 & 1 & 0 & 0 \\
  1040. 0 & 0 & 1 & 0
  1041. \end{bmatrix}
  1042. \begin{bmatrix}
  1043. r_{11} & r_{12} & r_{13} & t_x \\
  1044. r_{21} & r_{22} & r_{23} & t_y \\
  1045. r_{31} & r_{32} & r_{33} & t_z \\
  1046. 0 & 0 & 0 & 1
  1047. \end{bmatrix}
  1048. \begin{bmatrix}
  1049. X_{w} \\
  1050. Y_{w} \\
  1051. Z_{w} \\
  1052. 1
  1053. \end{bmatrix}
  1054. \end{align*}
  1055. \f]
  1056. The estimated pose is thus the rotation (`rvec`) and the translation (`tvec`) vectors that allow transforming
  1057. a 3D point expressed in the world frame into the camera frame:
  1058. \f[
  1059. \begin{align*}
  1060. \begin{bmatrix}
  1061. X_c \\
  1062. Y_c \\
  1063. Z_c \\
  1064. 1
  1065. \end{bmatrix} &=
  1066. \hspace{0.2em} ^{c}\bf{T}_w
  1067. \begin{bmatrix}
  1068. X_{w} \\
  1069. Y_{w} \\
  1070. Z_{w} \\
  1071. 1
  1072. \end{bmatrix} \\
  1073. \begin{bmatrix}
  1074. X_c \\
  1075. Y_c \\
  1076. Z_c \\
  1077. 1
  1078. \end{bmatrix} &=
  1079. \begin{bmatrix}
  1080. r_{11} & r_{12} & r_{13} & t_x \\
  1081. r_{21} & r_{22} & r_{23} & t_y \\
  1082. r_{31} & r_{32} & r_{33} & t_z \\
  1083. 0 & 0 & 0 & 1
  1084. \end{bmatrix}
  1085. \begin{bmatrix}
  1086. X_{w} \\
  1087. Y_{w} \\
  1088. Z_{w} \\
  1089. 1
  1090. \end{bmatrix}
  1091. \end{align*}
  1092. \f]
  1093. @note
  1094. - An example of how to use solvePnP for planar augmented reality can be found at
  1095. opencv_source_code/samples/python/plane_ar.py
  1096. - If you are using Python:
  1097. - Numpy array slices won't work as input because solvePnP requires contiguous
  1098. arrays (enforced by the assertion using cv::Mat::checkVector() around line 55 of
  1099. modules/calib3d/src/solvepnp.cpp version 2.4.9)
  1100. - The P3P algorithm requires image points to be in an array of shape (N,1,2) due
  1101. to its calling of cv::undistortPoints (around line 75 of modules/calib3d/src/solvepnp.cpp version 2.4.9)
  1102. which requires 2-channel information.
  1103. - Thus, given some data D = np.array(...) where D.shape = (N,M), in order to use a subset of
  1104. it as, e.g., imagePoints, one must effectively copy it into a new array: imagePoints =
  1105. np.ascontiguousarray(D[:,:2]).reshape((N,1,2))
  1106. - The methods **SOLVEPNP_DLS** and **SOLVEPNP_UPNP** cannot be used as the current implementations are
  1107. unstable and sometimes give completely wrong results. If you pass one of these two
  1108. flags, **SOLVEPNP_EPNP** method will be used instead.
  1109. - The minimum number of points is 4 in the general case. In the case of **SOLVEPNP_P3P** and **SOLVEPNP_AP3P**
  1110. methods, it is required to use exactly 4 points (the first 3 points are used to estimate all the solutions
  1111. of the P3P problem, the last one is used to retain the best solution that minimizes the reprojection error).
  1112. - With **SOLVEPNP_ITERATIVE** method and `useExtrinsicGuess=true`, the minimum number of points is 3 (3 points
  1113. are sufficient to compute a pose but there are up to 4 solutions). The initial solution should be close to the
  1114. global solution to converge.
  1115. - With **SOLVEPNP_IPPE** input points must be >= 4 and object points must be coplanar.
  1116. - With **SOLVEPNP_IPPE_SQUARE** this is a special case suitable for marker pose estimation.
  1117. Number of input points must be 4. Object points must be defined in the following order:
  1118. - point 0: [-squareLength / 2, squareLength / 2, 0]
  1119. - point 1: [ squareLength / 2, squareLength / 2, 0]
  1120. - point 2: [ squareLength / 2, -squareLength / 2, 0]
  1121. - point 3: [-squareLength / 2, -squareLength / 2, 0]
  1122. */
  1123. CV_EXPORTS_W int solvePnPGeneric( InputArray objectPoints, InputArray imagePoints,
  1124. InputArray cameraMatrix, InputArray distCoeffs,
  1125. OutputArrayOfArrays rvecs, OutputArrayOfArrays tvecs,
  1126. bool useExtrinsicGuess = false, SolvePnPMethod flags = SOLVEPNP_ITERATIVE,
  1127. InputArray rvec = noArray(), InputArray tvec = noArray(),
  1128. OutputArray reprojectionError = noArray() );
  1129. /** @brief Finds an initial camera intrinsic matrix from 3D-2D point correspondences.
  1130. @param objectPoints Vector of vectors of the calibration pattern points in the calibration pattern
  1131. coordinate space. In the old interface all the per-view vectors are concatenated. See
  1132. calibrateCamera for details.
  1133. @param imagePoints Vector of vectors of the projections of the calibration pattern points. In the
  1134. old interface all the per-view vectors are concatenated.
  1135. @param imageSize Image size in pixels used to initialize the principal point.
  1136. @param aspectRatio If it is zero or negative, both \f$f_x\f$ and \f$f_y\f$ are estimated independently.
  1137. Otherwise, \f$f_x = f_y * \texttt{aspectRatio}\f$ .
  1138. The function estimates and returns an initial camera intrinsic matrix for the camera calibration process.
  1139. Currently, the function only supports planar calibration patterns, which are patterns where each
  1140. object point has z-coordinate =0.
  1141. */
  1142. CV_EXPORTS_W Mat initCameraMatrix2D( InputArrayOfArrays objectPoints,
  1143. InputArrayOfArrays imagePoints,
  1144. Size imageSize, double aspectRatio = 1.0 );
  1145. /** @brief Finds the positions of internal corners of the chessboard.
  1146. @param image Source chessboard view. It must be an 8-bit grayscale or color image.
  1147. @param patternSize Number of inner corners per a chessboard row and column
  1148. ( patternSize = cvSize(points_per_row,points_per_colum) = cvSize(columns,rows) ).
  1149. @param corners Output array of detected corners.
  1150. @param flags Various operation flags that can be zero or a combination of the following values:
  1151. - **CALIB_CB_ADAPTIVE_THRESH** Use adaptive thresholding to convert the image to black
  1152. and white, rather than a fixed threshold level (computed from the average image brightness).
  1153. - **CALIB_CB_NORMALIZE_IMAGE** Normalize the image gamma with equalizeHist before
  1154. applying fixed or adaptive thresholding.
  1155. - **CALIB_CB_FILTER_QUADS** Use additional criteria (like contour area, perimeter,
  1156. square-like shape) to filter out false quads extracted at the contour retrieval stage.
  1157. - **CALIB_CB_FAST_CHECK** Run a fast check on the image that looks for chessboard corners,
  1158. and shortcut the call if none is found. This can drastically speed up the call in the
  1159. degenerate condition when no chessboard is observed.
  1160. The function attempts to determine whether the input image is a view of the chessboard pattern and
  1161. locate the internal chessboard corners. The function returns a non-zero value if all of the corners
  1162. are found and they are placed in a certain order (row by row, left to right in every row).
  1163. Otherwise, if the function fails to find all the corners or reorder them, it returns 0. For example,
  1164. a regular chessboard has 8 x 8 squares and 7 x 7 internal corners, that is, points where the black
  1165. squares touch each other. The detected coordinates are approximate, and to determine their positions
  1166. more accurately, the function calls cornerSubPix. You also may use the function cornerSubPix with
  1167. different parameters if returned coordinates are not accurate enough.
  1168. Sample usage of detecting and drawing chessboard corners: :
  1169. @code
  1170. Size patternsize(8,6); //interior number of corners
  1171. Mat gray = ....; //source image
  1172. vector<Point2f> corners; //this will be filled by the detected corners
  1173. //CALIB_CB_FAST_CHECK saves a lot of time on images
  1174. //that do not contain any chessboard corners
  1175. bool patternfound = findChessboardCorners(gray, patternsize, corners,
  1176. CALIB_CB_ADAPTIVE_THRESH + CALIB_CB_NORMALIZE_IMAGE
  1177. + CALIB_CB_FAST_CHECK);
  1178. if(patternfound)
  1179. cornerSubPix(gray, corners, Size(11, 11), Size(-1, -1),
  1180. TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 30, 0.1));
  1181. drawChessboardCorners(img, patternsize, Mat(corners), patternfound);
  1182. @endcode
  1183. @note The function requires white space (like a square-thick border, the wider the better) around
  1184. the board to make the detection more robust in various environments. Otherwise, if there is no
  1185. border and the background is dark, the outer black squares cannot be segmented properly and so the
  1186. square grouping and ordering algorithm fails.
  1187. */
  1188. CV_EXPORTS_W bool findChessboardCorners( InputArray image, Size patternSize, OutputArray corners,
  1189. int flags = CALIB_CB_ADAPTIVE_THRESH + CALIB_CB_NORMALIZE_IMAGE );
  1190. //! finds subpixel-accurate positions of the chessboard corners
  1191. CV_EXPORTS_W bool find4QuadCornerSubpix( InputArray img, InputOutputArray corners, Size region_size );
  1192. /** @brief Renders the detected chessboard corners.
  1193. @param image Destination image. It must be an 8-bit color image.
  1194. @param patternSize Number of inner corners per a chessboard row and column
  1195. (patternSize = cv::Size(points_per_row,points_per_column)).
  1196. @param corners Array of detected corners, the output of findChessboardCorners.
  1197. @param patternWasFound Parameter indicating whether the complete board was found or not. The
  1198. return value of findChessboardCorners should be passed here.
  1199. The function draws individual chessboard corners detected either as red circles if the board was not
  1200. found, or as colored corners connected with lines if the board was found.
  1201. */
  1202. CV_EXPORTS_W void drawChessboardCorners( InputOutputArray image, Size patternSize,
  1203. InputArray corners, bool patternWasFound );
  1204. /** @brief Draw axes of the world/object coordinate system from pose estimation. @sa solvePnP
  1205. @param image Input/output image. It must have 1 or 3 channels. The number of channels is not altered.
  1206. @param cameraMatrix Input 3x3 floating-point matrix of camera intrinsic parameters.
  1207. \f$\cameramatrix{A}\f$
  1208. @param distCoeffs Input vector of distortion coefficients
  1209. \f$\distcoeffs\f$. If the vector is empty, the zero distortion coefficients are assumed.
  1210. @param rvec Rotation vector (see @ref Rodrigues ) that, together with tvec, brings points from
  1211. the model coordinate system to the camera coordinate system.
  1212. @param tvec Translation vector.
  1213. @param length Length of the painted axes in the same unit than tvec (usually in meters).
  1214. @param thickness Line thickness of the painted axes.
  1215. This function draws the axes of the world/object coordinate system w.r.t. to the camera frame.
  1216. OX is drawn in red, OY in green and OZ in blue.
  1217. */
  1218. CV_EXPORTS_W void drawFrameAxes(InputOutputArray image, InputArray cameraMatrix, InputArray distCoeffs,
  1219. InputArray rvec, InputArray tvec, float length, int thickness=3);
  1220. struct CV_EXPORTS_W_SIMPLE CirclesGridFinderParameters
  1221. {
  1222. CV_WRAP CirclesGridFinderParameters();
  1223. CV_PROP_RW cv::Size2f densityNeighborhoodSize;
  1224. CV_PROP_RW float minDensity;
  1225. CV_PROP_RW int kmeansAttempts;
  1226. CV_PROP_RW int minDistanceToAddKeypoint;
  1227. CV_PROP_RW int keypointScale;
  1228. CV_PROP_RW float minGraphConfidence;
  1229. CV_PROP_RW float vertexGain;
  1230. CV_PROP_RW float vertexPenalty;
  1231. CV_PROP_RW float existingVertexGain;
  1232. CV_PROP_RW float edgeGain;
  1233. CV_PROP_RW float edgePenalty;
  1234. CV_PROP_RW float convexHullFactor;
  1235. CV_PROP_RW float minRNGEdgeSwitchDist;
  1236. enum GridType
  1237. {
  1238. SYMMETRIC_GRID, ASYMMETRIC_GRID
  1239. };
  1240. GridType gridType;
  1241. };
  1242. struct CV_EXPORTS_W_SIMPLE CirclesGridFinderParameters2 : public CirclesGridFinderParameters
  1243. {
  1244. CV_WRAP CirclesGridFinderParameters2();
  1245. CV_PROP_RW float squareSize; //!< Distance between two adjacent points. Used by CALIB_CB_CLUSTERING.
  1246. CV_PROP_RW float maxRectifiedDistance; //!< Max deviation from prediction. Used by CALIB_CB_CLUSTERING.
  1247. };
  1248. /** @brief Finds centers in the grid of circles.
  1249. @param image grid view of input circles; it must be an 8-bit grayscale or color image.
  1250. @param patternSize number of circles per row and column
  1251. ( patternSize = Size(points_per_row, points_per_colum) ).
  1252. @param centers output array of detected centers.
  1253. @param flags various operation flags that can be one of the following values:
  1254. - **CALIB_CB_SYMMETRIC_GRID** uses symmetric pattern of circles.
  1255. - **CALIB_CB_ASYMMETRIC_GRID** uses asymmetric pattern of circles.
  1256. - **CALIB_CB_CLUSTERING** uses a special algorithm for grid detection. It is more robust to
  1257. perspective distortions but much more sensitive to background clutter.
  1258. @param blobDetector feature detector that finds blobs like dark circles on light background.
  1259. @param parameters struct for finding circles in a grid pattern.
  1260. The function attempts to determine whether the input image contains a grid of circles. If it is, the
  1261. function locates centers of the circles. The function returns a non-zero value if all of the centers
  1262. have been found and they have been placed in a certain order (row by row, left to right in every
  1263. row). Otherwise, if the function fails to find all the corners or reorder them, it returns 0.
  1264. Sample usage of detecting and drawing the centers of circles: :
  1265. @code
  1266. Size patternsize(7,7); //number of centers
  1267. Mat gray = ....; //source image
  1268. vector<Point2f> centers; //this will be filled by the detected centers
  1269. bool patternfound = findCirclesGrid(gray, patternsize, centers);
  1270. drawChessboardCorners(img, patternsize, Mat(centers), patternfound);
  1271. @endcode
  1272. @note The function requires white space (like a square-thick border, the wider the better) around
  1273. the board to make the detection more robust in various environments.
  1274. */
  1275. CV_EXPORTS_W bool findCirclesGrid( InputArray image, Size patternSize,
  1276. OutputArray centers, int flags,
  1277. const Ptr<FeatureDetector> &blobDetector,
  1278. CirclesGridFinderParameters parameters);
  1279. /** @overload */
  1280. CV_EXPORTS_W bool findCirclesGrid2( InputArray image, Size patternSize,
  1281. OutputArray centers, int flags,
  1282. const Ptr<FeatureDetector> &blobDetector,
  1283. CirclesGridFinderParameters2 parameters);
  1284. /** @overload */
  1285. CV_EXPORTS_W bool findCirclesGrid( InputArray image, Size patternSize,
  1286. OutputArray centers, int flags = CALIB_CB_SYMMETRIC_GRID,
  1287. const Ptr<FeatureDetector> &blobDetector = SimpleBlobDetector::create());
  1288. /** @brief Finds the camera intrinsic and extrinsic parameters from several views of a calibration
  1289. pattern.
  1290. @param objectPoints In the new interface it is a vector of vectors of calibration pattern points in
  1291. the calibration pattern coordinate space (e.g. std::vector<std::vector<cv::Vec3f>>). The outer
  1292. vector contains as many elements as the number of pattern views. If the same calibration pattern
  1293. is shown in each view and it is fully visible, all the vectors will be the same. Although, it is
  1294. possible to use partially occluded patterns or even different patterns in different views. Then,
  1295. the vectors will be different. Although the points are 3D, they all lie in the calibration pattern's
  1296. XY coordinate plane (thus 0 in the Z-coordinate), if the used calibration pattern is a planar rig.
  1297. In the old interface all the vectors of object points from different views are concatenated
  1298. together.
  1299. @param imagePoints In the new interface it is a vector of vectors of the projections of calibration
  1300. pattern points (e.g. std::vector<std::vector<cv::Vec2f>>). imagePoints.size() and
  1301. objectPoints.size(), and imagePoints[i].size() and objectPoints[i].size() for each i, must be equal,
  1302. respectively. In the old interface all the vectors of object points from different views are
  1303. concatenated together.
  1304. @param imageSize Size of the image used only to initialize the camera intrinsic matrix.
  1305. @param cameraMatrix Input/output 3x3 floating-point camera intrinsic matrix
  1306. \f$\cameramatrix{A}\f$ . If CV\_CALIB\_USE\_INTRINSIC\_GUESS
  1307. and/or CALIB_FIX_ASPECT_RATIO are specified, some or all of fx, fy, cx, cy must be
  1308. initialized before calling the function.
  1309. @param distCoeffs Input/output vector of distortion coefficients
  1310. \f$\distcoeffs\f$.
  1311. @param rvecs Output vector of rotation vectors (@ref Rodrigues ) estimated for each pattern view
  1312. (e.g. std::vector<cv::Mat>>). That is, each i-th rotation vector together with the corresponding
  1313. i-th translation vector (see the next output parameter description) brings the calibration pattern
  1314. from the object coordinate space (in which object points are specified) to the camera coordinate
  1315. space. In more technical terms, the tuple of the i-th rotation and translation vector performs
  1316. a change of basis from object coordinate space to camera coordinate space. Due to its duality, this
  1317. tuple is equivalent to the position of the calibration pattern with respect to the camera coordinate
  1318. space.
  1319. @param tvecs Output vector of translation vectors estimated for each pattern view, see parameter
  1320. describtion above.
  1321. @param stdDeviationsIntrinsics Output vector of standard deviations estimated for intrinsic
  1322. parameters. Order of deviations values:
  1323. \f$(f_x, f_y, c_x, c_y, k_1, k_2, p_1, p_2, k_3, k_4, k_5, k_6 , s_1, s_2, s_3,
  1324. s_4, \tau_x, \tau_y)\f$ If one of parameters is not estimated, it's deviation is equals to zero.
  1325. @param stdDeviationsExtrinsics Output vector of standard deviations estimated for extrinsic
  1326. parameters. Order of deviations values: \f$(R_0, T_0, \dotsc , R_{M - 1}, T_{M - 1})\f$ where M is
  1327. the number of pattern views. \f$R_i, T_i\f$ are concatenated 1x3 vectors.
  1328. @param perViewErrors Output vector of the RMS re-projection error estimated for each pattern view.
  1329. @param flags Different flags that may be zero or a combination of the following values:
  1330. - **CALIB_USE_INTRINSIC_GUESS** cameraMatrix contains valid initial values of
  1331. fx, fy, cx, cy that are optimized further. Otherwise, (cx, cy) is initially set to the image
  1332. center ( imageSize is used), and focal distances are computed in a least-squares fashion.
  1333. Note, that if intrinsic parameters are known, there is no need to use this function just to
  1334. estimate extrinsic parameters. Use solvePnP instead.
  1335. - **CALIB_FIX_PRINCIPAL_POINT** The principal point is not changed during the global
  1336. optimization. It stays at the center or at a different location specified when
  1337. CALIB_USE_INTRINSIC_GUESS is set too.
  1338. - **CALIB_FIX_ASPECT_RATIO** The functions consider only fy as a free parameter. The
  1339. ratio fx/fy stays the same as in the input cameraMatrix . When
  1340. CALIB_USE_INTRINSIC_GUESS is not set, the actual input values of fx and fy are
  1341. ignored, only their ratio is computed and used further.
  1342. - **CALIB_ZERO_TANGENT_DIST** Tangential distortion coefficients \f$(p_1, p_2)\f$ are set
  1343. to zeros and stay zero.
  1344. - **CALIB_FIX_K1,...,CALIB_FIX_K6** The corresponding radial distortion
  1345. coefficient is not changed during the optimization. If CALIB_USE_INTRINSIC_GUESS is
  1346. set, the coefficient from the supplied distCoeffs matrix is used. Otherwise, it is set to 0.
  1347. - **CALIB_RATIONAL_MODEL** Coefficients k4, k5, and k6 are enabled. To provide the
  1348. backward compatibility, this extra flag should be explicitly specified to make the
  1349. calibration function use the rational model and return 8 coefficients. If the flag is not
  1350. set, the function computes and returns only 5 distortion coefficients.
  1351. - **CALIB_THIN_PRISM_MODEL** Coefficients s1, s2, s3 and s4 are enabled. To provide the
  1352. backward compatibility, this extra flag should be explicitly specified to make the
  1353. calibration function use the thin prism model and return 12 coefficients. If the flag is not
  1354. set, the function computes and returns only 5 distortion coefficients.
  1355. - **CALIB_FIX_S1_S2_S3_S4** The thin prism distortion coefficients are not changed during
  1356. the optimization. If CALIB_USE_INTRINSIC_GUESS is set, the coefficient from the
  1357. supplied distCoeffs matrix is used. Otherwise, it is set to 0.
  1358. - **CALIB_TILTED_MODEL** Coefficients tauX and tauY are enabled. To provide the
  1359. backward compatibility, this extra flag should be explicitly specified to make the
  1360. calibration function use the tilted sensor model and return 14 coefficients. If the flag is not
  1361. set, the function computes and returns only 5 distortion coefficients.
  1362. - **CALIB_FIX_TAUX_TAUY** The coefficients of the tilted sensor model are not changed during
  1363. the optimization. If CALIB_USE_INTRINSIC_GUESS is set, the coefficient from the
  1364. supplied distCoeffs matrix is used. Otherwise, it is set to 0.
  1365. @param criteria Termination criteria for the iterative optimization algorithm.
  1366. @return the overall RMS re-projection error.
  1367. The function estimates the intrinsic camera parameters and extrinsic parameters for each of the
  1368. views. The algorithm is based on @cite Zhang2000 and @cite BouguetMCT . The coordinates of 3D object
  1369. points and their corresponding 2D projections in each view must be specified. That may be achieved
  1370. by using an object with known geometry and easily detectable feature points. Such an object is
  1371. called a calibration rig or calibration pattern, and OpenCV has built-in support for a chessboard as
  1372. a calibration rig (see @ref findChessboardCorners). Currently, initialization of intrinsic
  1373. parameters (when CALIB_USE_INTRINSIC_GUESS is not set) is only implemented for planar calibration
  1374. patterns (where Z-coordinates of the object points must be all zeros). 3D calibration rigs can also
  1375. be used as long as initial cameraMatrix is provided.
  1376. The algorithm performs the following steps:
  1377. - Compute the initial intrinsic parameters (the option only available for planar calibration
  1378. patterns) or read them from the input parameters. The distortion coefficients are all set to
  1379. zeros initially unless some of CALIB_FIX_K? are specified.
  1380. - Estimate the initial camera pose as if the intrinsic parameters have been already known. This is
  1381. done using solvePnP .
  1382. - Run the global Levenberg-Marquardt optimization algorithm to minimize the reprojection error,
  1383. that is, the total sum of squared distances between the observed feature points imagePoints and
  1384. the projected (using the current estimates for camera parameters and the poses) object points
  1385. objectPoints. See projectPoints for details.
  1386. @note
  1387. If you use a non-square (i.e. non-N-by-N) grid and @ref findChessboardCorners for calibration,
  1388. and @ref calibrateCamera returns bad values (zero distortion coefficients, \f$c_x\f$ and
  1389. \f$c_y\f$ very far from the image center, and/or large differences between \f$f_x\f$ and
  1390. \f$f_y\f$ (ratios of 10:1 or more)), then you are probably using patternSize=cvSize(rows,cols)
  1391. instead of using patternSize=cvSize(cols,rows) in @ref findChessboardCorners.
  1392. @sa
  1393. findChessboardCorners, solvePnP, initCameraMatrix2D, stereoCalibrate, undistort
  1394. */
  1395. CV_EXPORTS_AS(calibrateCameraExtended) double calibrateCamera( InputArrayOfArrays objectPoints,
  1396. InputArrayOfArrays imagePoints, Size imageSize,
  1397. InputOutputArray cameraMatrix, InputOutputArray distCoeffs,
  1398. OutputArrayOfArrays rvecs, OutputArrayOfArrays tvecs,
  1399. OutputArray stdDeviationsIntrinsics,
  1400. OutputArray stdDeviationsExtrinsics,
  1401. OutputArray perViewErrors,
  1402. int flags = 0, TermCriteria criteria = TermCriteria(
  1403. TermCriteria::COUNT + TermCriteria::EPS, 30, DBL_EPSILON) );
  1404. /** @overload double calibrateCamera( InputArrayOfArrays objectPoints,
  1405. InputArrayOfArrays imagePoints, Size imageSize,
  1406. InputOutputArray cameraMatrix, InputOutputArray distCoeffs,
  1407. OutputArrayOfArrays rvecs, OutputArrayOfArrays tvecs,
  1408. OutputArray stdDeviations, OutputArray perViewErrors,
  1409. int flags = 0, TermCriteria criteria = TermCriteria(
  1410. TermCriteria::COUNT + TermCriteria::EPS, 30, DBL_EPSILON) )
  1411. */
  1412. CV_EXPORTS_W double calibrateCamera( InputArrayOfArrays objectPoints,
  1413. InputArrayOfArrays imagePoints, Size imageSize,
  1414. InputOutputArray cameraMatrix, InputOutputArray distCoeffs,
  1415. OutputArrayOfArrays rvecs, OutputArrayOfArrays tvecs,
  1416. int flags = 0, TermCriteria criteria = TermCriteria(
  1417. TermCriteria::COUNT + TermCriteria::EPS, 30, DBL_EPSILON) );
  1418. /** @brief Computes useful camera characteristics from the camera intrinsic matrix.
  1419. @param cameraMatrix Input camera intrinsic matrix that can be estimated by calibrateCamera or
  1420. stereoCalibrate .
  1421. @param imageSize Input image size in pixels.
  1422. @param apertureWidth Physical width in mm of the sensor.
  1423. @param apertureHeight Physical height in mm of the sensor.
  1424. @param fovx Output field of view in degrees along the horizontal sensor axis.
  1425. @param fovy Output field of view in degrees along the vertical sensor axis.
  1426. @param focalLength Focal length of the lens in mm.
  1427. @param principalPoint Principal point in mm.
  1428. @param aspectRatio \f$f_y/f_x\f$
  1429. The function computes various useful camera characteristics from the previously estimated camera
  1430. matrix.
  1431. @note
  1432. Do keep in mind that the unity measure 'mm' stands for whatever unit of measure one chooses for
  1433. the chessboard pitch (it can thus be any value).
  1434. */
  1435. CV_EXPORTS_W void calibrationMatrixValues( InputArray cameraMatrix, Size imageSize,
  1436. double apertureWidth, double apertureHeight,
  1437. CV_OUT double& fovx, CV_OUT double& fovy,
  1438. CV_OUT double& focalLength, CV_OUT Point2d& principalPoint,
  1439. CV_OUT double& aspectRatio );
  1440. /** @brief Calibrates a stereo camera set up. This function finds the intrinsic parameters
  1441. for each of the two cameras and the extrinsic parameters between the two cameras.
  1442. @param objectPoints Vector of vectors of the calibration pattern points. The same structure as
  1443. in @ref calibrateCamera. For each pattern view, both cameras need to see the same object
  1444. points. Therefore, objectPoints.size(), imagePoints1.size(), and imagePoints2.size() need to be
  1445. equal as well as objectPoints[i].size(), imagePoints1[i].size(), and imagePoints2[i].size() need to
  1446. be equal for each i.
  1447. @param imagePoints1 Vector of vectors of the projections of the calibration pattern points,
  1448. observed by the first camera. The same structure as in @ref calibrateCamera.
  1449. @param imagePoints2 Vector of vectors of the projections of the calibration pattern points,
  1450. observed by the second camera. The same structure as in @ref calibrateCamera.
  1451. @param cameraMatrix1 Input/output camera intrinsic matrix for the first camera, the same as in
  1452. @ref calibrateCamera. Furthermore, for the stereo case, additional flags may be used, see below.
  1453. @param distCoeffs1 Input/output vector of distortion coefficients, the same as in
  1454. @ref calibrateCamera.
  1455. @param cameraMatrix2 Input/output second camera intrinsic matrix for the second camera. See description for
  1456. cameraMatrix1.
  1457. @param distCoeffs2 Input/output lens distortion coefficients for the second camera. See
  1458. description for distCoeffs1.
  1459. @param imageSize Size of the image used only to initialize the camera intrinsic matrices.
  1460. @param R Output rotation matrix. Together with the translation vector T, this matrix brings
  1461. points given in the first camera's coordinate system to points in the second camera's
  1462. coordinate system. In more technical terms, the tuple of R and T performs a change of basis
  1463. from the first camera's coordinate system to the second camera's coordinate system. Due to its
  1464. duality, this tuple is equivalent to the position of the first camera with respect to the
  1465. second camera coordinate system.
  1466. @param T Output translation vector, see description above.
  1467. @param E Output essential matrix.
  1468. @param F Output fundamental matrix.
  1469. @param perViewErrors Output vector of the RMS re-projection error estimated for each pattern view.
  1470. @param flags Different flags that may be zero or a combination of the following values:
  1471. - **CALIB_FIX_INTRINSIC** Fix cameraMatrix? and distCoeffs? so that only R, T, E, and F
  1472. matrices are estimated.
  1473. - **CALIB_USE_INTRINSIC_GUESS** Optimize some or all of the intrinsic parameters
  1474. according to the specified flags. Initial values are provided by the user.
  1475. - **CALIB_USE_EXTRINSIC_GUESS** R and T contain valid initial values that are optimized further.
  1476. Otherwise R and T are initialized to the median value of the pattern views (each dimension separately).
  1477. - **CALIB_FIX_PRINCIPAL_POINT** Fix the principal points during the optimization.
  1478. - **CALIB_FIX_FOCAL_LENGTH** Fix \f$f^{(j)}_x\f$ and \f$f^{(j)}_y\f$ .
  1479. - **CALIB_FIX_ASPECT_RATIO** Optimize \f$f^{(j)}_y\f$ . Fix the ratio \f$f^{(j)}_x/f^{(j)}_y\f$
  1480. .
  1481. - **CALIB_SAME_FOCAL_LENGTH** Enforce \f$f^{(0)}_x=f^{(1)}_x\f$ and \f$f^{(0)}_y=f^{(1)}_y\f$ .
  1482. - **CALIB_ZERO_TANGENT_DIST** Set tangential distortion coefficients for each camera to
  1483. zeros and fix there.
  1484. - **CALIB_FIX_K1,...,CALIB_FIX_K6** Do not change the corresponding radial
  1485. distortion coefficient during the optimization. If CALIB_USE_INTRINSIC_GUESS is set,
  1486. the coefficient from the supplied distCoeffs matrix is used. Otherwise, it is set to 0.
  1487. - **CALIB_RATIONAL_MODEL** Enable coefficients k4, k5, and k6. To provide the backward
  1488. compatibility, this extra flag should be explicitly specified to make the calibration
  1489. function use the rational model and return 8 coefficients. If the flag is not set, the
  1490. function computes and returns only 5 distortion coefficients.
  1491. - **CALIB_THIN_PRISM_MODEL** Coefficients s1, s2, s3 and s4 are enabled. To provide the
  1492. backward compatibility, this extra flag should be explicitly specified to make the
  1493. calibration function use the thin prism model and return 12 coefficients. If the flag is not
  1494. set, the function computes and returns only 5 distortion coefficients.
  1495. - **CALIB_FIX_S1_S2_S3_S4** The thin prism distortion coefficients are not changed during
  1496. the optimization. If CALIB_USE_INTRINSIC_GUESS is set, the coefficient from the
  1497. supplied distCoeffs matrix is used. Otherwise, it is set to 0.
  1498. - **CALIB_TILTED_MODEL** Coefficients tauX and tauY are enabled. To provide the
  1499. backward compatibility, this extra flag should be explicitly specified to make the
  1500. calibration function use the tilted sensor model and return 14 coefficients. If the flag is not
  1501. set, the function computes and returns only 5 distortion coefficients.
  1502. - **CALIB_FIX_TAUX_TAUY** The coefficients of the tilted sensor model are not changed during
  1503. the optimization. If CALIB_USE_INTRINSIC_GUESS is set, the coefficient from the
  1504. supplied distCoeffs matrix is used. Otherwise, it is set to 0.
  1505. @param criteria Termination criteria for the iterative optimization algorithm.
  1506. The function estimates the transformation between two cameras making a stereo pair. If one computes
  1507. the poses of an object relative to the first camera and to the second camera,
  1508. ( \f$R_1\f$,\f$T_1\f$ ) and (\f$R_2\f$,\f$T_2\f$), respectively, for a stereo camera where the
  1509. relative position and orientation between the two cameras are fixed, then those poses definitely
  1510. relate to each other. This means, if the relative position and orientation (\f$R\f$,\f$T\f$) of the
  1511. two cameras is known, it is possible to compute (\f$R_2\f$,\f$T_2\f$) when (\f$R_1\f$,\f$T_1\f$) is
  1512. given. This is what the described function does. It computes (\f$R\f$,\f$T\f$) such that:
  1513. \f[R_2=R R_1\f]
  1514. \f[T_2=R T_1 + T.\f]
  1515. Therefore, one can compute the coordinate representation of a 3D point for the second camera's
  1516. coordinate system when given the point's coordinate representation in the first camera's coordinate
  1517. system:
  1518. \f[\begin{bmatrix}
  1519. X_2 \\
  1520. Y_2 \\
  1521. Z_2 \\
  1522. 1
  1523. \end{bmatrix} = \begin{bmatrix}
  1524. R & T \\
  1525. 0 & 1
  1526. \end{bmatrix} \begin{bmatrix}
  1527. X_1 \\
  1528. Y_1 \\
  1529. Z_1 \\
  1530. 1
  1531. \end{bmatrix}.\f]
  1532. Optionally, it computes the essential matrix E:
  1533. \f[E= \vecthreethree{0}{-T_2}{T_1}{T_2}{0}{-T_0}{-T_1}{T_0}{0} R\f]
  1534. where \f$T_i\f$ are components of the translation vector \f$T\f$ : \f$T=[T_0, T_1, T_2]^T\f$ .
  1535. And the function can also compute the fundamental matrix F:
  1536. \f[F = cameraMatrix2^{-T}\cdot E \cdot cameraMatrix1^{-1}\f]
  1537. Besides the stereo-related information, the function can also perform a full calibration of each of
  1538. the two cameras. However, due to the high dimensionality of the parameter space and noise in the
  1539. input data, the function can diverge from the correct solution. If the intrinsic parameters can be
  1540. estimated with high accuracy for each of the cameras individually (for example, using
  1541. calibrateCamera ), you are recommended to do so and then pass CALIB_FIX_INTRINSIC flag to the
  1542. function along with the computed intrinsic parameters. Otherwise, if all the parameters are
  1543. estimated at once, it makes sense to restrict some parameters, for example, pass
  1544. CALIB_SAME_FOCAL_LENGTH and CALIB_ZERO_TANGENT_DIST flags, which is usually a
  1545. reasonable assumption.
  1546. Similarly to calibrateCamera, the function minimizes the total re-projection error for all the
  1547. points in all the available views from both cameras. The function returns the final value of the
  1548. re-projection error.
  1549. */
  1550. CV_EXPORTS_AS(stereoCalibrateExtended) double stereoCalibrate( InputArrayOfArrays objectPoints,
  1551. InputArrayOfArrays imagePoints1, InputArrayOfArrays imagePoints2,
  1552. InputOutputArray cameraMatrix1, InputOutputArray distCoeffs1,
  1553. InputOutputArray cameraMatrix2, InputOutputArray distCoeffs2,
  1554. Size imageSize, InputOutputArray R,InputOutputArray T, OutputArray E, OutputArray F,
  1555. OutputArray perViewErrors, int flags = CALIB_FIX_INTRINSIC,
  1556. TermCriteria criteria = TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, 1e-6) );
  1557. /// @overload
  1558. CV_EXPORTS_W double stereoCalibrate( InputArrayOfArrays objectPoints,
  1559. InputArrayOfArrays imagePoints1, InputArrayOfArrays imagePoints2,
  1560. InputOutputArray cameraMatrix1, InputOutputArray distCoeffs1,
  1561. InputOutputArray cameraMatrix2, InputOutputArray distCoeffs2,
  1562. Size imageSize, OutputArray R,OutputArray T, OutputArray E, OutputArray F,
  1563. int flags = CALIB_FIX_INTRINSIC,
  1564. TermCriteria criteria = TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, 1e-6) );
  1565. /** @brief Computes rectification transforms for each head of a calibrated stereo camera.
  1566. @param cameraMatrix1 First camera intrinsic matrix.
  1567. @param distCoeffs1 First camera distortion parameters.
  1568. @param cameraMatrix2 Second camera intrinsic matrix.
  1569. @param distCoeffs2 Second camera distortion parameters.
  1570. @param imageSize Size of the image used for stereo calibration.
  1571. @param R Rotation matrix from the coordinate system of the first camera to the second camera,
  1572. see @ref stereoCalibrate.
  1573. @param T Translation vector from the coordinate system of the first camera to the second camera,
  1574. see @ref stereoCalibrate.
  1575. @param R1 Output 3x3 rectification transform (rotation matrix) for the first camera. This matrix
  1576. brings points given in the unrectified first camera's coordinate system to points in the rectified
  1577. first camera's coordinate system. In more technical terms, it performs a change of basis from the
  1578. unrectified first camera's coordinate system to the rectified first camera's coordinate system.
  1579. @param R2 Output 3x3 rectification transform (rotation matrix) for the second camera. This matrix
  1580. brings points given in the unrectified second camera's coordinate system to points in the rectified
  1581. second camera's coordinate system. In more technical terms, it performs a change of basis from the
  1582. unrectified second camera's coordinate system to the rectified second camera's coordinate system.
  1583. @param P1 Output 3x4 projection matrix in the new (rectified) coordinate systems for the first
  1584. camera, i.e. it projects points given in the rectified first camera coordinate system into the
  1585. rectified first camera's image.
  1586. @param P2 Output 3x4 projection matrix in the new (rectified) coordinate systems for the second
  1587. camera, i.e. it projects points given in the rectified first camera coordinate system into the
  1588. rectified second camera's image.
  1589. @param Q Output \f$4 \times 4\f$ disparity-to-depth mapping matrix (see @ref reprojectImageTo3D).
  1590. @param flags Operation flags that may be zero or CALIB_ZERO_DISPARITY . If the flag is set,
  1591. the function makes the principal points of each camera have the same pixel coordinates in the
  1592. rectified views. And if the flag is not set, the function may still shift the images in the
  1593. horizontal or vertical direction (depending on the orientation of epipolar lines) to maximize the
  1594. useful image area.
  1595. @param alpha Free scaling parameter. If it is -1 or absent, the function performs the default
  1596. scaling. Otherwise, the parameter should be between 0 and 1. alpha=0 means that the rectified
  1597. images are zoomed and shifted so that only valid pixels are visible (no black areas after
  1598. rectification). alpha=1 means that the rectified image is decimated and shifted so that all the
  1599. pixels from the original images from the cameras are retained in the rectified images (no source
  1600. image pixels are lost). Any intermediate value yields an intermediate result between
  1601. those two extreme cases.
  1602. @param newImageSize New image resolution after rectification. The same size should be passed to
  1603. initUndistortRectifyMap (see the stereo_calib.cpp sample in OpenCV samples directory). When (0,0)
  1604. is passed (default), it is set to the original imageSize . Setting it to a larger value can help you
  1605. preserve details in the original image, especially when there is a big radial distortion.
  1606. @param validPixROI1 Optional output rectangles inside the rectified images where all the pixels
  1607. are valid. If alpha=0 , the ROIs cover the whole images. Otherwise, they are likely to be smaller
  1608. (see the picture below).
  1609. @param validPixROI2 Optional output rectangles inside the rectified images where all the pixels
  1610. are valid. If alpha=0 , the ROIs cover the whole images. Otherwise, they are likely to be smaller
  1611. (see the picture below).
  1612. The function computes the rotation matrices for each camera that (virtually) make both camera image
  1613. planes the same plane. Consequently, this makes all the epipolar lines parallel and thus simplifies
  1614. the dense stereo correspondence problem. The function takes the matrices computed by stereoCalibrate
  1615. as input. As output, it provides two rotation matrices and also two projection matrices in the new
  1616. coordinates. The function distinguishes the following two cases:
  1617. - **Horizontal stereo**: the first and the second camera views are shifted relative to each other
  1618. mainly along the x-axis (with possible small vertical shift). In the rectified images, the
  1619. corresponding epipolar lines in the left and right cameras are horizontal and have the same
  1620. y-coordinate. P1 and P2 look like:
  1621. \f[\texttt{P1} = \begin{bmatrix}
  1622. f & 0 & cx_1 & 0 \\
  1623. 0 & f & cy & 0 \\
  1624. 0 & 0 & 1 & 0
  1625. \end{bmatrix}\f]
  1626. \f[\texttt{P2} = \begin{bmatrix}
  1627. f & 0 & cx_2 & T_x*f \\
  1628. 0 & f & cy & 0 \\
  1629. 0 & 0 & 1 & 0
  1630. \end{bmatrix} ,\f]
  1631. where \f$T_x\f$ is a horizontal shift between the cameras and \f$cx_1=cx_2\f$ if
  1632. CALIB_ZERO_DISPARITY is set.
  1633. - **Vertical stereo**: the first and the second camera views are shifted relative to each other
  1634. mainly in the vertical direction (and probably a bit in the horizontal direction too). The epipolar
  1635. lines in the rectified images are vertical and have the same x-coordinate. P1 and P2 look like:
  1636. \f[\texttt{P1} = \begin{bmatrix}
  1637. f & 0 & cx & 0 \\
  1638. 0 & f & cy_1 & 0 \\
  1639. 0 & 0 & 1 & 0
  1640. \end{bmatrix}\f]
  1641. \f[\texttt{P2} = \begin{bmatrix}
  1642. f & 0 & cx & 0 \\
  1643. 0 & f & cy_2 & T_y*f \\
  1644. 0 & 0 & 1 & 0
  1645. \end{bmatrix},\f]
  1646. where \f$T_y\f$ is a vertical shift between the cameras and \f$cy_1=cy_2\f$ if
  1647. CALIB_ZERO_DISPARITY is set.
  1648. As you can see, the first three columns of P1 and P2 will effectively be the new "rectified" camera
  1649. matrices. The matrices, together with R1 and R2 , can then be passed to initUndistortRectifyMap to
  1650. initialize the rectification map for each camera.
  1651. See below the screenshot from the stereo_calib.cpp sample. Some red horizontal lines pass through
  1652. the corresponding image regions. This means that the images are well rectified, which is what most
  1653. stereo correspondence algorithms rely on. The green rectangles are roi1 and roi2 . You see that
  1654. their interiors are all valid pixels.
  1655. ![image](pics/stereo_undistort.jpg)
  1656. */
  1657. CV_EXPORTS_W void stereoRectify( InputArray cameraMatrix1, InputArray distCoeffs1,
  1658. InputArray cameraMatrix2, InputArray distCoeffs2,
  1659. Size imageSize, InputArray R, InputArray T,
  1660. OutputArray R1, OutputArray R2,
  1661. OutputArray P1, OutputArray P2,
  1662. OutputArray Q, int flags = CALIB_ZERO_DISPARITY,
  1663. double alpha = -1, Size newImageSize = Size(),
  1664. CV_OUT Rect* validPixROI1 = 0, CV_OUT Rect* validPixROI2 = 0 );
  1665. /** @brief Computes a rectification transform for an uncalibrated stereo camera.
  1666. @param points1 Array of feature points in the first image.
  1667. @param points2 The corresponding points in the second image. The same formats as in
  1668. findFundamentalMat are supported.
  1669. @param F Input fundamental matrix. It can be computed from the same set of point pairs using
  1670. findFundamentalMat .
  1671. @param imgSize Size of the image.
  1672. @param H1 Output rectification homography matrix for the first image.
  1673. @param H2 Output rectification homography matrix for the second image.
  1674. @param threshold Optional threshold used to filter out the outliers. If the parameter is greater
  1675. than zero, all the point pairs that do not comply with the epipolar geometry (that is, the points
  1676. for which \f$|\texttt{points2[i]}^T*\texttt{F}*\texttt{points1[i]}|>\texttt{threshold}\f$ ) are
  1677. rejected prior to computing the homographies. Otherwise, all the points are considered inliers.
  1678. The function computes the rectification transformations without knowing intrinsic parameters of the
  1679. cameras and their relative position in the space, which explains the suffix "uncalibrated". Another
  1680. related difference from stereoRectify is that the function outputs not the rectification
  1681. transformations in the object (3D) space, but the planar perspective transformations encoded by the
  1682. homography matrices H1 and H2 . The function implements the algorithm @cite Hartley99 .
  1683. @note
  1684. While the algorithm does not need to know the intrinsic parameters of the cameras, it heavily
  1685. depends on the epipolar geometry. Therefore, if the camera lenses have a significant distortion,
  1686. it would be better to correct it before computing the fundamental matrix and calling this
  1687. function. For example, distortion coefficients can be estimated for each head of stereo camera
  1688. separately by using calibrateCamera . Then, the images can be corrected using undistort , or
  1689. just the point coordinates can be corrected with undistortPoints .
  1690. */
  1691. CV_EXPORTS_W bool stereoRectifyUncalibrated( InputArray points1, InputArray points2,
  1692. InputArray F, Size imgSize,
  1693. OutputArray H1, OutputArray H2,
  1694. double threshold = 5 );
  1695. //! computes the rectification transformations for 3-head camera, where all the heads are on the same line.
  1696. CV_EXPORTS_W float rectify3Collinear( InputArray cameraMatrix1, InputArray distCoeffs1,
  1697. InputArray cameraMatrix2, InputArray distCoeffs2,
  1698. InputArray cameraMatrix3, InputArray distCoeffs3,
  1699. InputArrayOfArrays imgpt1, InputArrayOfArrays imgpt3,
  1700. Size imageSize, InputArray R12, InputArray T12,
  1701. InputArray R13, InputArray T13,
  1702. OutputArray R1, OutputArray R2, OutputArray R3,
  1703. OutputArray P1, OutputArray P2, OutputArray P3,
  1704. OutputArray Q, double alpha, Size newImgSize,
  1705. CV_OUT Rect* roi1, CV_OUT Rect* roi2, int flags );
  1706. /** @brief Returns the new camera intrinsic matrix based on the free scaling parameter.
  1707. @param cameraMatrix Input camera intrinsic matrix.
  1708. @param distCoeffs Input vector of distortion coefficients
  1709. \f$\distcoeffs\f$. If the vector is NULL/empty, the zero distortion coefficients are
  1710. assumed.
  1711. @param imageSize Original image size.
  1712. @param alpha Free scaling parameter between 0 (when all the pixels in the undistorted image are
  1713. valid) and 1 (when all the source image pixels are retained in the undistorted image). See
  1714. stereoRectify for details.
  1715. @param newImgSize Image size after rectification. By default, it is set to imageSize .
  1716. @param validPixROI Optional output rectangle that outlines all-good-pixels region in the
  1717. undistorted image. See roi1, roi2 description in stereoRectify .
  1718. @param centerPrincipalPoint Optional flag that indicates whether in the new camera intrinsic matrix the
  1719. principal point should be at the image center or not. By default, the principal point is chosen to
  1720. best fit a subset of the source image (determined by alpha) to the corrected image.
  1721. @return new_camera_matrix Output new camera intrinsic matrix.
  1722. The function computes and returns the optimal new camera intrinsic matrix based on the free scaling parameter.
  1723. By varying this parameter, you may retrieve only sensible pixels alpha=0 , keep all the original
  1724. image pixels if there is valuable information in the corners alpha=1 , or get something in between.
  1725. When alpha\>0 , the undistorted result is likely to have some black pixels corresponding to
  1726. "virtual" pixels outside of the captured distorted image. The original camera intrinsic matrix, distortion
  1727. coefficients, the computed new camera intrinsic matrix, and newImageSize should be passed to
  1728. initUndistortRectifyMap to produce the maps for remap .
  1729. */
  1730. CV_EXPORTS_W Mat getOptimalNewCameraMatrix( InputArray cameraMatrix, InputArray distCoeffs,
  1731. Size imageSize, double alpha, Size newImgSize = Size(),
  1732. CV_OUT Rect* validPixROI = 0,
  1733. bool centerPrincipalPoint = false);
  1734. /** @brief Computes Hand-Eye calibration: \f$_{}^{g}\textrm{T}_c\f$
  1735. @param[in] R_gripper2base Rotation part extracted from the homogeneous matrix that transforms a point
  1736. expressed in the gripper frame to the robot base frame (\f$_{}^{b}\textrm{T}_g\f$).
  1737. This is a vector (`vector<Mat>`) that contains the rotation, `(3x3)` rotation matrices or `(3x1)` rotation vectors,
  1738. for all the transformations from gripper frame to robot base frame.
  1739. @param[in] t_gripper2base Translation part extracted from the homogeneous matrix that transforms a point
  1740. expressed in the gripper frame to the robot base frame (\f$_{}^{b}\textrm{T}_g\f$).
  1741. This is a vector (`vector<Mat>`) that contains the `(3x1)` translation vectors for all the transformations
  1742. from gripper frame to robot base frame.
  1743. @param[in] R_target2cam Rotation part extracted from the homogeneous matrix that transforms a point
  1744. expressed in the target frame to the camera frame (\f$_{}^{c}\textrm{T}_t\f$).
  1745. This is a vector (`vector<Mat>`) that contains the rotation, `(3x3)` rotation matrices or `(3x1)` rotation vectors,
  1746. for all the transformations from calibration target frame to camera frame.
  1747. @param[in] t_target2cam Rotation part extracted from the homogeneous matrix that transforms a point
  1748. expressed in the target frame to the camera frame (\f$_{}^{c}\textrm{T}_t\f$).
  1749. This is a vector (`vector<Mat>`) that contains the `(3x1)` translation vectors for all the transformations
  1750. from calibration target frame to camera frame.
  1751. @param[out] R_cam2gripper Estimated `(3x3)` rotation part extracted from the homogeneous matrix that transforms a point
  1752. expressed in the camera frame to the gripper frame (\f$_{}^{g}\textrm{T}_c\f$).
  1753. @param[out] t_cam2gripper Estimated `(3x1)` translation part extracted from the homogeneous matrix that transforms a point
  1754. expressed in the camera frame to the gripper frame (\f$_{}^{g}\textrm{T}_c\f$).
  1755. @param[in] method One of the implemented Hand-Eye calibration method, see cv::HandEyeCalibrationMethod
  1756. The function performs the Hand-Eye calibration using various methods. One approach consists in estimating the
  1757. rotation then the translation (separable solutions) and the following methods are implemented:
  1758. - R. Tsai, R. Lenz A New Technique for Fully Autonomous and Efficient 3D Robotics Hand/EyeCalibration \cite Tsai89
  1759. - F. Park, B. Martin Robot Sensor Calibration: Solving AX = XB on the Euclidean Group \cite Park94
  1760. - R. Horaud, F. Dornaika Hand-Eye Calibration \cite Horaud95
  1761. Another approach consists in estimating simultaneously the rotation and the translation (simultaneous solutions),
  1762. with the following implemented method:
  1763. - N. Andreff, R. Horaud, B. Espiau On-line Hand-Eye Calibration \cite Andreff99
  1764. - K. Daniilidis Hand-Eye Calibration Using Dual Quaternions \cite Daniilidis98
  1765. The following picture describes the Hand-Eye calibration problem where the transformation between a camera ("eye")
  1766. mounted on a robot gripper ("hand") has to be estimated.
  1767. ![](pics/hand-eye_figure.png)
  1768. The calibration procedure is the following:
  1769. - a static calibration pattern is used to estimate the transformation between the target frame
  1770. and the camera frame
  1771. - the robot gripper is moved in order to acquire several poses
  1772. - for each pose, the homogeneous transformation between the gripper frame and the robot base frame is recorded using for
  1773. instance the robot kinematics
  1774. \f[
  1775. \begin{bmatrix}
  1776. X_b\\
  1777. Y_b\\
  1778. Z_b\\
  1779. 1
  1780. \end{bmatrix}
  1781. =
  1782. \begin{bmatrix}
  1783. _{}^{b}\textrm{R}_g & _{}^{b}\textrm{t}_g \\
  1784. 0_{1 \times 3} & 1
  1785. \end{bmatrix}
  1786. \begin{bmatrix}
  1787. X_g\\
  1788. Y_g\\
  1789. Z_g\\
  1790. 1
  1791. \end{bmatrix}
  1792. \f]
  1793. - for each pose, the homogeneous transformation between the calibration target frame and the camera frame is recorded using
  1794. for instance a pose estimation method (PnP) from 2D-3D point correspondences
  1795. \f[
  1796. \begin{bmatrix}
  1797. X_c\\
  1798. Y_c\\
  1799. Z_c\\
  1800. 1
  1801. \end{bmatrix}
  1802. =
  1803. \begin{bmatrix}
  1804. _{}^{c}\textrm{R}_t & _{}^{c}\textrm{t}_t \\
  1805. 0_{1 \times 3} & 1
  1806. \end{bmatrix}
  1807. \begin{bmatrix}
  1808. X_t\\
  1809. Y_t\\
  1810. Z_t\\
  1811. 1
  1812. \end{bmatrix}
  1813. \f]
  1814. The Hand-Eye calibration procedure returns the following homogeneous transformation
  1815. \f[
  1816. \begin{bmatrix}
  1817. X_g\\
  1818. Y_g\\
  1819. Z_g\\
  1820. 1
  1821. \end{bmatrix}
  1822. =
  1823. \begin{bmatrix}
  1824. _{}^{g}\textrm{R}_c & _{}^{g}\textrm{t}_c \\
  1825. 0_{1 \times 3} & 1
  1826. \end{bmatrix}
  1827. \begin{bmatrix}
  1828. X_c\\
  1829. Y_c\\
  1830. Z_c\\
  1831. 1
  1832. \end{bmatrix}
  1833. \f]
  1834. This problem is also known as solving the \f$\mathbf{A}\mathbf{X}=\mathbf{X}\mathbf{B}\f$ equation:
  1835. \f[
  1836. \begin{align*}
  1837. ^{b}{\textrm{T}_g}^{(1)} \hspace{0.2em} ^{g}\textrm{T}_c \hspace{0.2em} ^{c}{\textrm{T}_t}^{(1)} &=
  1838. \hspace{0.1em} ^{b}{\textrm{T}_g}^{(2)} \hspace{0.2em} ^{g}\textrm{T}_c \hspace{0.2em} ^{c}{\textrm{T}_t}^{(2)} \\
  1839. (^{b}{\textrm{T}_g}^{(2)})^{-1} \hspace{0.2em} ^{b}{\textrm{T}_g}^{(1)} \hspace{0.2em} ^{g}\textrm{T}_c &=
  1840. \hspace{0.1em} ^{g}\textrm{T}_c \hspace{0.2em} ^{c}{\textrm{T}_t}^{(2)} (^{c}{\textrm{T}_t}^{(1)})^{-1} \\
  1841. \textrm{A}_i \textrm{X} &= \textrm{X} \textrm{B}_i \\
  1842. \end{align*}
  1843. \f]
  1844. \note
  1845. Additional information can be found on this [website](http://campar.in.tum.de/Chair/HandEyeCalibration).
  1846. \note
  1847. A minimum of 2 motions with non parallel rotation axes are necessary to determine the hand-eye transformation.
  1848. So at least 3 different poses are required, but it is strongly recommended to use many more poses.
  1849. */
  1850. CV_EXPORTS_W void calibrateHandEye( InputArrayOfArrays R_gripper2base, InputArrayOfArrays t_gripper2base,
  1851. InputArrayOfArrays R_target2cam, InputArrayOfArrays t_target2cam,
  1852. OutputArray R_cam2gripper, OutputArray t_cam2gripper,
  1853. HandEyeCalibrationMethod method=CALIB_HAND_EYE_TSAI );
  1854. /** @brief Converts points from Euclidean to homogeneous space.
  1855. @param src Input vector of N-dimensional points.
  1856. @param dst Output vector of N+1-dimensional points.
  1857. The function converts points from Euclidean to homogeneous space by appending 1's to the tuple of
  1858. point coordinates. That is, each point (x1, x2, ..., xn) is converted to (x1, x2, ..., xn, 1).
  1859. */
  1860. CV_EXPORTS_W void convertPointsToHomogeneous( InputArray src, OutputArray dst );
  1861. /** @brief Converts points from homogeneous to Euclidean space.
  1862. @param src Input vector of N-dimensional points.
  1863. @param dst Output vector of N-1-dimensional points.
  1864. The function converts points homogeneous to Euclidean space using perspective projection. That is,
  1865. each point (x1, x2, ... x(n-1), xn) is converted to (x1/xn, x2/xn, ..., x(n-1)/xn). When xn=0, the
  1866. output point coordinates will be (0,0,0,...).
  1867. */
  1868. CV_EXPORTS_W void convertPointsFromHomogeneous( InputArray src, OutputArray dst );
  1869. /** @brief Converts points to/from homogeneous coordinates.
  1870. @param src Input array or vector of 2D, 3D, or 4D points.
  1871. @param dst Output vector of 2D, 3D, or 4D points.
  1872. The function converts 2D or 3D points from/to homogeneous coordinates by calling either
  1873. convertPointsToHomogeneous or convertPointsFromHomogeneous.
  1874. @note The function is obsolete. Use one of the previous two functions instead.
  1875. */
  1876. CV_EXPORTS void convertPointsHomogeneous( InputArray src, OutputArray dst );
  1877. /** @brief Calculates a fundamental matrix from the corresponding points in two images.
  1878. @param points1 Array of N points from the first image. The point coordinates should be
  1879. floating-point (single or double precision).
  1880. @param points2 Array of the second image points of the same size and format as points1 .
  1881. @param method Method for computing a fundamental matrix.
  1882. - **CV_FM_7POINT** for a 7-point algorithm. \f$N = 7\f$
  1883. - **CV_FM_8POINT** for an 8-point algorithm. \f$N \ge 8\f$
  1884. - **CV_FM_RANSAC** for the RANSAC algorithm. \f$N \ge 8\f$
  1885. - **CV_FM_LMEDS** for the LMedS algorithm. \f$N \ge 8\f$
  1886. @param ransacReprojThreshold Parameter used only for RANSAC. It is the maximum distance from a point to an epipolar
  1887. line in pixels, beyond which the point is considered an outlier and is not used for computing the
  1888. final fundamental matrix. It can be set to something like 1-3, depending on the accuracy of the
  1889. point localization, image resolution, and the image noise.
  1890. @param confidence Parameter used for the RANSAC and LMedS methods only. It specifies a desirable level
  1891. of confidence (probability) that the estimated matrix is correct.
  1892. @param[out] mask optional output mask
  1893. @param maxIters The maximum number of robust method iterations.
  1894. The epipolar geometry is described by the following equation:
  1895. \f[[p_2; 1]^T F [p_1; 1] = 0\f]
  1896. where \f$F\f$ is a fundamental matrix, \f$p_1\f$ and \f$p_2\f$ are corresponding points in the first and the
  1897. second images, respectively.
  1898. The function calculates the fundamental matrix using one of four methods listed above and returns
  1899. the found fundamental matrix. Normally just one matrix is found. But in case of the 7-point
  1900. algorithm, the function may return up to 3 solutions ( \f$9 \times 3\f$ matrix that stores all 3
  1901. matrices sequentially).
  1902. The calculated fundamental matrix may be passed further to computeCorrespondEpilines that finds the
  1903. epipolar lines corresponding to the specified points. It can also be passed to
  1904. stereoRectifyUncalibrated to compute the rectification transformation. :
  1905. @code
  1906. // Example. Estimation of fundamental matrix using the RANSAC algorithm
  1907. int point_count = 100;
  1908. vector<Point2f> points1(point_count);
  1909. vector<Point2f> points2(point_count);
  1910. // initialize the points here ...
  1911. for( int i = 0; i < point_count; i++ )
  1912. {
  1913. points1[i] = ...;
  1914. points2[i] = ...;
  1915. }
  1916. Mat fundamental_matrix =
  1917. findFundamentalMat(points1, points2, FM_RANSAC, 3, 0.99);
  1918. @endcode
  1919. */
  1920. CV_EXPORTS_W Mat findFundamentalMat( InputArray points1, InputArray points2,
  1921. int method, double ransacReprojThreshold, double confidence,
  1922. int maxIters, OutputArray mask = noArray() );
  1923. /** @overload */
  1924. CV_EXPORTS_W Mat findFundamentalMat( InputArray points1, InputArray points2,
  1925. int method = FM_RANSAC,
  1926. double ransacReprojThreshold = 3., double confidence = 0.99,
  1927. OutputArray mask = noArray() );
  1928. /** @overload */
  1929. CV_EXPORTS Mat findFundamentalMat( InputArray points1, InputArray points2,
  1930. OutputArray mask, int method = FM_RANSAC,
  1931. double ransacReprojThreshold = 3., double confidence = 0.99 );
  1932. /** @brief Calculates an essential matrix from the corresponding points in two images.
  1933. @param points1 Array of N (N \>= 5) 2D points from the first image. The point coordinates should
  1934. be floating-point (single or double precision).
  1935. @param points2 Array of the second image points of the same size and format as points1 .
  1936. @param cameraMatrix Camera intrinsic matrix \f$\cameramatrix{A}\f$ .
  1937. Note that this function assumes that points1 and points2 are feature points from cameras with the
  1938. same camera intrinsic matrix. If this assumption does not hold for your use case, use
  1939. `undistortPoints()` with `P = cv::NoArray()` for both cameras to transform image points
  1940. to normalized image coordinates, which are valid for the identity camera intrinsic matrix. When
  1941. passing these coordinates, pass the identity matrix for this parameter.
  1942. @param method Method for computing an essential matrix.
  1943. - **RANSAC** for the RANSAC algorithm.
  1944. - **LMEDS** for the LMedS algorithm.
  1945. @param prob Parameter used for the RANSAC or LMedS methods only. It specifies a desirable level of
  1946. confidence (probability) that the estimated matrix is correct.
  1947. @param threshold Parameter used for RANSAC. It is the maximum distance from a point to an epipolar
  1948. line in pixels, beyond which the point is considered an outlier and is not used for computing the
  1949. final fundamental matrix. It can be set to something like 1-3, depending on the accuracy of the
  1950. point localization, image resolution, and the image noise.
  1951. @param mask Output array of N elements, every element of which is set to 0 for outliers and to 1
  1952. for the other points. The array is computed only in the RANSAC and LMedS methods.
  1953. This function estimates essential matrix based on the five-point algorithm solver in @cite Nister03 .
  1954. @cite SteweniusCFS is also a related. The epipolar geometry is described by the following equation:
  1955. \f[[p_2; 1]^T K^{-T} E K^{-1} [p_1; 1] = 0\f]
  1956. where \f$E\f$ is an essential matrix, \f$p_1\f$ and \f$p_2\f$ are corresponding points in the first and the
  1957. second images, respectively. The result of this function may be passed further to
  1958. decomposeEssentialMat or recoverPose to recover the relative pose between cameras.
  1959. */
  1960. CV_EXPORTS_W Mat findEssentialMat( InputArray points1, InputArray points2,
  1961. InputArray cameraMatrix, int method = RANSAC,
  1962. double prob = 0.999, double threshold = 1.0,
  1963. OutputArray mask = noArray() );
  1964. /** @overload
  1965. @param points1 Array of N (N \>= 5) 2D points from the first image. The point coordinates should
  1966. be floating-point (single or double precision).
  1967. @param points2 Array of the second image points of the same size and format as points1 .
  1968. @param focal focal length of the camera. Note that this function assumes that points1 and points2
  1969. are feature points from cameras with same focal length and principal point.
  1970. @param pp principal point of the camera.
  1971. @param method Method for computing a fundamental matrix.
  1972. - **RANSAC** for the RANSAC algorithm.
  1973. - **LMEDS** for the LMedS algorithm.
  1974. @param threshold Parameter used for RANSAC. It is the maximum distance from a point to an epipolar
  1975. line in pixels, beyond which the point is considered an outlier and is not used for computing the
  1976. final fundamental matrix. It can be set to something like 1-3, depending on the accuracy of the
  1977. point localization, image resolution, and the image noise.
  1978. @param prob Parameter used for the RANSAC or LMedS methods only. It specifies a desirable level of
  1979. confidence (probability) that the estimated matrix is correct.
  1980. @param mask Output array of N elements, every element of which is set to 0 for outliers and to 1
  1981. for the other points. The array is computed only in the RANSAC and LMedS methods.
  1982. This function differs from the one above that it computes camera intrinsic matrix from focal length and
  1983. principal point:
  1984. \f[A =
  1985. \begin{bmatrix}
  1986. f & 0 & x_{pp} \\
  1987. 0 & f & y_{pp} \\
  1988. 0 & 0 & 1
  1989. \end{bmatrix}\f]
  1990. */
  1991. CV_EXPORTS_W Mat findEssentialMat( InputArray points1, InputArray points2,
  1992. double focal = 1.0, Point2d pp = Point2d(0, 0),
  1993. int method = RANSAC, double prob = 0.999,
  1994. double threshold = 1.0, OutputArray mask = noArray() );
  1995. /** @brief Decompose an essential matrix to possible rotations and translation.
  1996. @param E The input essential matrix.
  1997. @param R1 One possible rotation matrix.
  1998. @param R2 Another possible rotation matrix.
  1999. @param t One possible translation.
  2000. This function decomposes the essential matrix E using svd decomposition @cite HartleyZ00. In
  2001. general, four possible poses exist for the decomposition of E. They are \f$[R_1, t]\f$,
  2002. \f$[R_1, -t]\f$, \f$[R_2, t]\f$, \f$[R_2, -t]\f$.
  2003. If E gives the epipolar constraint \f$[p_2; 1]^T A^{-T} E A^{-1} [p_1; 1] = 0\f$ between the image
  2004. points \f$p_1\f$ in the first image and \f$p_2\f$ in second image, then any of the tuples
  2005. \f$[R_1, t]\f$, \f$[R_1, -t]\f$, \f$[R_2, t]\f$, \f$[R_2, -t]\f$ is a change of basis from the first
  2006. camera's coordinate system to the second camera's coordinate system. However, by decomposing E, one
  2007. can only get the direction of the translation. For this reason, the translation t is returned with
  2008. unit length.
  2009. */
  2010. CV_EXPORTS_W void decomposeEssentialMat( InputArray E, OutputArray R1, OutputArray R2, OutputArray t );
  2011. /** @brief Recovers the relative camera rotation and the translation from an estimated essential
  2012. matrix and the corresponding points in two images, using cheirality check. Returns the number of
  2013. inliers that pass the check.
  2014. @param E The input essential matrix.
  2015. @param points1 Array of N 2D points from the first image. The point coordinates should be
  2016. floating-point (single or double precision).
  2017. @param points2 Array of the second image points of the same size and format as points1 .
  2018. @param cameraMatrix Camera intrinsic matrix \f$\cameramatrix{A}\f$ .
  2019. Note that this function assumes that points1 and points2 are feature points from cameras with the
  2020. same camera intrinsic matrix.
  2021. @param R Output rotation matrix. Together with the translation vector, this matrix makes up a tuple
  2022. that performs a change of basis from the first camera's coordinate system to the second camera's
  2023. coordinate system. Note that, in general, t can not be used for this tuple, see the parameter
  2024. described below.
  2025. @param t Output translation vector. This vector is obtained by @ref decomposeEssentialMat and
  2026. therefore is only known up to scale, i.e. t is the direction of the translation vector and has unit
  2027. length.
  2028. @param mask Input/output mask for inliers in points1 and points2. If it is not empty, then it marks
  2029. inliers in points1 and points2 for then given essential matrix E. Only these inliers will be used to
  2030. recover pose. In the output mask only inliers which pass the cheirality check.
  2031. This function decomposes an essential matrix using @ref decomposeEssentialMat and then verifies
  2032. possible pose hypotheses by doing cheirality check. The cheirality check means that the
  2033. triangulated 3D points should have positive depth. Some details can be found in @cite Nister03.
  2034. This function can be used to process the output E and mask from @ref findEssentialMat. In this
  2035. scenario, points1 and points2 are the same input for findEssentialMat.:
  2036. @code
  2037. // Example. Estimation of fundamental matrix using the RANSAC algorithm
  2038. int point_count = 100;
  2039. vector<Point2f> points1(point_count);
  2040. vector<Point2f> points2(point_count);
  2041. // initialize the points here ...
  2042. for( int i = 0; i < point_count; i++ )
  2043. {
  2044. points1[i] = ...;
  2045. points2[i] = ...;
  2046. }
  2047. // cametra matrix with both focal lengths = 1, and principal point = (0, 0)
  2048. Mat cameraMatrix = Mat::eye(3, 3, CV_64F);
  2049. Mat E, R, t, mask;
  2050. E = findEssentialMat(points1, points2, cameraMatrix, RANSAC, 0.999, 1.0, mask);
  2051. recoverPose(E, points1, points2, cameraMatrix, R, t, mask);
  2052. @endcode
  2053. */
  2054. CV_EXPORTS_W int recoverPose( InputArray E, InputArray points1, InputArray points2,
  2055. InputArray cameraMatrix, OutputArray R, OutputArray t,
  2056. InputOutputArray mask = noArray() );
  2057. /** @overload
  2058. @param E The input essential matrix.
  2059. @param points1 Array of N 2D points from the first image. The point coordinates should be
  2060. floating-point (single or double precision).
  2061. @param points2 Array of the second image points of the same size and format as points1 .
  2062. @param R Output rotation matrix. Together with the translation vector, this matrix makes up a tuple
  2063. that performs a change of basis from the first camera's coordinate system to the second camera's
  2064. coordinate system. Note that, in general, t can not be used for this tuple, see the parameter
  2065. description below.
  2066. @param t Output translation vector. This vector is obtained by @ref decomposeEssentialMat and
  2067. therefore is only known up to scale, i.e. t is the direction of the translation vector and has unit
  2068. length.
  2069. @param focal Focal length of the camera. Note that this function assumes that points1 and points2
  2070. are feature points from cameras with same focal length and principal point.
  2071. @param pp principal point of the camera.
  2072. @param mask Input/output mask for inliers in points1 and points2. If it is not empty, then it marks
  2073. inliers in points1 and points2 for then given essential matrix E. Only these inliers will be used to
  2074. recover pose. In the output mask only inliers which pass the cheirality check.
  2075. This function differs from the one above that it computes camera intrinsic matrix from focal length and
  2076. principal point:
  2077. \f[A =
  2078. \begin{bmatrix}
  2079. f & 0 & x_{pp} \\
  2080. 0 & f & y_{pp} \\
  2081. 0 & 0 & 1
  2082. \end{bmatrix}\f]
  2083. */
  2084. CV_EXPORTS_W int recoverPose( InputArray E, InputArray points1, InputArray points2,
  2085. OutputArray R, OutputArray t,
  2086. double focal = 1.0, Point2d pp = Point2d(0, 0),
  2087. InputOutputArray mask = noArray() );
  2088. /** @overload
  2089. @param E The input essential matrix.
  2090. @param points1 Array of N 2D points from the first image. The point coordinates should be
  2091. floating-point (single or double precision).
  2092. @param points2 Array of the second image points of the same size and format as points1.
  2093. @param cameraMatrix Camera intrinsic matrix \f$\cameramatrix{A}\f$ .
  2094. Note that this function assumes that points1 and points2 are feature points from cameras with the
  2095. same camera intrinsic matrix.
  2096. @param R Output rotation matrix. Together with the translation vector, this matrix makes up a tuple
  2097. that performs a change of basis from the first camera's coordinate system to the second camera's
  2098. coordinate system. Note that, in general, t can not be used for this tuple, see the parameter
  2099. description below.
  2100. @param t Output translation vector. This vector is obtained by @ref decomposeEssentialMat and
  2101. therefore is only known up to scale, i.e. t is the direction of the translation vector and has unit
  2102. length.
  2103. @param distanceThresh threshold distance which is used to filter out far away points (i.e. infinite
  2104. points).
  2105. @param mask Input/output mask for inliers in points1 and points2. If it is not empty, then it marks
  2106. inliers in points1 and points2 for then given essential matrix E. Only these inliers will be used to
  2107. recover pose. In the output mask only inliers which pass the cheirality check.
  2108. @param triangulatedPoints 3D points which were reconstructed by triangulation.
  2109. This function differs from the one above that it outputs the triangulated 3D point that are used for
  2110. the cheirality check.
  2111. */
  2112. CV_EXPORTS_W int recoverPose( InputArray E, InputArray points1, InputArray points2,
  2113. InputArray cameraMatrix, OutputArray R, OutputArray t, double distanceThresh, InputOutputArray mask = noArray(),
  2114. OutputArray triangulatedPoints = noArray());
  2115. /** @brief For points in an image of a stereo pair, computes the corresponding epilines in the other image.
  2116. @param points Input points. \f$N \times 1\f$ or \f$1 \times N\f$ matrix of type CV_32FC2 or
  2117. vector\<Point2f\> .
  2118. @param whichImage Index of the image (1 or 2) that contains the points .
  2119. @param F Fundamental matrix that can be estimated using findFundamentalMat or stereoRectify .
  2120. @param lines Output vector of the epipolar lines corresponding to the points in the other image.
  2121. Each line \f$ax + by + c=0\f$ is encoded by 3 numbers \f$(a, b, c)\f$ .
  2122. For every point in one of the two images of a stereo pair, the function finds the equation of the
  2123. corresponding epipolar line in the other image.
  2124. From the fundamental matrix definition (see findFundamentalMat ), line \f$l^{(2)}_i\f$ in the second
  2125. image for the point \f$p^{(1)}_i\f$ in the first image (when whichImage=1 ) is computed as:
  2126. \f[l^{(2)}_i = F p^{(1)}_i\f]
  2127. And vice versa, when whichImage=2, \f$l^{(1)}_i\f$ is computed from \f$p^{(2)}_i\f$ as:
  2128. \f[l^{(1)}_i = F^T p^{(2)}_i\f]
  2129. Line coefficients are defined up to a scale. They are normalized so that \f$a_i^2+b_i^2=1\f$ .
  2130. */
  2131. CV_EXPORTS_W void computeCorrespondEpilines( InputArray points, int whichImage,
  2132. InputArray F, OutputArray lines );
  2133. /** @brief This function reconstructs 3-dimensional points (in homogeneous coordinates) by using
  2134. their observations with a stereo camera.
  2135. @param projMatr1 3x4 projection matrix of the first camera, i.e. this matrix projects 3D points
  2136. given in the world's coordinate system into the first image.
  2137. @param projMatr2 3x4 projection matrix of the second camera, i.e. this matrix projects 3D points
  2138. given in the world's coordinate system into the second image.
  2139. @param projPoints1 2xN array of feature points in the first image. In the case of the c++ version,
  2140. it can be also a vector of feature points or two-channel matrix of size 1xN or Nx1.
  2141. @param projPoints2 2xN array of corresponding points in the second image. In the case of the c++
  2142. version, it can be also a vector of feature points or two-channel matrix of size 1xN or Nx1.
  2143. @param points4D 4xN array of reconstructed points in homogeneous coordinates. These points are
  2144. returned in the world's coordinate system.
  2145. @note
  2146. Keep in mind that all input data should be of float type in order for this function to work.
  2147. @note
  2148. If the projection matrices from @ref stereoRectify are used, then the returned points are
  2149. represented in the first camera's rectified coordinate system.
  2150. @sa
  2151. reprojectImageTo3D
  2152. */
  2153. CV_EXPORTS_W void triangulatePoints( InputArray projMatr1, InputArray projMatr2,
  2154. InputArray projPoints1, InputArray projPoints2,
  2155. OutputArray points4D );
  2156. /** @brief Refines coordinates of corresponding points.
  2157. @param F 3x3 fundamental matrix.
  2158. @param points1 1xN array containing the first set of points.
  2159. @param points2 1xN array containing the second set of points.
  2160. @param newPoints1 The optimized points1.
  2161. @param newPoints2 The optimized points2.
  2162. The function implements the Optimal Triangulation Method (see Multiple View Geometry for details).
  2163. For each given point correspondence points1[i] \<-\> points2[i], and a fundamental matrix F, it
  2164. computes the corrected correspondences newPoints1[i] \<-\> newPoints2[i] that minimize the geometric
  2165. error \f$d(points1[i], newPoints1[i])^2 + d(points2[i],newPoints2[i])^2\f$ (where \f$d(a,b)\f$ is the
  2166. geometric distance between points \f$a\f$ and \f$b\f$ ) subject to the epipolar constraint
  2167. \f$newPoints2^T * F * newPoints1 = 0\f$ .
  2168. */
  2169. CV_EXPORTS_W void correctMatches( InputArray F, InputArray points1, InputArray points2,
  2170. OutputArray newPoints1, OutputArray newPoints2 );
  2171. /** @brief Filters off small noise blobs (speckles) in the disparity map
  2172. @param img The input 16-bit signed disparity image
  2173. @param newVal The disparity value used to paint-off the speckles
  2174. @param maxSpeckleSize The maximum speckle size to consider it a speckle. Larger blobs are not
  2175. affected by the algorithm
  2176. @param maxDiff Maximum difference between neighbor disparity pixels to put them into the same
  2177. blob. Note that since StereoBM, StereoSGBM and may be other algorithms return a fixed-point
  2178. disparity map, where disparity values are multiplied by 16, this scale factor should be taken into
  2179. account when specifying this parameter value.
  2180. @param buf The optional temporary buffer to avoid memory allocation within the function.
  2181. */
  2182. CV_EXPORTS_W void filterSpeckles( InputOutputArray img, double newVal,
  2183. int maxSpeckleSize, double maxDiff,
  2184. InputOutputArray buf = noArray() );
  2185. //! computes valid disparity ROI from the valid ROIs of the rectified images (that are returned by cv::stereoRectify())
  2186. CV_EXPORTS_W Rect getValidDisparityROI( Rect roi1, Rect roi2,
  2187. int minDisparity, int numberOfDisparities,
  2188. int blockSize );
  2189. //! validates disparity using the left-right check. The matrix "cost" should be computed by the stereo correspondence algorithm
  2190. CV_EXPORTS_W void validateDisparity( InputOutputArray disparity, InputArray cost,
  2191. int minDisparity, int numberOfDisparities,
  2192. int disp12MaxDisp = 1 );
  2193. /** @brief Reprojects a disparity image to 3D space.
  2194. @param disparity Input single-channel 8-bit unsigned, 16-bit signed, 32-bit signed or 32-bit
  2195. floating-point disparity image. The values of 8-bit / 16-bit signed formats are assumed to have no
  2196. fractional bits. If the disparity is 16-bit signed format, as computed by @ref StereoBM or
  2197. @ref StereoSGBM and maybe other algorithms, it should be divided by 16 (and scaled to float) before
  2198. being used here.
  2199. @param _3dImage Output 3-channel floating-point image of the same size as disparity. Each element of
  2200. _3dImage(x,y) contains 3D coordinates of the point (x,y) computed from the disparity map. If one
  2201. uses Q obtained by @ref stereoRectify, then the returned points are represented in the first
  2202. camera's rectified coordinate system.
  2203. @param Q \f$4 \times 4\f$ perspective transformation matrix that can be obtained with
  2204. @ref stereoRectify.
  2205. @param handleMissingValues Indicates, whether the function should handle missing values (i.e.
  2206. points where the disparity was not computed). If handleMissingValues=true, then pixels with the
  2207. minimal disparity that corresponds to the outliers (see StereoMatcher::compute ) are transformed
  2208. to 3D points with a very large Z value (currently set to 10000).
  2209. @param ddepth The optional output array depth. If it is -1, the output image will have CV_32F
  2210. depth. ddepth can also be set to CV_16S, CV_32S or CV_32F.
  2211. The function transforms a single-channel disparity map to a 3-channel image representing a 3D
  2212. surface. That is, for each pixel (x,y) and the corresponding disparity d=disparity(x,y) , it
  2213. computes:
  2214. \f[\begin{bmatrix}
  2215. X \\
  2216. Y \\
  2217. Z \\
  2218. W
  2219. \end{bmatrix} = Q \begin{bmatrix}
  2220. x \\
  2221. y \\
  2222. \texttt{disparity} (x,y) \\
  2223. z
  2224. \end{bmatrix}.\f]
  2225. @sa
  2226. To reproject a sparse set of points {(x,y,d),...} to 3D space, use perspectiveTransform.
  2227. */
  2228. CV_EXPORTS_W void reprojectImageTo3D( InputArray disparity,
  2229. OutputArray _3dImage, InputArray Q,
  2230. bool handleMissingValues = false,
  2231. int ddepth = -1 );
  2232. /** @brief Calculates the Sampson Distance between two points.
  2233. The function cv::sampsonDistance calculates and returns the first order approximation of the geometric error as:
  2234. \f[
  2235. sd( \texttt{pt1} , \texttt{pt2} )=
  2236. \frac{(\texttt{pt2}^t \cdot \texttt{F} \cdot \texttt{pt1})^2}
  2237. {((\texttt{F} \cdot \texttt{pt1})(0))^2 +
  2238. ((\texttt{F} \cdot \texttt{pt1})(1))^2 +
  2239. ((\texttt{F}^t \cdot \texttt{pt2})(0))^2 +
  2240. ((\texttt{F}^t \cdot \texttt{pt2})(1))^2}
  2241. \f]
  2242. The fundamental matrix may be calculated using the cv::findFundamentalMat function. See @cite HartleyZ00 11.4.3 for details.
  2243. @param pt1 first homogeneous 2d point
  2244. @param pt2 second homogeneous 2d point
  2245. @param F fundamental matrix
  2246. @return The computed Sampson distance.
  2247. */
  2248. CV_EXPORTS_W double sampsonDistance(InputArray pt1, InputArray pt2, InputArray F);
  2249. /** @brief Computes an optimal affine transformation between two 3D point sets.
  2250. It computes
  2251. \f[
  2252. \begin{bmatrix}
  2253. x\\
  2254. y\\
  2255. z\\
  2256. \end{bmatrix}
  2257. =
  2258. \begin{bmatrix}
  2259. a_{11} & a_{12} & a_{13}\\
  2260. a_{21} & a_{22} & a_{23}\\
  2261. a_{31} & a_{32} & a_{33}\\
  2262. \end{bmatrix}
  2263. \begin{bmatrix}
  2264. X\\
  2265. Y\\
  2266. Z\\
  2267. \end{bmatrix}
  2268. +
  2269. \begin{bmatrix}
  2270. b_1\\
  2271. b_2\\
  2272. b_3\\
  2273. \end{bmatrix}
  2274. \f]
  2275. @param src First input 3D point set containing \f$(X,Y,Z)\f$.
  2276. @param dst Second input 3D point set containing \f$(x,y,z)\f$.
  2277. @param out Output 3D affine transformation matrix \f$3 \times 4\f$ of the form
  2278. \f[
  2279. \begin{bmatrix}
  2280. a_{11} & a_{12} & a_{13} & b_1\\
  2281. a_{21} & a_{22} & a_{23} & b_2\\
  2282. a_{31} & a_{32} & a_{33} & b_3\\
  2283. \end{bmatrix}
  2284. \f]
  2285. @param inliers Output vector indicating which points are inliers (1-inlier, 0-outlier).
  2286. @param ransacThreshold Maximum reprojection error in the RANSAC algorithm to consider a point as
  2287. an inlier.
  2288. @param confidence Confidence level, between 0 and 1, for the estimated transformation. Anything
  2289. between 0.95 and 0.99 is usually good enough. Values too close to 1 can slow down the estimation
  2290. significantly. Values lower than 0.8-0.9 can result in an incorrectly estimated transformation.
  2291. The function estimates an optimal 3D affine transformation between two 3D point sets using the
  2292. RANSAC algorithm.
  2293. */
  2294. CV_EXPORTS_W int estimateAffine3D(InputArray src, InputArray dst,
  2295. OutputArray out, OutputArray inliers,
  2296. double ransacThreshold = 3, double confidence = 0.99);
  2297. /** @brief Computes an optimal affine transformation between two 2D point sets.
  2298. It computes
  2299. \f[
  2300. \begin{bmatrix}
  2301. x\\
  2302. y\\
  2303. \end{bmatrix}
  2304. =
  2305. \begin{bmatrix}
  2306. a_{11} & a_{12}\\
  2307. a_{21} & a_{22}\\
  2308. \end{bmatrix}
  2309. \begin{bmatrix}
  2310. X\\
  2311. Y\\
  2312. \end{bmatrix}
  2313. +
  2314. \begin{bmatrix}
  2315. b_1\\
  2316. b_2\\
  2317. \end{bmatrix}
  2318. \f]
  2319. @param from First input 2D point set containing \f$(X,Y)\f$.
  2320. @param to Second input 2D point set containing \f$(x,y)\f$.
  2321. @param inliers Output vector indicating which points are inliers (1-inlier, 0-outlier).
  2322. @param method Robust method used to compute transformation. The following methods are possible:
  2323. - cv::RANSAC - RANSAC-based robust method
  2324. - cv::LMEDS - Least-Median robust method
  2325. RANSAC is the default method.
  2326. @param ransacReprojThreshold Maximum reprojection error in the RANSAC algorithm to consider
  2327. a point as an inlier. Applies only to RANSAC.
  2328. @param maxIters The maximum number of robust method iterations.
  2329. @param confidence Confidence level, between 0 and 1, for the estimated transformation. Anything
  2330. between 0.95 and 0.99 is usually good enough. Values too close to 1 can slow down the estimation
  2331. significantly. Values lower than 0.8-0.9 can result in an incorrectly estimated transformation.
  2332. @param refineIters Maximum number of iterations of refining algorithm (Levenberg-Marquardt).
  2333. Passing 0 will disable refining, so the output matrix will be output of robust method.
  2334. @return Output 2D affine transformation matrix \f$2 \times 3\f$ or empty matrix if transformation
  2335. could not be estimated. The returned matrix has the following form:
  2336. \f[
  2337. \begin{bmatrix}
  2338. a_{11} & a_{12} & b_1\\
  2339. a_{21} & a_{22} & b_2\\
  2340. \end{bmatrix}
  2341. \f]
  2342. The function estimates an optimal 2D affine transformation between two 2D point sets using the
  2343. selected robust algorithm.
  2344. The computed transformation is then refined further (using only inliers) with the
  2345. Levenberg-Marquardt method to reduce the re-projection error even more.
  2346. @note
  2347. The RANSAC method can handle practically any ratio of outliers but needs a threshold to
  2348. distinguish inliers from outliers. The method LMeDS does not need any threshold but it works
  2349. correctly only when there are more than 50% of inliers.
  2350. @sa estimateAffinePartial2D, getAffineTransform
  2351. */
  2352. CV_EXPORTS_W cv::Mat estimateAffine2D(InputArray from, InputArray to, OutputArray inliers = noArray(),
  2353. int method = RANSAC, double ransacReprojThreshold = 3,
  2354. size_t maxIters = 2000, double confidence = 0.99,
  2355. size_t refineIters = 10);
  2356. /** @brief Computes an optimal limited affine transformation with 4 degrees of freedom between
  2357. two 2D point sets.
  2358. @param from First input 2D point set.
  2359. @param to Second input 2D point set.
  2360. @param inliers Output vector indicating which points are inliers.
  2361. @param method Robust method used to compute transformation. The following methods are possible:
  2362. - cv::RANSAC - RANSAC-based robust method
  2363. - cv::LMEDS - Least-Median robust method
  2364. RANSAC is the default method.
  2365. @param ransacReprojThreshold Maximum reprojection error in the RANSAC algorithm to consider
  2366. a point as an inlier. Applies only to RANSAC.
  2367. @param maxIters The maximum number of robust method iterations.
  2368. @param confidence Confidence level, between 0 and 1, for the estimated transformation. Anything
  2369. between 0.95 and 0.99 is usually good enough. Values too close to 1 can slow down the estimation
  2370. significantly. Values lower than 0.8-0.9 can result in an incorrectly estimated transformation.
  2371. @param refineIters Maximum number of iterations of refining algorithm (Levenberg-Marquardt).
  2372. Passing 0 will disable refining, so the output matrix will be output of robust method.
  2373. @return Output 2D affine transformation (4 degrees of freedom) matrix \f$2 \times 3\f$ or
  2374. empty matrix if transformation could not be estimated.
  2375. The function estimates an optimal 2D affine transformation with 4 degrees of freedom limited to
  2376. combinations of translation, rotation, and uniform scaling. Uses the selected algorithm for robust
  2377. estimation.
  2378. The computed transformation is then refined further (using only inliers) with the
  2379. Levenberg-Marquardt method to reduce the re-projection error even more.
  2380. Estimated transformation matrix is:
  2381. \f[ \begin{bmatrix} \cos(\theta) \cdot s & -\sin(\theta) \cdot s & t_x \\
  2382. \sin(\theta) \cdot s & \cos(\theta) \cdot s & t_y
  2383. \end{bmatrix} \f]
  2384. Where \f$ \theta \f$ is the rotation angle, \f$ s \f$ the scaling factor and \f$ t_x, t_y \f$ are
  2385. translations in \f$ x, y \f$ axes respectively.
  2386. @note
  2387. The RANSAC method can handle practically any ratio of outliers but need a threshold to
  2388. distinguish inliers from outliers. The method LMeDS does not need any threshold but it works
  2389. correctly only when there are more than 50% of inliers.
  2390. @sa estimateAffine2D, getAffineTransform
  2391. */
  2392. CV_EXPORTS_W cv::Mat estimateAffinePartial2D(InputArray from, InputArray to, OutputArray inliers = noArray(),
  2393. int method = RANSAC, double ransacReprojThreshold = 3,
  2394. size_t maxIters = 2000, double confidence = 0.99,
  2395. size_t refineIters = 10);
  2396. /** @example samples/cpp/tutorial_code/features2D/Homography/decompose_homography.cpp
  2397. An example program with homography decomposition.
  2398. Check @ref tutorial_homography "the corresponding tutorial" for more details.
  2399. */
  2400. /** @brief Decompose a homography matrix to rotation(s), translation(s) and plane normal(s).
  2401. @param H The input homography matrix between two images.
  2402. @param K The input camera intrinsic matrix.
  2403. @param rotations Array of rotation matrices.
  2404. @param translations Array of translation matrices.
  2405. @param normals Array of plane normal matrices.
  2406. This function extracts relative camera motion between two views of a planar object and returns up to
  2407. four mathematical solution tuples of rotation, translation, and plane normal. The decomposition of
  2408. the homography matrix H is described in detail in @cite Malis.
  2409. If the homography H, induced by the plane, gives the constraint
  2410. \f[s_i \vecthree{x'_i}{y'_i}{1} \sim H \vecthree{x_i}{y_i}{1}\f] on the source image points
  2411. \f$p_i\f$ and the destination image points \f$p'_i\f$, then the tuple of rotations[k] and
  2412. translations[k] is a change of basis from the source camera's coordinate system to the destination
  2413. camera's coordinate system. However, by decomposing H, one can only get the translation normalized
  2414. by the (typically unknown) depth of the scene, i.e. its direction but with normalized length.
  2415. If point correspondences are available, at least two solutions may further be invalidated, by
  2416. applying positive depth constraint, i.e. all points must be in front of the camera.
  2417. */
  2418. CV_EXPORTS_W int decomposeHomographyMat(InputArray H,
  2419. InputArray K,
  2420. OutputArrayOfArrays rotations,
  2421. OutputArrayOfArrays translations,
  2422. OutputArrayOfArrays normals);
  2423. /** @brief Filters homography decompositions based on additional information.
  2424. @param rotations Vector of rotation matrices.
  2425. @param normals Vector of plane normal matrices.
  2426. @param beforePoints Vector of (rectified) visible reference points before the homography is applied
  2427. @param afterPoints Vector of (rectified) visible reference points after the homography is applied
  2428. @param possibleSolutions Vector of int indices representing the viable solution set after filtering
  2429. @param pointsMask optional Mat/Vector of 8u type representing the mask for the inliers as given by the findHomography function
  2430. This function is intended to filter the output of the decomposeHomographyMat based on additional
  2431. information as described in @cite Malis . The summary of the method: the decomposeHomographyMat function
  2432. returns 2 unique solutions and their "opposites" for a total of 4 solutions. If we have access to the
  2433. sets of points visible in the camera frame before and after the homography transformation is applied,
  2434. we can determine which are the true potential solutions and which are the opposites by verifying which
  2435. homographies are consistent with all visible reference points being in front of the camera. The inputs
  2436. are left unchanged; the filtered solution set is returned as indices into the existing one.
  2437. */
  2438. CV_EXPORTS_W void filterHomographyDecompByVisibleRefpoints(InputArrayOfArrays rotations,
  2439. InputArrayOfArrays normals,
  2440. InputArray beforePoints,
  2441. InputArray afterPoints,
  2442. OutputArray possibleSolutions,
  2443. InputArray pointsMask = noArray());
  2444. /** @brief The base class for stereo correspondence algorithms.
  2445. */
  2446. class CV_EXPORTS_W StereoMatcher : public Algorithm
  2447. {
  2448. public:
  2449. enum { DISP_SHIFT = 4,
  2450. DISP_SCALE = (1 << DISP_SHIFT)
  2451. };
  2452. /** @brief Computes disparity map for the specified stereo pair
  2453. @param left Left 8-bit single-channel image.
  2454. @param right Right image of the same size and the same type as the left one.
  2455. @param disparity Output disparity map. It has the same size as the input images. Some algorithms,
  2456. like StereoBM or StereoSGBM compute 16-bit fixed-point disparity map (where each disparity value
  2457. has 4 fractional bits), whereas other algorithms output 32-bit floating-point disparity map.
  2458. */
  2459. CV_WRAP virtual void compute( InputArray left, InputArray right,
  2460. OutputArray disparity ) = 0;
  2461. CV_WRAP virtual int getMinDisparity() const = 0;
  2462. CV_WRAP virtual void setMinDisparity(int minDisparity) = 0;
  2463. CV_WRAP virtual int getNumDisparities() const = 0;
  2464. CV_WRAP virtual void setNumDisparities(int numDisparities) = 0;
  2465. CV_WRAP virtual int getBlockSize() const = 0;
  2466. CV_WRAP virtual void setBlockSize(int blockSize) = 0;
  2467. CV_WRAP virtual int getSpeckleWindowSize() const = 0;
  2468. CV_WRAP virtual void setSpeckleWindowSize(int speckleWindowSize) = 0;
  2469. CV_WRAP virtual int getSpeckleRange() const = 0;
  2470. CV_WRAP virtual void setSpeckleRange(int speckleRange) = 0;
  2471. CV_WRAP virtual int getDisp12MaxDiff() const = 0;
  2472. CV_WRAP virtual void setDisp12MaxDiff(int disp12MaxDiff) = 0;
  2473. };
  2474. /** @brief Class for computing stereo correspondence using the block matching algorithm, introduced and
  2475. contributed to OpenCV by K. Konolige.
  2476. */
  2477. class CV_EXPORTS_W StereoBM : public StereoMatcher
  2478. {
  2479. public:
  2480. enum { PREFILTER_NORMALIZED_RESPONSE = 0,
  2481. PREFILTER_XSOBEL = 1
  2482. };
  2483. CV_WRAP virtual int getPreFilterType() const = 0;
  2484. CV_WRAP virtual void setPreFilterType(int preFilterType) = 0;
  2485. CV_WRAP virtual int getPreFilterSize() const = 0;
  2486. CV_WRAP virtual void setPreFilterSize(int preFilterSize) = 0;
  2487. CV_WRAP virtual int getPreFilterCap() const = 0;
  2488. CV_WRAP virtual void setPreFilterCap(int preFilterCap) = 0;
  2489. CV_WRAP virtual int getTextureThreshold() const = 0;
  2490. CV_WRAP virtual void setTextureThreshold(int textureThreshold) = 0;
  2491. CV_WRAP virtual int getUniquenessRatio() const = 0;
  2492. CV_WRAP virtual void setUniquenessRatio(int uniquenessRatio) = 0;
  2493. CV_WRAP virtual int getSmallerBlockSize() const = 0;
  2494. CV_WRAP virtual void setSmallerBlockSize(int blockSize) = 0;
  2495. CV_WRAP virtual Rect getROI1() const = 0;
  2496. CV_WRAP virtual void setROI1(Rect roi1) = 0;
  2497. CV_WRAP virtual Rect getROI2() const = 0;
  2498. CV_WRAP virtual void setROI2(Rect roi2) = 0;
  2499. /** @brief Creates StereoBM object
  2500. @param numDisparities the disparity search range. For each pixel algorithm will find the best
  2501. disparity from 0 (default minimum disparity) to numDisparities. The search range can then be
  2502. shifted by changing the minimum disparity.
  2503. @param blockSize the linear size of the blocks compared by the algorithm. The size should be odd
  2504. (as the block is centered at the current pixel). Larger block size implies smoother, though less
  2505. accurate disparity map. Smaller block size gives more detailed disparity map, but there is higher
  2506. chance for algorithm to find a wrong correspondence.
  2507. The function create StereoBM object. You can then call StereoBM::compute() to compute disparity for
  2508. a specific stereo pair.
  2509. */
  2510. CV_WRAP static Ptr<StereoBM> create(int numDisparities = 0, int blockSize = 21);
  2511. };
  2512. /** @brief The class implements the modified H. Hirschmuller algorithm @cite HH08 that differs from the original
  2513. one as follows:
  2514. - By default, the algorithm is single-pass, which means that you consider only 5 directions
  2515. instead of 8. Set mode=StereoSGBM::MODE_HH in createStereoSGBM to run the full variant of the
  2516. algorithm but beware that it may consume a lot of memory.
  2517. - The algorithm matches blocks, not individual pixels. Though, setting blockSize=1 reduces the
  2518. blocks to single pixels.
  2519. - Mutual information cost function is not implemented. Instead, a simpler Birchfield-Tomasi
  2520. sub-pixel metric from @cite BT98 is used. Though, the color images are supported as well.
  2521. - Some pre- and post- processing steps from K. Konolige algorithm StereoBM are included, for
  2522. example: pre-filtering (StereoBM::PREFILTER_XSOBEL type) and post-filtering (uniqueness
  2523. check, quadratic interpolation and speckle filtering).
  2524. @note
  2525. - (Python) An example illustrating the use of the StereoSGBM matching algorithm can be found
  2526. at opencv_source_code/samples/python/stereo_match.py
  2527. */
  2528. class CV_EXPORTS_W StereoSGBM : public StereoMatcher
  2529. {
  2530. public:
  2531. enum
  2532. {
  2533. MODE_SGBM = 0,
  2534. MODE_HH = 1,
  2535. MODE_SGBM_3WAY = 2,
  2536. MODE_HH4 = 3
  2537. };
  2538. CV_WRAP virtual int getPreFilterCap() const = 0;
  2539. CV_WRAP virtual void setPreFilterCap(int preFilterCap) = 0;
  2540. CV_WRAP virtual int getUniquenessRatio() const = 0;
  2541. CV_WRAP virtual void setUniquenessRatio(int uniquenessRatio) = 0;
  2542. CV_WRAP virtual int getP1() const = 0;
  2543. CV_WRAP virtual void setP1(int P1) = 0;
  2544. CV_WRAP virtual int getP2() const = 0;
  2545. CV_WRAP virtual void setP2(int P2) = 0;
  2546. CV_WRAP virtual int getMode() const = 0;
  2547. CV_WRAP virtual void setMode(int mode) = 0;
  2548. /** @brief Creates StereoSGBM object
  2549. @param minDisparity Minimum possible disparity value. Normally, it is zero but sometimes
  2550. rectification algorithms can shift images, so this parameter needs to be adjusted accordingly.
  2551. @param numDisparities Maximum disparity minus minimum disparity. The value is always greater than
  2552. zero. In the current implementation, this parameter must be divisible by 16.
  2553. @param blockSize Matched block size. It must be an odd number \>=1 . Normally, it should be
  2554. somewhere in the 3..11 range.
  2555. @param P1 The first parameter controlling the disparity smoothness. See below.
  2556. @param P2 The second parameter controlling the disparity smoothness. The larger the values are,
  2557. the smoother the disparity is. P1 is the penalty on the disparity change by plus or minus 1
  2558. between neighbor pixels. P2 is the penalty on the disparity change by more than 1 between neighbor
  2559. pixels. The algorithm requires P2 \> P1 . See stereo_match.cpp sample where some reasonably good
  2560. P1 and P2 values are shown (like 8\*number_of_image_channels\*blockSize\*blockSize and
  2561. 32\*number_of_image_channels\*blockSize\*blockSize , respectively).
  2562. @param disp12MaxDiff Maximum allowed difference (in integer pixel units) in the left-right
  2563. disparity check. Set it to a non-positive value to disable the check.
  2564. @param preFilterCap Truncation value for the prefiltered image pixels. The algorithm first
  2565. computes x-derivative at each pixel and clips its value by [-preFilterCap, preFilterCap] interval.
  2566. The result values are passed to the Birchfield-Tomasi pixel cost function.
  2567. @param uniquenessRatio Margin in percentage by which the best (minimum) computed cost function
  2568. value should "win" the second best value to consider the found match correct. Normally, a value
  2569. within the 5-15 range is good enough.
  2570. @param speckleWindowSize Maximum size of smooth disparity regions to consider their noise speckles
  2571. and invalidate. Set it to 0 to disable speckle filtering. Otherwise, set it somewhere in the
  2572. 50-200 range.
  2573. @param speckleRange Maximum disparity variation within each connected component. If you do speckle
  2574. filtering, set the parameter to a positive value, it will be implicitly multiplied by 16.
  2575. Normally, 1 or 2 is good enough.
  2576. @param mode Set it to StereoSGBM::MODE_HH to run the full-scale two-pass dynamic programming
  2577. algorithm. It will consume O(W\*H\*numDisparities) bytes, which is large for 640x480 stereo and
  2578. huge for HD-size pictures. By default, it is set to false .
  2579. The first constructor initializes StereoSGBM with all the default parameters. So, you only have to
  2580. set StereoSGBM::numDisparities at minimum. The second constructor enables you to set each parameter
  2581. to a custom value.
  2582. */
  2583. CV_WRAP static Ptr<StereoSGBM> create(int minDisparity = 0, int numDisparities = 16, int blockSize = 3,
  2584. int P1 = 0, int P2 = 0, int disp12MaxDiff = 0,
  2585. int preFilterCap = 0, int uniquenessRatio = 0,
  2586. int speckleWindowSize = 0, int speckleRange = 0,
  2587. int mode = StereoSGBM::MODE_SGBM);
  2588. };
  2589. //! @} calib3d
  2590. /** @brief The methods in this namespace use a so-called fisheye camera model.
  2591. @ingroup calib3d_fisheye
  2592. */
  2593. namespace fisheye
  2594. {
  2595. //! @addtogroup calib3d_fisheye
  2596. //! @{
  2597. enum{
  2598. CALIB_USE_INTRINSIC_GUESS = 1 << 0,
  2599. CALIB_RECOMPUTE_EXTRINSIC = 1 << 1,
  2600. CALIB_CHECK_COND = 1 << 2,
  2601. CALIB_FIX_SKEW = 1 << 3,
  2602. CALIB_FIX_K1 = 1 << 4,
  2603. CALIB_FIX_K2 = 1 << 5,
  2604. CALIB_FIX_K3 = 1 << 6,
  2605. CALIB_FIX_K4 = 1 << 7,
  2606. CALIB_FIX_INTRINSIC = 1 << 8,
  2607. CALIB_FIX_PRINCIPAL_POINT = 1 << 9
  2608. };
  2609. /** @brief Projects points using fisheye model
  2610. @param objectPoints Array of object points, 1xN/Nx1 3-channel (or vector\<Point3f\> ), where N is
  2611. the number of points in the view.
  2612. @param imagePoints Output array of image points, 2xN/Nx2 1-channel or 1xN/Nx1 2-channel, or
  2613. vector\<Point2f\>.
  2614. @param affine
  2615. @param K Camera intrinsic matrix \f$cameramatrix{K}\f$.
  2616. @param D Input vector of distortion coefficients \f$\distcoeffsfisheye\f$.
  2617. @param alpha The skew coefficient.
  2618. @param jacobian Optional output 2Nx15 jacobian matrix of derivatives of image points with respect
  2619. to components of the focal lengths, coordinates of the principal point, distortion coefficients,
  2620. rotation vector, translation vector, and the skew. In the old interface different components of
  2621. the jacobian are returned via different output parameters.
  2622. The function computes projections of 3D points to the image plane given intrinsic and extrinsic
  2623. camera parameters. Optionally, the function computes Jacobians - matrices of partial derivatives of
  2624. image points coordinates (as functions of all the input parameters) with respect to the particular
  2625. parameters, intrinsic and/or extrinsic.
  2626. */
  2627. CV_EXPORTS void projectPoints(InputArray objectPoints, OutputArray imagePoints, const Affine3d& affine,
  2628. InputArray K, InputArray D, double alpha = 0, OutputArray jacobian = noArray());
  2629. /** @overload */
  2630. CV_EXPORTS_W void projectPoints(InputArray objectPoints, OutputArray imagePoints, InputArray rvec, InputArray tvec,
  2631. InputArray K, InputArray D, double alpha = 0, OutputArray jacobian = noArray());
  2632. /** @brief Distorts 2D points using fisheye model.
  2633. @param undistorted Array of object points, 1xN/Nx1 2-channel (or vector\<Point2f\> ), where N is
  2634. the number of points in the view.
  2635. @param K Camera intrinsic matrix \f$cameramatrix{K}\f$.
  2636. @param D Input vector of distortion coefficients \f$\distcoeffsfisheye\f$.
  2637. @param alpha The skew coefficient.
  2638. @param distorted Output array of image points, 1xN/Nx1 2-channel, or vector\<Point2f\> .
  2639. Note that the function assumes the camera intrinsic matrix of the undistorted points to be identity.
  2640. This means if you want to transform back points undistorted with undistortPoints() you have to
  2641. multiply them with \f$P^{-1}\f$.
  2642. */
  2643. CV_EXPORTS_W void distortPoints(InputArray undistorted, OutputArray distorted, InputArray K, InputArray D, double alpha = 0);
  2644. /** @brief Undistorts 2D points using fisheye model
  2645. @param distorted Array of object points, 1xN/Nx1 2-channel (or vector\<Point2f\> ), where N is the
  2646. number of points in the view.
  2647. @param K Camera intrinsic matrix \f$cameramatrix{K}\f$.
  2648. @param D Input vector of distortion coefficients \f$\distcoeffsfisheye\f$.
  2649. @param R Rectification transformation in the object space: 3x3 1-channel, or vector: 3x1/1x3
  2650. 1-channel or 1x1 3-channel
  2651. @param P New camera intrinsic matrix (3x3) or new projection matrix (3x4)
  2652. @param undistorted Output array of image points, 1xN/Nx1 2-channel, or vector\<Point2f\> .
  2653. */
  2654. CV_EXPORTS_W void undistortPoints(InputArray distorted, OutputArray undistorted,
  2655. InputArray K, InputArray D, InputArray R = noArray(), InputArray P = noArray());
  2656. /** @brief Computes undistortion and rectification maps for image transform by cv::remap(). If D is empty zero
  2657. distortion is used, if R or P is empty identity matrixes are used.
  2658. @param K Camera intrinsic matrix \f$cameramatrix{K}\f$.
  2659. @param D Input vector of distortion coefficients \f$\distcoeffsfisheye\f$.
  2660. @param R Rectification transformation in the object space: 3x3 1-channel, or vector: 3x1/1x3
  2661. 1-channel or 1x1 3-channel
  2662. @param P New camera intrinsic matrix (3x3) or new projection matrix (3x4)
  2663. @param size Undistorted image size.
  2664. @param m1type Type of the first output map that can be CV_32FC1 or CV_16SC2 . See convertMaps()
  2665. for details.
  2666. @param map1 The first output map.
  2667. @param map2 The second output map.
  2668. */
  2669. CV_EXPORTS_W void initUndistortRectifyMap(InputArray K, InputArray D, InputArray R, InputArray P,
  2670. const cv::Size& size, int m1type, OutputArray map1, OutputArray map2);
  2671. /** @brief Transforms an image to compensate for fisheye lens distortion.
  2672. @param distorted image with fisheye lens distortion.
  2673. @param undistorted Output image with compensated fisheye lens distortion.
  2674. @param K Camera intrinsic matrix \f$cameramatrix{K}\f$.
  2675. @param D Input vector of distortion coefficients \f$\distcoeffsfisheye\f$.
  2676. @param Knew Camera intrinsic matrix of the distorted image. By default, it is the identity matrix but you
  2677. may additionally scale and shift the result by using a different matrix.
  2678. @param new_size the new size
  2679. The function transforms an image to compensate radial and tangential lens distortion.
  2680. The function is simply a combination of fisheye::initUndistortRectifyMap (with unity R ) and remap
  2681. (with bilinear interpolation). See the former function for details of the transformation being
  2682. performed.
  2683. See below the results of undistortImage.
  2684. - a\) result of undistort of perspective camera model (all possible coefficients (k_1, k_2, k_3,
  2685. k_4, k_5, k_6) of distortion were optimized under calibration)
  2686. - b\) result of fisheye::undistortImage of fisheye camera model (all possible coefficients (k_1, k_2,
  2687. k_3, k_4) of fisheye distortion were optimized under calibration)
  2688. - c\) original image was captured with fisheye lens
  2689. Pictures a) and b) almost the same. But if we consider points of image located far from the center
  2690. of image, we can notice that on image a) these points are distorted.
  2691. ![image](pics/fisheye_undistorted.jpg)
  2692. */
  2693. CV_EXPORTS_W void undistortImage(InputArray distorted, OutputArray undistorted,
  2694. InputArray K, InputArray D, InputArray Knew = cv::noArray(), const Size& new_size = Size());
  2695. /** @brief Estimates new camera intrinsic matrix for undistortion or rectification.
  2696. @param K Camera intrinsic matrix \f$cameramatrix{K}\f$.
  2697. @param image_size Size of the image
  2698. @param D Input vector of distortion coefficients \f$\distcoeffsfisheye\f$.
  2699. @param R Rectification transformation in the object space: 3x3 1-channel, or vector: 3x1/1x3
  2700. 1-channel or 1x1 3-channel
  2701. @param P New camera intrinsic matrix (3x3) or new projection matrix (3x4)
  2702. @param balance Sets the new focal length in range between the min focal length and the max focal
  2703. length. Balance is in range of [0, 1].
  2704. @param new_size the new size
  2705. @param fov_scale Divisor for new focal length.
  2706. */
  2707. CV_EXPORTS_W void estimateNewCameraMatrixForUndistortRectify(InputArray K, InputArray D, const Size &image_size, InputArray R,
  2708. OutputArray P, double balance = 0.0, const Size& new_size = Size(), double fov_scale = 1.0);
  2709. /** @brief Performs camera calibaration
  2710. @param objectPoints vector of vectors of calibration pattern points in the calibration pattern
  2711. coordinate space.
  2712. @param imagePoints vector of vectors of the projections of calibration pattern points.
  2713. imagePoints.size() and objectPoints.size() and imagePoints[i].size() must be equal to
  2714. objectPoints[i].size() for each i.
  2715. @param image_size Size of the image used only to initialize the camera intrinsic matrix.
  2716. @param K Output 3x3 floating-point camera intrinsic matrix
  2717. \f$\cameramatrix{A}\f$ . If
  2718. fisheye::CALIB_USE_INTRINSIC_GUESS/ is specified, some or all of fx, fy, cx, cy must be
  2719. initialized before calling the function.
  2720. @param D Output vector of distortion coefficients \f$\distcoeffsfisheye\f$.
  2721. @param rvecs Output vector of rotation vectors (see Rodrigues ) estimated for each pattern view.
  2722. That is, each k-th rotation vector together with the corresponding k-th translation vector (see
  2723. the next output parameter description) brings the calibration pattern from the model coordinate
  2724. space (in which object points are specified) to the world coordinate space, that is, a real
  2725. position of the calibration pattern in the k-th pattern view (k=0.. *M* -1).
  2726. @param tvecs Output vector of translation vectors estimated for each pattern view.
  2727. @param flags Different flags that may be zero or a combination of the following values:
  2728. - **fisheye::CALIB_USE_INTRINSIC_GUESS** cameraMatrix contains valid initial values of
  2729. fx, fy, cx, cy that are optimized further. Otherwise, (cx, cy) is initially set to the image
  2730. center ( imageSize is used), and focal distances are computed in a least-squares fashion.
  2731. - **fisheye::CALIB_RECOMPUTE_EXTRINSIC** Extrinsic will be recomputed after each iteration
  2732. of intrinsic optimization.
  2733. - **fisheye::CALIB_CHECK_COND** The functions will check validity of condition number.
  2734. - **fisheye::CALIB_FIX_SKEW** Skew coefficient (alpha) is set to zero and stay zero.
  2735. - **fisheye::CALIB_FIX_K1..fisheye::CALIB_FIX_K4** Selected distortion coefficients
  2736. are set to zeros and stay zero.
  2737. - **fisheye::CALIB_FIX_PRINCIPAL_POINT** The principal point is not changed during the global
  2738. optimization. It stays at the center or at a different location specified when CALIB_USE_INTRINSIC_GUESS is set too.
  2739. @param criteria Termination criteria for the iterative optimization algorithm.
  2740. */
  2741. CV_EXPORTS_W double calibrate(InputArrayOfArrays objectPoints, InputArrayOfArrays imagePoints, const Size& image_size,
  2742. InputOutputArray K, InputOutputArray D, OutputArrayOfArrays rvecs, OutputArrayOfArrays tvecs, int flags = 0,
  2743. TermCriteria criteria = TermCriteria(TermCriteria::COUNT + TermCriteria::EPS, 100, DBL_EPSILON));
  2744. /** @brief Stereo rectification for fisheye camera model
  2745. @param K1 First camera intrinsic matrix.
  2746. @param D1 First camera distortion parameters.
  2747. @param K2 Second camera intrinsic matrix.
  2748. @param D2 Second camera distortion parameters.
  2749. @param imageSize Size of the image used for stereo calibration.
  2750. @param R Rotation matrix between the coordinate systems of the first and the second
  2751. cameras.
  2752. @param tvec Translation vector between coordinate systems of the cameras.
  2753. @param R1 Output 3x3 rectification transform (rotation matrix) for the first camera.
  2754. @param R2 Output 3x3 rectification transform (rotation matrix) for the second camera.
  2755. @param P1 Output 3x4 projection matrix in the new (rectified) coordinate systems for the first
  2756. camera.
  2757. @param P2 Output 3x4 projection matrix in the new (rectified) coordinate systems for the second
  2758. camera.
  2759. @param Q Output \f$4 \times 4\f$ disparity-to-depth mapping matrix (see reprojectImageTo3D ).
  2760. @param flags Operation flags that may be zero or CALIB_ZERO_DISPARITY . If the flag is set,
  2761. the function makes the principal points of each camera have the same pixel coordinates in the
  2762. rectified views. And if the flag is not set, the function may still shift the images in the
  2763. horizontal or vertical direction (depending on the orientation of epipolar lines) to maximize the
  2764. useful image area.
  2765. @param newImageSize New image resolution after rectification. The same size should be passed to
  2766. initUndistortRectifyMap (see the stereo_calib.cpp sample in OpenCV samples directory). When (0,0)
  2767. is passed (default), it is set to the original imageSize . Setting it to larger value can help you
  2768. preserve details in the original image, especially when there is a big radial distortion.
  2769. @param balance Sets the new focal length in range between the min focal length and the max focal
  2770. length. Balance is in range of [0, 1].
  2771. @param fov_scale Divisor for new focal length.
  2772. */
  2773. CV_EXPORTS_W void stereoRectify(InputArray K1, InputArray D1, InputArray K2, InputArray D2, const Size &imageSize, InputArray R, InputArray tvec,
  2774. OutputArray R1, OutputArray R2, OutputArray P1, OutputArray P2, OutputArray Q, int flags, const Size &newImageSize = Size(),
  2775. double balance = 0.0, double fov_scale = 1.0);
  2776. /** @brief Performs stereo calibration
  2777. @param objectPoints Vector of vectors of the calibration pattern points.
  2778. @param imagePoints1 Vector of vectors of the projections of the calibration pattern points,
  2779. observed by the first camera.
  2780. @param imagePoints2 Vector of vectors of the projections of the calibration pattern points,
  2781. observed by the second camera.
  2782. @param K1 Input/output first camera intrinsic matrix:
  2783. \f$\vecthreethree{f_x^{(j)}}{0}{c_x^{(j)}}{0}{f_y^{(j)}}{c_y^{(j)}}{0}{0}{1}\f$ , \f$j = 0,\, 1\f$ . If
  2784. any of fisheye::CALIB_USE_INTRINSIC_GUESS , fisheye::CALIB_FIX_INTRINSIC are specified,
  2785. some or all of the matrix components must be initialized.
  2786. @param D1 Input/output vector of distortion coefficients \f$\distcoeffsfisheye\f$ of 4 elements.
  2787. @param K2 Input/output second camera intrinsic matrix. The parameter is similar to K1 .
  2788. @param D2 Input/output lens distortion coefficients for the second camera. The parameter is
  2789. similar to D1 .
  2790. @param imageSize Size of the image used only to initialize camera intrinsic matrix.
  2791. @param R Output rotation matrix between the 1st and the 2nd camera coordinate systems.
  2792. @param T Output translation vector between the coordinate systems of the cameras.
  2793. @param flags Different flags that may be zero or a combination of the following values:
  2794. - **fisheye::CALIB_FIX_INTRINSIC** Fix K1, K2? and D1, D2? so that only R, T matrices
  2795. are estimated.
  2796. - **fisheye::CALIB_USE_INTRINSIC_GUESS** K1, K2 contains valid initial values of
  2797. fx, fy, cx, cy that are optimized further. Otherwise, (cx, cy) is initially set to the image
  2798. center (imageSize is used), and focal distances are computed in a least-squares fashion.
  2799. - **fisheye::CALIB_RECOMPUTE_EXTRINSIC** Extrinsic will be recomputed after each iteration
  2800. of intrinsic optimization.
  2801. - **fisheye::CALIB_CHECK_COND** The functions will check validity of condition number.
  2802. - **fisheye::CALIB_FIX_SKEW** Skew coefficient (alpha) is set to zero and stay zero.
  2803. - **fisheye::CALIB_FIX_K1..4** Selected distortion coefficients are set to zeros and stay
  2804. zero.
  2805. @param criteria Termination criteria for the iterative optimization algorithm.
  2806. */
  2807. CV_EXPORTS_W double stereoCalibrate(InputArrayOfArrays objectPoints, InputArrayOfArrays imagePoints1, InputArrayOfArrays imagePoints2,
  2808. InputOutputArray K1, InputOutputArray D1, InputOutputArray K2, InputOutputArray D2, Size imageSize,
  2809. OutputArray R, OutputArray T, int flags = fisheye::CALIB_FIX_INTRINSIC,
  2810. TermCriteria criteria = TermCriteria(TermCriteria::COUNT + TermCriteria::EPS, 100, DBL_EPSILON));
  2811. //! @} calib3d_fisheye
  2812. } // end namespace fisheye
  2813. } //end namespace cv
  2814. #ifndef DISABLE_OPENCV_24_COMPATIBILITY
  2815. #include "opencv2/calib3d/calib3d_c.h"
  2816. #endif
  2817. #endif