mat.inl.hpp 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973
  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. // Copyright (C) 2015, Itseez Inc., all rights reserved.
  17. // Third party copyrights are property of their respective owners.
  18. //
  19. // Redistribution and use in source and binary forms, with or without modification,
  20. // are permitted provided that the following conditions are met:
  21. //
  22. // * Redistribution's of source code must retain the above copyright notice,
  23. // this list of conditions and the following disclaimer.
  24. //
  25. // * Redistribution's in binary form must reproduce the above copyright notice,
  26. // this list of conditions and the following disclaimer in the documentation
  27. // and/or other materials provided with the distribution.
  28. //
  29. // * The name of the copyright holders may not be used to endorse or promote products
  30. // derived from this software without specific prior written permission.
  31. //
  32. // This software is provided by the copyright holders and contributors "as is" and
  33. // any express or implied warranties, including, but not limited to, the implied
  34. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  35. // In no event shall the Intel Corporation or contributors be liable for any direct,
  36. // indirect, incidental, special, exemplary, or consequential damages
  37. // (including, but not limited to, procurement of substitute goods or services;
  38. // loss of use, data, or profits; or business interruption) however caused
  39. // and on any theory of liability, whether in contract, strict liability,
  40. // or tort (including negligence or otherwise) arising in any way out of
  41. // the use of this software, even if advised of the possibility of such damage.
  42. //
  43. //M*/
  44. #ifndef OPENCV_CORE_MATRIX_OPERATIONS_HPP
  45. #define OPENCV_CORE_MATRIX_OPERATIONS_HPP
  46. #ifndef __cplusplus
  47. # error mat.inl.hpp header must be compiled as C++
  48. #endif
  49. #ifdef _MSC_VER
  50. #pragma warning( push )
  51. #pragma warning( disable: 4127 )
  52. #endif
  53. namespace cv
  54. {
  55. CV__DEBUG_NS_BEGIN
  56. //! @cond IGNORED
  57. ////////////////////////// Custom (raw) type wrapper //////////////////////////
  58. template<typename _Tp> static inline
  59. int rawType()
  60. {
  61. CV_StaticAssert(sizeof(_Tp) <= CV_CN_MAX, "sizeof(_Tp) is too large");
  62. const int elemSize = sizeof(_Tp);
  63. return (int)CV_MAKETYPE(CV_8U, elemSize);
  64. }
  65. //////////////////////// Input/Output Arrays ////////////////////////
  66. inline void _InputArray::init(int _flags, const void* _obj)
  67. { flags = _flags; obj = (void*)_obj; }
  68. inline void _InputArray::init(int _flags, const void* _obj, Size _sz)
  69. { flags = _flags; obj = (void*)_obj; sz = _sz; }
  70. inline void* _InputArray::getObj() const { return obj; }
  71. inline int _InputArray::getFlags() const { return flags; }
  72. inline Size _InputArray::getSz() const { return sz; }
  73. inline _InputArray::_InputArray() { init(0 + NONE, 0); }
  74. inline _InputArray::_InputArray(int _flags, void* _obj) { init(_flags, _obj); }
  75. inline _InputArray::_InputArray(const Mat& m) { init(MAT+ACCESS_READ, &m); }
  76. inline _InputArray::_InputArray(const std::vector<Mat>& vec) { init(STD_VECTOR_MAT+ACCESS_READ, &vec); }
  77. inline _InputArray::_InputArray(const UMat& m) { init(UMAT+ACCESS_READ, &m); }
  78. inline _InputArray::_InputArray(const std::vector<UMat>& vec) { init(STD_VECTOR_UMAT+ACCESS_READ, &vec); }
  79. template<typename _Tp> inline
  80. _InputArray::_InputArray(const std::vector<_Tp>& vec)
  81. { init(FIXED_TYPE + STD_VECTOR + traits::Type<_Tp>::value + ACCESS_READ, &vec); }
  82. template<typename _Tp, std::size_t _Nm> inline
  83. _InputArray::_InputArray(const std::array<_Tp, _Nm>& arr)
  84. { init(FIXED_TYPE + FIXED_SIZE + STD_ARRAY + traits::Type<_Tp>::value + ACCESS_READ, arr.data(), Size(1, _Nm)); }
  85. template<std::size_t _Nm> inline
  86. _InputArray::_InputArray(const std::array<Mat, _Nm>& arr)
  87. { init(STD_ARRAY_MAT + ACCESS_READ, arr.data(), Size(1, _Nm)); }
  88. inline
  89. _InputArray::_InputArray(const std::vector<bool>& vec)
  90. { init(FIXED_TYPE + STD_BOOL_VECTOR + traits::Type<bool>::value + ACCESS_READ, &vec); }
  91. template<typename _Tp> inline
  92. _InputArray::_InputArray(const std::vector<std::vector<_Tp> >& vec)
  93. { init(FIXED_TYPE + STD_VECTOR_VECTOR + traits::Type<_Tp>::value + ACCESS_READ, &vec); }
  94. template<typename _Tp> inline
  95. _InputArray::_InputArray(const std::vector<Mat_<_Tp> >& vec)
  96. { init(FIXED_TYPE + STD_VECTOR_MAT + traits::Type<_Tp>::value + ACCESS_READ, &vec); }
  97. template<typename _Tp, int m, int n> inline
  98. _InputArray::_InputArray(const Matx<_Tp, m, n>& mtx)
  99. { init(FIXED_TYPE + FIXED_SIZE + MATX + traits::Type<_Tp>::value + ACCESS_READ, &mtx, Size(n, m)); }
  100. template<typename _Tp> inline
  101. _InputArray::_InputArray(const _Tp* vec, int n)
  102. { init(FIXED_TYPE + FIXED_SIZE + MATX + traits::Type<_Tp>::value + ACCESS_READ, vec, Size(n, 1)); }
  103. template<typename _Tp> inline
  104. _InputArray::_InputArray(const Mat_<_Tp>& m)
  105. { init(FIXED_TYPE + MAT + traits::Type<_Tp>::value + ACCESS_READ, &m); }
  106. inline _InputArray::_InputArray(const double& val)
  107. { init(FIXED_TYPE + FIXED_SIZE + MATX + CV_64F + ACCESS_READ, &val, Size(1,1)); }
  108. inline _InputArray::_InputArray(const MatExpr& expr)
  109. { init(FIXED_TYPE + FIXED_SIZE + EXPR + ACCESS_READ, &expr); }
  110. inline _InputArray::_InputArray(const cuda::GpuMat& d_mat)
  111. { init(CUDA_GPU_MAT + ACCESS_READ, &d_mat); }
  112. inline _InputArray::_InputArray(const std::vector<cuda::GpuMat>& d_mat)
  113. { init(STD_VECTOR_CUDA_GPU_MAT + ACCESS_READ, &d_mat);}
  114. inline _InputArray::_InputArray(const ogl::Buffer& buf)
  115. { init(OPENGL_BUFFER + ACCESS_READ, &buf); }
  116. inline _InputArray::_InputArray(const cuda::HostMem& cuda_mem)
  117. { init(CUDA_HOST_MEM + ACCESS_READ, &cuda_mem); }
  118. template<typename _Tp> inline
  119. _InputArray _InputArray::rawIn(const std::vector<_Tp>& vec)
  120. {
  121. _InputArray v;
  122. v.flags = _InputArray::FIXED_TYPE + _InputArray::STD_VECTOR + rawType<_Tp>() + ACCESS_READ;
  123. v.obj = (void*)&vec;
  124. return v;
  125. }
  126. template<typename _Tp, std::size_t _Nm> inline
  127. _InputArray _InputArray::rawIn(const std::array<_Tp, _Nm>& arr)
  128. {
  129. _InputArray v;
  130. v.flags = FIXED_TYPE + FIXED_SIZE + STD_ARRAY + traits::Type<_Tp>::value + ACCESS_READ;
  131. v.obj = (void*)arr.data();
  132. v.sz = Size(1, _Nm);
  133. return v;
  134. }
  135. inline _InputArray::~_InputArray() {}
  136. inline Mat _InputArray::getMat(int i) const
  137. {
  138. if( kind() == MAT && i < 0 )
  139. return *(const Mat*)obj;
  140. return getMat_(i);
  141. }
  142. inline bool _InputArray::isMat() const { return kind() == _InputArray::MAT; }
  143. inline bool _InputArray::isUMat() const { return kind() == _InputArray::UMAT; }
  144. inline bool _InputArray::isMatVector() const { return kind() == _InputArray::STD_VECTOR_MAT; }
  145. inline bool _InputArray::isUMatVector() const { return kind() == _InputArray::STD_VECTOR_UMAT; }
  146. inline bool _InputArray::isMatx() const { return kind() == _InputArray::MATX; }
  147. inline bool _InputArray::isVector() const { return kind() == _InputArray::STD_VECTOR ||
  148. kind() == _InputArray::STD_BOOL_VECTOR ||
  149. kind() == _InputArray::STD_ARRAY; }
  150. inline bool _InputArray::isGpuMat() const { return kind() == _InputArray::CUDA_GPU_MAT; }
  151. inline bool _InputArray::isGpuMatVector() const { return kind() == _InputArray::STD_VECTOR_CUDA_GPU_MAT; }
  152. ////////////////////////////////////////////////////////////////////////////////////////
  153. inline _OutputArray::_OutputArray() { init(NONE + ACCESS_WRITE, 0); }
  154. inline _OutputArray::_OutputArray(int _flags, void* _obj) { init(_flags + ACCESS_WRITE, _obj); }
  155. inline _OutputArray::_OutputArray(Mat& m) { init(MAT+ACCESS_WRITE, &m); }
  156. inline _OutputArray::_OutputArray(std::vector<Mat>& vec) { init(STD_VECTOR_MAT + ACCESS_WRITE, &vec); }
  157. inline _OutputArray::_OutputArray(UMat& m) { init(UMAT + ACCESS_WRITE, &m); }
  158. inline _OutputArray::_OutputArray(std::vector<UMat>& vec) { init(STD_VECTOR_UMAT + ACCESS_WRITE, &vec); }
  159. template<typename _Tp> inline
  160. _OutputArray::_OutputArray(std::vector<_Tp>& vec)
  161. { init(FIXED_TYPE + STD_VECTOR + traits::Type<_Tp>::value + ACCESS_WRITE, &vec); }
  162. template<typename _Tp, std::size_t _Nm> inline
  163. _OutputArray::_OutputArray(std::array<_Tp, _Nm>& arr)
  164. { init(FIXED_TYPE + FIXED_SIZE + STD_ARRAY + traits::Type<_Tp>::value + ACCESS_WRITE, arr.data(), Size(1, _Nm)); }
  165. template<std::size_t _Nm> inline
  166. _OutputArray::_OutputArray(std::array<Mat, _Nm>& arr)
  167. { init(STD_ARRAY_MAT + ACCESS_WRITE, arr.data(), Size(1, _Nm)); }
  168. template<typename _Tp> inline
  169. _OutputArray::_OutputArray(std::vector<std::vector<_Tp> >& vec)
  170. { init(FIXED_TYPE + STD_VECTOR_VECTOR + traits::Type<_Tp>::value + ACCESS_WRITE, &vec); }
  171. template<typename _Tp> inline
  172. _OutputArray::_OutputArray(std::vector<Mat_<_Tp> >& vec)
  173. { init(FIXED_TYPE + STD_VECTOR_MAT + traits::Type<_Tp>::value + ACCESS_WRITE, &vec); }
  174. template<typename _Tp> inline
  175. _OutputArray::_OutputArray(Mat_<_Tp>& m)
  176. { init(FIXED_TYPE + MAT + traits::Type<_Tp>::value + ACCESS_WRITE, &m); }
  177. template<typename _Tp, int m, int n> inline
  178. _OutputArray::_OutputArray(Matx<_Tp, m, n>& mtx)
  179. { init(FIXED_TYPE + FIXED_SIZE + MATX + traits::Type<_Tp>::value + ACCESS_WRITE, &mtx, Size(n, m)); }
  180. template<typename _Tp> inline
  181. _OutputArray::_OutputArray(_Tp* vec, int n)
  182. { init(FIXED_TYPE + FIXED_SIZE + MATX + traits::Type<_Tp>::value + ACCESS_WRITE, vec, Size(n, 1)); }
  183. template<typename _Tp> inline
  184. _OutputArray::_OutputArray(const std::vector<_Tp>& vec)
  185. { init(FIXED_TYPE + FIXED_SIZE + STD_VECTOR + traits::Type<_Tp>::value + ACCESS_WRITE, &vec); }
  186. template<typename _Tp, std::size_t _Nm> inline
  187. _OutputArray::_OutputArray(const std::array<_Tp, _Nm>& arr)
  188. { init(FIXED_TYPE + FIXED_SIZE + STD_ARRAY + traits::Type<_Tp>::value + ACCESS_WRITE, arr.data(), Size(1, _Nm)); }
  189. template<std::size_t _Nm> inline
  190. _OutputArray::_OutputArray(const std::array<Mat, _Nm>& arr)
  191. { init(FIXED_SIZE + STD_ARRAY_MAT + ACCESS_WRITE, arr.data(), Size(1, _Nm)); }
  192. template<typename _Tp> inline
  193. _OutputArray::_OutputArray(const std::vector<std::vector<_Tp> >& vec)
  194. { init(FIXED_TYPE + FIXED_SIZE + STD_VECTOR_VECTOR + traits::Type<_Tp>::value + ACCESS_WRITE, &vec); }
  195. template<typename _Tp> inline
  196. _OutputArray::_OutputArray(const std::vector<Mat_<_Tp> >& vec)
  197. { init(FIXED_TYPE + FIXED_SIZE + STD_VECTOR_MAT + traits::Type<_Tp>::value + ACCESS_WRITE, &vec); }
  198. template<typename _Tp> inline
  199. _OutputArray::_OutputArray(const Mat_<_Tp>& m)
  200. { init(FIXED_TYPE + FIXED_SIZE + MAT + traits::Type<_Tp>::value + ACCESS_WRITE, &m); }
  201. template<typename _Tp, int m, int n> inline
  202. _OutputArray::_OutputArray(const Matx<_Tp, m, n>& mtx)
  203. { init(FIXED_TYPE + FIXED_SIZE + MATX + traits::Type<_Tp>::value + ACCESS_WRITE, &mtx, Size(n, m)); }
  204. template<typename _Tp> inline
  205. _OutputArray::_OutputArray(const _Tp* vec, int n)
  206. { init(FIXED_TYPE + FIXED_SIZE + MATX + traits::Type<_Tp>::value + ACCESS_WRITE, vec, Size(n, 1)); }
  207. inline _OutputArray::_OutputArray(cuda::GpuMat& d_mat)
  208. { init(CUDA_GPU_MAT + ACCESS_WRITE, &d_mat); }
  209. inline _OutputArray::_OutputArray(std::vector<cuda::GpuMat>& d_mat)
  210. { init(STD_VECTOR_CUDA_GPU_MAT + ACCESS_WRITE, &d_mat);}
  211. inline _OutputArray::_OutputArray(ogl::Buffer& buf)
  212. { init(OPENGL_BUFFER + ACCESS_WRITE, &buf); }
  213. inline _OutputArray::_OutputArray(cuda::HostMem& cuda_mem)
  214. { init(CUDA_HOST_MEM + ACCESS_WRITE, &cuda_mem); }
  215. inline _OutputArray::_OutputArray(const Mat& m)
  216. { init(FIXED_TYPE + FIXED_SIZE + MAT + ACCESS_WRITE, &m); }
  217. inline _OutputArray::_OutputArray(const std::vector<Mat>& vec)
  218. { init(FIXED_SIZE + STD_VECTOR_MAT + ACCESS_WRITE, &vec); }
  219. inline _OutputArray::_OutputArray(const UMat& m)
  220. { init(FIXED_TYPE + FIXED_SIZE + UMAT + ACCESS_WRITE, &m); }
  221. inline _OutputArray::_OutputArray(const std::vector<UMat>& vec)
  222. { init(FIXED_SIZE + STD_VECTOR_UMAT + ACCESS_WRITE, &vec); }
  223. inline _OutputArray::_OutputArray(const cuda::GpuMat& d_mat)
  224. { init(FIXED_TYPE + FIXED_SIZE + CUDA_GPU_MAT + ACCESS_WRITE, &d_mat); }
  225. inline _OutputArray::_OutputArray(const ogl::Buffer& buf)
  226. { init(FIXED_TYPE + FIXED_SIZE + OPENGL_BUFFER + ACCESS_WRITE, &buf); }
  227. inline _OutputArray::_OutputArray(const cuda::HostMem& cuda_mem)
  228. { init(FIXED_TYPE + FIXED_SIZE + CUDA_HOST_MEM + ACCESS_WRITE, &cuda_mem); }
  229. template<typename _Tp> inline
  230. _OutputArray _OutputArray::rawOut(std::vector<_Tp>& vec)
  231. {
  232. _OutputArray v;
  233. v.flags = _InputArray::FIXED_TYPE + _InputArray::STD_VECTOR + rawType<_Tp>() + ACCESS_WRITE;
  234. v.obj = (void*)&vec;
  235. return v;
  236. }
  237. template<typename _Tp, std::size_t _Nm> inline
  238. _OutputArray _OutputArray::rawOut(std::array<_Tp, _Nm>& arr)
  239. {
  240. _OutputArray v;
  241. v.flags = FIXED_TYPE + FIXED_SIZE + STD_ARRAY + traits::Type<_Tp>::value + ACCESS_WRITE;
  242. v.obj = (void*)arr.data();
  243. v.sz = Size(1, _Nm);
  244. return v;
  245. }
  246. ///////////////////////////////////////////////////////////////////////////////////////////
  247. inline _InputOutputArray::_InputOutputArray() { init(0+ACCESS_RW, 0); }
  248. inline _InputOutputArray::_InputOutputArray(int _flags, void* _obj) { init(_flags+ACCESS_RW, _obj); }
  249. inline _InputOutputArray::_InputOutputArray(Mat& m) { init(MAT+ACCESS_RW, &m); }
  250. inline _InputOutputArray::_InputOutputArray(std::vector<Mat>& vec) { init(STD_VECTOR_MAT+ACCESS_RW, &vec); }
  251. inline _InputOutputArray::_InputOutputArray(UMat& m) { init(UMAT+ACCESS_RW, &m); }
  252. inline _InputOutputArray::_InputOutputArray(std::vector<UMat>& vec) { init(STD_VECTOR_UMAT+ACCESS_RW, &vec); }
  253. template<typename _Tp> inline
  254. _InputOutputArray::_InputOutputArray(std::vector<_Tp>& vec)
  255. { init(FIXED_TYPE + STD_VECTOR + traits::Type<_Tp>::value + ACCESS_RW, &vec); }
  256. template<typename _Tp, std::size_t _Nm> inline
  257. _InputOutputArray::_InputOutputArray(std::array<_Tp, _Nm>& arr)
  258. { init(FIXED_TYPE + FIXED_SIZE + STD_ARRAY + traits::Type<_Tp>::value + ACCESS_RW, arr.data(), Size(1, _Nm)); }
  259. template<std::size_t _Nm> inline
  260. _InputOutputArray::_InputOutputArray(std::array<Mat, _Nm>& arr)
  261. { init(STD_ARRAY_MAT + ACCESS_RW, arr.data(), Size(1, _Nm)); }
  262. template<typename _Tp> inline
  263. _InputOutputArray::_InputOutputArray(std::vector<std::vector<_Tp> >& vec)
  264. { init(FIXED_TYPE + STD_VECTOR_VECTOR + traits::Type<_Tp>::value + ACCESS_RW, &vec); }
  265. template<typename _Tp> inline
  266. _InputOutputArray::_InputOutputArray(std::vector<Mat_<_Tp> >& vec)
  267. { init(FIXED_TYPE + STD_VECTOR_MAT + traits::Type<_Tp>::value + ACCESS_RW, &vec); }
  268. template<typename _Tp> inline
  269. _InputOutputArray::_InputOutputArray(Mat_<_Tp>& m)
  270. { init(FIXED_TYPE + MAT + traits::Type<_Tp>::value + ACCESS_RW, &m); }
  271. template<typename _Tp, int m, int n> inline
  272. _InputOutputArray::_InputOutputArray(Matx<_Tp, m, n>& mtx)
  273. { init(FIXED_TYPE + FIXED_SIZE + MATX + traits::Type<_Tp>::value + ACCESS_RW, &mtx, Size(n, m)); }
  274. template<typename _Tp> inline
  275. _InputOutputArray::_InputOutputArray(_Tp* vec, int n)
  276. { init(FIXED_TYPE + FIXED_SIZE + MATX + traits::Type<_Tp>::value + ACCESS_RW, vec, Size(n, 1)); }
  277. template<typename _Tp> inline
  278. _InputOutputArray::_InputOutputArray(const std::vector<_Tp>& vec)
  279. { init(FIXED_TYPE + FIXED_SIZE + STD_VECTOR + traits::Type<_Tp>::value + ACCESS_RW, &vec); }
  280. template<typename _Tp, std::size_t _Nm> inline
  281. _InputOutputArray::_InputOutputArray(const std::array<_Tp, _Nm>& arr)
  282. { init(FIXED_TYPE + FIXED_SIZE + STD_ARRAY + traits::Type<_Tp>::value + ACCESS_RW, arr.data(), Size(1, _Nm)); }
  283. template<std::size_t _Nm> inline
  284. _InputOutputArray::_InputOutputArray(const std::array<Mat, _Nm>& arr)
  285. { init(FIXED_SIZE + STD_ARRAY_MAT + ACCESS_RW, arr.data(), Size(1, _Nm)); }
  286. template<typename _Tp> inline
  287. _InputOutputArray::_InputOutputArray(const std::vector<std::vector<_Tp> >& vec)
  288. { init(FIXED_TYPE + FIXED_SIZE + STD_VECTOR_VECTOR + traits::Type<_Tp>::value + ACCESS_RW, &vec); }
  289. template<typename _Tp> inline
  290. _InputOutputArray::_InputOutputArray(const std::vector<Mat_<_Tp> >& vec)
  291. { init(FIXED_TYPE + FIXED_SIZE + STD_VECTOR_MAT + traits::Type<_Tp>::value + ACCESS_RW, &vec); }
  292. template<typename _Tp> inline
  293. _InputOutputArray::_InputOutputArray(const Mat_<_Tp>& m)
  294. { init(FIXED_TYPE + FIXED_SIZE + MAT + traits::Type<_Tp>::value + ACCESS_RW, &m); }
  295. template<typename _Tp, int m, int n> inline
  296. _InputOutputArray::_InputOutputArray(const Matx<_Tp, m, n>& mtx)
  297. { init(FIXED_TYPE + FIXED_SIZE + MATX + traits::Type<_Tp>::value + ACCESS_RW, &mtx, Size(n, m)); }
  298. template<typename _Tp> inline
  299. _InputOutputArray::_InputOutputArray(const _Tp* vec, int n)
  300. { init(FIXED_TYPE + FIXED_SIZE + MATX + traits::Type<_Tp>::value + ACCESS_RW, vec, Size(n, 1)); }
  301. inline _InputOutputArray::_InputOutputArray(cuda::GpuMat& d_mat)
  302. { init(CUDA_GPU_MAT + ACCESS_RW, &d_mat); }
  303. inline _InputOutputArray::_InputOutputArray(ogl::Buffer& buf)
  304. { init(OPENGL_BUFFER + ACCESS_RW, &buf); }
  305. inline _InputOutputArray::_InputOutputArray(cuda::HostMem& cuda_mem)
  306. { init(CUDA_HOST_MEM + ACCESS_RW, &cuda_mem); }
  307. inline _InputOutputArray::_InputOutputArray(const Mat& m)
  308. { init(FIXED_TYPE + FIXED_SIZE + MAT + ACCESS_RW, &m); }
  309. inline _InputOutputArray::_InputOutputArray(const std::vector<Mat>& vec)
  310. { init(FIXED_SIZE + STD_VECTOR_MAT + ACCESS_RW, &vec); }
  311. inline _InputOutputArray::_InputOutputArray(const UMat& m)
  312. { init(FIXED_TYPE + FIXED_SIZE + UMAT + ACCESS_RW, &m); }
  313. inline _InputOutputArray::_InputOutputArray(const std::vector<UMat>& vec)
  314. { init(FIXED_SIZE + STD_VECTOR_UMAT + ACCESS_RW, &vec); }
  315. inline _InputOutputArray::_InputOutputArray(const cuda::GpuMat& d_mat)
  316. { init(FIXED_TYPE + FIXED_SIZE + CUDA_GPU_MAT + ACCESS_RW, &d_mat); }
  317. inline _InputOutputArray::_InputOutputArray(const std::vector<cuda::GpuMat>& d_mat)
  318. { init(FIXED_TYPE + FIXED_SIZE + STD_VECTOR_CUDA_GPU_MAT + ACCESS_RW, &d_mat);}
  319. template<> inline _InputOutputArray::_InputOutputArray(std::vector<cuda::GpuMat>& d_mat)
  320. { init(FIXED_TYPE + FIXED_SIZE + STD_VECTOR_CUDA_GPU_MAT + ACCESS_RW, &d_mat);}
  321. inline _InputOutputArray::_InputOutputArray(const ogl::Buffer& buf)
  322. { init(FIXED_TYPE + FIXED_SIZE + OPENGL_BUFFER + ACCESS_RW, &buf); }
  323. inline _InputOutputArray::_InputOutputArray(const cuda::HostMem& cuda_mem)
  324. { init(FIXED_TYPE + FIXED_SIZE + CUDA_HOST_MEM + ACCESS_RW, &cuda_mem); }
  325. template<typename _Tp> inline
  326. _InputOutputArray _InputOutputArray::rawInOut(std::vector<_Tp>& vec)
  327. {
  328. _InputOutputArray v;
  329. v.flags = _InputArray::FIXED_TYPE + _InputArray::STD_VECTOR + rawType<_Tp>() + ACCESS_RW;
  330. v.obj = (void*)&vec;
  331. return v;
  332. }
  333. template<typename _Tp, std::size_t _Nm> inline
  334. _InputOutputArray _InputOutputArray::rawInOut(std::array<_Tp, _Nm>& arr)
  335. {
  336. _InputOutputArray v;
  337. v.flags = FIXED_TYPE + FIXED_SIZE + STD_ARRAY + traits::Type<_Tp>::value + ACCESS_RW;
  338. v.obj = (void*)arr.data();
  339. v.sz = Size(1, _Nm);
  340. return v;
  341. }
  342. template<typename _Tp> static inline _InputArray rawIn(_Tp& v) { return _InputArray::rawIn(v); }
  343. template<typename _Tp> static inline _OutputArray rawOut(_Tp& v) { return _OutputArray::rawOut(v); }
  344. template<typename _Tp> static inline _InputOutputArray rawInOut(_Tp& v) { return _InputOutputArray::rawInOut(v); }
  345. CV__DEBUG_NS_END
  346. //////////////////////////////////////////// Mat //////////////////////////////////////////
  347. inline
  348. Mat::Mat()
  349. : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0),
  350. datalimit(0), allocator(0), u(0), size(&rows), step(0)
  351. {}
  352. inline
  353. Mat::Mat(int _rows, int _cols, int _type)
  354. : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0),
  355. datalimit(0), allocator(0), u(0), size(&rows), step(0)
  356. {
  357. create(_rows, _cols, _type);
  358. }
  359. inline
  360. Mat::Mat(int _rows, int _cols, int _type, const Scalar& _s)
  361. : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0),
  362. datalimit(0), allocator(0), u(0), size(&rows), step(0)
  363. {
  364. create(_rows, _cols, _type);
  365. *this = _s;
  366. }
  367. inline
  368. Mat::Mat(Size _sz, int _type)
  369. : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0),
  370. datalimit(0), allocator(0), u(0), size(&rows), step(0)
  371. {
  372. create( _sz.height, _sz.width, _type );
  373. }
  374. inline
  375. Mat::Mat(Size _sz, int _type, const Scalar& _s)
  376. : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0),
  377. datalimit(0), allocator(0), u(0), size(&rows), step(0)
  378. {
  379. create(_sz.height, _sz.width, _type);
  380. *this = _s;
  381. }
  382. inline
  383. Mat::Mat(int _dims, const int* _sz, int _type)
  384. : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0),
  385. datalimit(0), allocator(0), u(0), size(&rows), step(0)
  386. {
  387. create(_dims, _sz, _type);
  388. }
  389. inline
  390. Mat::Mat(int _dims, const int* _sz, int _type, const Scalar& _s)
  391. : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0),
  392. datalimit(0), allocator(0), u(0), size(&rows), step(0)
  393. {
  394. create(_dims, _sz, _type);
  395. *this = _s;
  396. }
  397. inline
  398. Mat::Mat(const std::vector<int>& _sz, int _type)
  399. : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0),
  400. datalimit(0), allocator(0), u(0), size(&rows), step(0)
  401. {
  402. create(_sz, _type);
  403. }
  404. inline
  405. Mat::Mat(const std::vector<int>& _sz, int _type, const Scalar& _s)
  406. : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0),
  407. datalimit(0), allocator(0), u(0), size(&rows), step(0)
  408. {
  409. create(_sz, _type);
  410. *this = _s;
  411. }
  412. inline
  413. Mat::Mat(const Mat& m)
  414. : flags(m.flags), dims(m.dims), rows(m.rows), cols(m.cols), data(m.data),
  415. datastart(m.datastart), dataend(m.dataend), datalimit(m.datalimit), allocator(m.allocator),
  416. u(m.u), size(&rows), step(0)
  417. {
  418. if( u )
  419. CV_XADD(&u->refcount, 1);
  420. if( m.dims <= 2 )
  421. {
  422. step[0] = m.step[0]; step[1] = m.step[1];
  423. }
  424. else
  425. {
  426. dims = 0;
  427. copySize(m);
  428. }
  429. }
  430. inline
  431. Mat::Mat(int _rows, int _cols, int _type, void* _data, size_t _step)
  432. : flags(MAGIC_VAL + (_type & TYPE_MASK)), dims(2), rows(_rows), cols(_cols),
  433. data((uchar*)_data), datastart((uchar*)_data), dataend(0), datalimit(0),
  434. allocator(0), u(0), size(&rows)
  435. {
  436. CV_Assert(total() == 0 || data != NULL);
  437. size_t esz = CV_ELEM_SIZE(_type), esz1 = CV_ELEM_SIZE1(_type);
  438. size_t minstep = cols * esz;
  439. if( _step == AUTO_STEP )
  440. {
  441. _step = minstep;
  442. }
  443. else
  444. {
  445. CV_DbgAssert( _step >= minstep );
  446. if (_step % esz1 != 0)
  447. {
  448. CV_Error(Error::BadStep, "Step must be a multiple of esz1");
  449. }
  450. }
  451. step[0] = _step;
  452. step[1] = esz;
  453. datalimit = datastart + _step * rows;
  454. dataend = datalimit - _step + minstep;
  455. updateContinuityFlag();
  456. }
  457. inline
  458. Mat::Mat(Size _sz, int _type, void* _data, size_t _step)
  459. : flags(MAGIC_VAL + (_type & TYPE_MASK)), dims(2), rows(_sz.height), cols(_sz.width),
  460. data((uchar*)_data), datastart((uchar*)_data), dataend(0), datalimit(0),
  461. allocator(0), u(0), size(&rows)
  462. {
  463. CV_Assert(total() == 0 || data != NULL);
  464. size_t esz = CV_ELEM_SIZE(_type), esz1 = CV_ELEM_SIZE1(_type);
  465. size_t minstep = cols*esz;
  466. if( _step == AUTO_STEP )
  467. {
  468. _step = minstep;
  469. }
  470. else
  471. {
  472. CV_DbgAssert( _step >= minstep );
  473. if (_step % esz1 != 0)
  474. {
  475. CV_Error(Error::BadStep, "Step must be a multiple of esz1");
  476. }
  477. }
  478. step[0] = _step;
  479. step[1] = esz;
  480. datalimit = datastart + _step*rows;
  481. dataend = datalimit - _step + minstep;
  482. updateContinuityFlag();
  483. }
  484. template<typename _Tp> inline
  485. Mat::Mat(const std::vector<_Tp>& vec, bool copyData)
  486. : flags(MAGIC_VAL + traits::Type<_Tp>::value + CV_MAT_CONT_FLAG), dims(2), rows((int)vec.size()),
  487. cols(1), data(0), datastart(0), dataend(0), datalimit(0), allocator(0), u(0), size(&rows), step(0)
  488. {
  489. if(vec.empty())
  490. return;
  491. if( !copyData )
  492. {
  493. step[0] = step[1] = sizeof(_Tp);
  494. datastart = data = (uchar*)&vec[0];
  495. datalimit = dataend = datastart + rows * step[0];
  496. }
  497. else
  498. Mat((int)vec.size(), 1, traits::Type<_Tp>::value, (uchar*)&vec[0]).copyTo(*this);
  499. }
  500. template<typename _Tp, typename> inline
  501. Mat::Mat(const std::initializer_list<_Tp> list)
  502. : Mat()
  503. {
  504. CV_Assert(list.size() != 0);
  505. Mat((int)list.size(), 1, traits::Type<_Tp>::value, (uchar*)list.begin()).copyTo(*this);
  506. }
  507. template<typename _Tp> inline
  508. Mat::Mat(const std::initializer_list<int> sizes, const std::initializer_list<_Tp> list)
  509. : Mat()
  510. {
  511. size_t size_total = 1;
  512. for(auto s : sizes)
  513. size_total *= s;
  514. CV_Assert(list.size() != 0);
  515. CV_Assert(size_total == list.size());
  516. Mat((int)sizes.size(), (int*)sizes.begin(), traits::Type<_Tp>::value, (uchar*)list.begin()).copyTo(*this);
  517. }
  518. template<typename _Tp, std::size_t _Nm> inline
  519. Mat::Mat(const std::array<_Tp, _Nm>& arr, bool copyData)
  520. : flags(MAGIC_VAL + traits::Type<_Tp>::value + CV_MAT_CONT_FLAG), dims(2), rows((int)arr.size()),
  521. cols(1), data(0), datastart(0), dataend(0), datalimit(0), allocator(0), u(0), size(&rows), step(0)
  522. {
  523. if(arr.empty())
  524. return;
  525. if( !copyData )
  526. {
  527. step[0] = step[1] = sizeof(_Tp);
  528. datastart = data = (uchar*)arr.data();
  529. datalimit = dataend = datastart + rows * step[0];
  530. }
  531. else
  532. Mat((int)arr.size(), 1, traits::Type<_Tp>::value, (uchar*)arr.data()).copyTo(*this);
  533. }
  534. template<typename _Tp, int n> inline
  535. Mat::Mat(const Vec<_Tp, n>& vec, bool copyData)
  536. : flags(MAGIC_VAL + traits::Type<_Tp>::value + CV_MAT_CONT_FLAG), dims(2), rows(n), cols(1), data(0),
  537. datastart(0), dataend(0), datalimit(0), allocator(0), u(0), size(&rows), step(0)
  538. {
  539. if( !copyData )
  540. {
  541. step[0] = step[1] = sizeof(_Tp);
  542. datastart = data = (uchar*)vec.val;
  543. datalimit = dataend = datastart + rows * step[0];
  544. }
  545. else
  546. Mat(n, 1, traits::Type<_Tp>::value, (void*)vec.val).copyTo(*this);
  547. }
  548. template<typename _Tp, int m, int n> inline
  549. Mat::Mat(const Matx<_Tp,m,n>& M, bool copyData)
  550. : flags(MAGIC_VAL + traits::Type<_Tp>::value + CV_MAT_CONT_FLAG), dims(2), rows(m), cols(n), data(0),
  551. datastart(0), dataend(0), datalimit(0), allocator(0), u(0), size(&rows), step(0)
  552. {
  553. if( !copyData )
  554. {
  555. step[0] = cols * sizeof(_Tp);
  556. step[1] = sizeof(_Tp);
  557. datastart = data = (uchar*)M.val;
  558. datalimit = dataend = datastart + rows * step[0];
  559. }
  560. else
  561. Mat(m, n, traits::Type<_Tp>::value, (uchar*)M.val).copyTo(*this);
  562. }
  563. template<typename _Tp> inline
  564. Mat::Mat(const Point_<_Tp>& pt, bool copyData)
  565. : flags(MAGIC_VAL + traits::Type<_Tp>::value + CV_MAT_CONT_FLAG), dims(2), rows(2), cols(1), data(0),
  566. datastart(0), dataend(0), datalimit(0), allocator(0), u(0), size(&rows), step(0)
  567. {
  568. if( !copyData )
  569. {
  570. step[0] = step[1] = sizeof(_Tp);
  571. datastart = data = (uchar*)&pt.x;
  572. datalimit = dataend = datastart + rows * step[0];
  573. }
  574. else
  575. {
  576. create(2, 1, traits::Type<_Tp>::value);
  577. ((_Tp*)data)[0] = pt.x;
  578. ((_Tp*)data)[1] = pt.y;
  579. }
  580. }
  581. template<typename _Tp> inline
  582. Mat::Mat(const Point3_<_Tp>& pt, bool copyData)
  583. : flags(MAGIC_VAL + traits::Type<_Tp>::value + CV_MAT_CONT_FLAG), dims(2), rows(3), cols(1), data(0),
  584. datastart(0), dataend(0), datalimit(0), allocator(0), u(0), size(&rows), step(0)
  585. {
  586. if( !copyData )
  587. {
  588. step[0] = step[1] = sizeof(_Tp);
  589. datastart = data = (uchar*)&pt.x;
  590. datalimit = dataend = datastart + rows * step[0];
  591. }
  592. else
  593. {
  594. create(3, 1, traits::Type<_Tp>::value);
  595. ((_Tp*)data)[0] = pt.x;
  596. ((_Tp*)data)[1] = pt.y;
  597. ((_Tp*)data)[2] = pt.z;
  598. }
  599. }
  600. template<typename _Tp> inline
  601. Mat::Mat(const MatCommaInitializer_<_Tp>& commaInitializer)
  602. : flags(MAGIC_VAL + traits::Type<_Tp>::value + CV_MAT_CONT_FLAG), dims(0), rows(0), cols(0), data(0),
  603. datastart(0), dataend(0), allocator(0), u(0), size(&rows)
  604. {
  605. *this = commaInitializer.operator Mat_<_Tp>();
  606. }
  607. inline
  608. Mat::~Mat()
  609. {
  610. release();
  611. if( step.p != step.buf )
  612. fastFree(step.p);
  613. }
  614. inline
  615. Mat& Mat::operator = (const Mat& m)
  616. {
  617. if( this != &m )
  618. {
  619. if( m.u )
  620. CV_XADD(&m.u->refcount, 1);
  621. release();
  622. flags = m.flags;
  623. if( dims <= 2 && m.dims <= 2 )
  624. {
  625. dims = m.dims;
  626. rows = m.rows;
  627. cols = m.cols;
  628. step[0] = m.step[0];
  629. step[1] = m.step[1];
  630. }
  631. else
  632. copySize(m);
  633. data = m.data;
  634. datastart = m.datastart;
  635. dataend = m.dataend;
  636. datalimit = m.datalimit;
  637. allocator = m.allocator;
  638. u = m.u;
  639. }
  640. return *this;
  641. }
  642. inline
  643. Mat Mat::row(int y) const
  644. {
  645. return Mat(*this, Range(y, y + 1), Range::all());
  646. }
  647. inline
  648. Mat Mat::col(int x) const
  649. {
  650. return Mat(*this, Range::all(), Range(x, x + 1));
  651. }
  652. inline
  653. Mat Mat::rowRange(int startrow, int endrow) const
  654. {
  655. return Mat(*this, Range(startrow, endrow), Range::all());
  656. }
  657. inline
  658. Mat Mat::rowRange(const Range& r) const
  659. {
  660. return Mat(*this, r, Range::all());
  661. }
  662. inline
  663. Mat Mat::colRange(int startcol, int endcol) const
  664. {
  665. return Mat(*this, Range::all(), Range(startcol, endcol));
  666. }
  667. inline
  668. Mat Mat::colRange(const Range& r) const
  669. {
  670. return Mat(*this, Range::all(), r);
  671. }
  672. inline
  673. Mat Mat::clone() const
  674. {
  675. Mat m;
  676. copyTo(m);
  677. return m;
  678. }
  679. inline
  680. void Mat::assignTo( Mat& m, int _type ) const
  681. {
  682. if( _type < 0 )
  683. m = *this;
  684. else
  685. convertTo(m, _type);
  686. }
  687. inline
  688. void Mat::create(int _rows, int _cols, int _type)
  689. {
  690. _type &= TYPE_MASK;
  691. if( dims <= 2 && rows == _rows && cols == _cols && type() == _type && data )
  692. return;
  693. int sz[] = {_rows, _cols};
  694. create(2, sz, _type);
  695. }
  696. inline
  697. void Mat::create(Size _sz, int _type)
  698. {
  699. create(_sz.height, _sz.width, _type);
  700. }
  701. inline
  702. void Mat::addref()
  703. {
  704. if( u )
  705. CV_XADD(&u->refcount, 1);
  706. }
  707. inline
  708. void Mat::release()
  709. {
  710. if( u && CV_XADD(&u->refcount, -1) == 1 )
  711. deallocate();
  712. u = NULL;
  713. datastart = dataend = datalimit = data = 0;
  714. for(int i = 0; i < dims; i++)
  715. size.p[i] = 0;
  716. #ifdef _DEBUG
  717. flags = MAGIC_VAL;
  718. dims = rows = cols = 0;
  719. if(step.p != step.buf)
  720. {
  721. fastFree(step.p);
  722. step.p = step.buf;
  723. size.p = &rows;
  724. }
  725. #endif
  726. }
  727. inline
  728. Mat Mat::operator()( Range _rowRange, Range _colRange ) const
  729. {
  730. return Mat(*this, _rowRange, _colRange);
  731. }
  732. inline
  733. Mat Mat::operator()( const Rect& roi ) const
  734. {
  735. return Mat(*this, roi);
  736. }
  737. inline
  738. Mat Mat::operator()(const Range* ranges) const
  739. {
  740. return Mat(*this, ranges);
  741. }
  742. inline
  743. Mat Mat::operator()(const std::vector<Range>& ranges) const
  744. {
  745. return Mat(*this, ranges);
  746. }
  747. inline
  748. bool Mat::isContinuous() const
  749. {
  750. return (flags & CONTINUOUS_FLAG) != 0;
  751. }
  752. inline
  753. bool Mat::isSubmatrix() const
  754. {
  755. return (flags & SUBMATRIX_FLAG) != 0;
  756. }
  757. inline
  758. size_t Mat::elemSize() const
  759. {
  760. size_t res = dims > 0 ? step.p[dims - 1] : 0;
  761. CV_DbgAssert(res != 0);
  762. return res;
  763. }
  764. inline
  765. size_t Mat::elemSize1() const
  766. {
  767. return CV_ELEM_SIZE1(flags);
  768. }
  769. inline
  770. int Mat::type() const
  771. {
  772. return CV_MAT_TYPE(flags);
  773. }
  774. inline
  775. int Mat::depth() const
  776. {
  777. return CV_MAT_DEPTH(flags);
  778. }
  779. inline
  780. int Mat::channels() const
  781. {
  782. return CV_MAT_CN(flags);
  783. }
  784. inline
  785. size_t Mat::step1(int i) const
  786. {
  787. return step.p[i] / elemSize1();
  788. }
  789. inline
  790. bool Mat::empty() const
  791. {
  792. return data == 0 || total() == 0 || dims == 0;
  793. }
  794. inline
  795. size_t Mat::total() const
  796. {
  797. if( dims <= 2 )
  798. return (size_t)rows * cols;
  799. size_t p = 1;
  800. for( int i = 0; i < dims; i++ )
  801. p *= size[i];
  802. return p;
  803. }
  804. inline
  805. size_t Mat::total(int startDim, int endDim) const
  806. {
  807. CV_Assert( 0 <= startDim && startDim <= endDim);
  808. size_t p = 1;
  809. int endDim_ = endDim <= dims ? endDim : dims;
  810. for( int i = startDim; i < endDim_; i++ )
  811. p *= size[i];
  812. return p;
  813. }
  814. inline
  815. uchar* Mat::ptr(int y)
  816. {
  817. CV_DbgAssert( y == 0 || (data && dims >= 1 && (unsigned)y < (unsigned)size.p[0]) );
  818. return data + step.p[0] * y;
  819. }
  820. inline
  821. const uchar* Mat::ptr(int y) const
  822. {
  823. CV_DbgAssert( y == 0 || (data && dims >= 1 && (unsigned)y < (unsigned)size.p[0]) );
  824. return data + step.p[0] * y;
  825. }
  826. template<typename _Tp> inline
  827. _Tp* Mat::ptr(int y)
  828. {
  829. CV_DbgAssert( y == 0 || (data && dims >= 1 && (unsigned)y < (unsigned)size.p[0]) );
  830. return (_Tp*)(data + step.p[0] * y);
  831. }
  832. template<typename _Tp> inline
  833. const _Tp* Mat::ptr(int y) const
  834. {
  835. CV_DbgAssert( y == 0 || (data && dims >= 1 && (unsigned)y < (unsigned)size.p[0]) );
  836. return (const _Tp*)(data + step.p[0] * y);
  837. }
  838. inline
  839. uchar* Mat::ptr(int i0, int i1)
  840. {
  841. CV_DbgAssert(dims >= 2);
  842. CV_DbgAssert(data);
  843. CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
  844. CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
  845. return data + i0 * step.p[0] + i1 * step.p[1];
  846. }
  847. inline
  848. const uchar* Mat::ptr(int i0, int i1) const
  849. {
  850. CV_DbgAssert(dims >= 2);
  851. CV_DbgAssert(data);
  852. CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
  853. CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
  854. return data + i0 * step.p[0] + i1 * step.p[1];
  855. }
  856. template<typename _Tp> inline
  857. _Tp* Mat::ptr(int i0, int i1)
  858. {
  859. CV_DbgAssert(dims >= 2);
  860. CV_DbgAssert(data);
  861. CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
  862. CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
  863. return (_Tp*)(data + i0 * step.p[0] + i1 * step.p[1]);
  864. }
  865. template<typename _Tp> inline
  866. const _Tp* Mat::ptr(int i0, int i1) const
  867. {
  868. CV_DbgAssert(dims >= 2);
  869. CV_DbgAssert(data);
  870. CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
  871. CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
  872. return (const _Tp*)(data + i0 * step.p[0] + i1 * step.p[1]);
  873. }
  874. inline
  875. uchar* Mat::ptr(int i0, int i1, int i2)
  876. {
  877. CV_DbgAssert(dims >= 3);
  878. CV_DbgAssert(data);
  879. CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
  880. CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
  881. CV_DbgAssert((unsigned)i2 < (unsigned)size.p[2]);
  882. return data + i0 * step.p[0] + i1 * step.p[1] + i2 * step.p[2];
  883. }
  884. inline
  885. const uchar* Mat::ptr(int i0, int i1, int i2) const
  886. {
  887. CV_DbgAssert(dims >= 3);
  888. CV_DbgAssert(data);
  889. CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
  890. CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
  891. CV_DbgAssert((unsigned)i2 < (unsigned)size.p[2]);
  892. return data + i0 * step.p[0] + i1 * step.p[1] + i2 * step.p[2];
  893. }
  894. template<typename _Tp> inline
  895. _Tp* Mat::ptr(int i0, int i1, int i2)
  896. {
  897. CV_DbgAssert(dims >= 3);
  898. CV_DbgAssert(data);
  899. CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
  900. CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
  901. CV_DbgAssert((unsigned)i2 < (unsigned)size.p[2]);
  902. return (_Tp*)(data + i0 * step.p[0] + i1 * step.p[1] + i2 * step.p[2]);
  903. }
  904. template<typename _Tp> inline
  905. const _Tp* Mat::ptr(int i0, int i1, int i2) const
  906. {
  907. CV_DbgAssert(dims >= 3);
  908. CV_DbgAssert(data);
  909. CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
  910. CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
  911. CV_DbgAssert((unsigned)i2 < (unsigned)size.p[2]);
  912. return (const _Tp*)(data + i0 * step.p[0] + i1 * step.p[1] + i2 * step.p[2]);
  913. }
  914. inline
  915. uchar* Mat::ptr(const int* idx)
  916. {
  917. int i, d = dims;
  918. uchar* p = data;
  919. CV_DbgAssert( d >= 1 && p );
  920. for( i = 0; i < d; i++ )
  921. {
  922. CV_DbgAssert( (unsigned)idx[i] < (unsigned)size.p[i] );
  923. p += idx[i] * step.p[i];
  924. }
  925. return p;
  926. }
  927. inline
  928. const uchar* Mat::ptr(const int* idx) const
  929. {
  930. int i, d = dims;
  931. uchar* p = data;
  932. CV_DbgAssert( d >= 1 && p );
  933. for( i = 0; i < d; i++ )
  934. {
  935. CV_DbgAssert( (unsigned)idx[i] < (unsigned)size.p[i] );
  936. p += idx[i] * step.p[i];
  937. }
  938. return p;
  939. }
  940. template<typename _Tp> inline
  941. _Tp* Mat::ptr(const int* idx)
  942. {
  943. int i, d = dims;
  944. uchar* p = data;
  945. CV_DbgAssert( d >= 1 && p );
  946. for( i = 0; i < d; i++ )
  947. {
  948. CV_DbgAssert( (unsigned)idx[i] < (unsigned)size.p[i] );
  949. p += idx[i] * step.p[i];
  950. }
  951. return (_Tp*)p;
  952. }
  953. template<typename _Tp> inline
  954. const _Tp* Mat::ptr(const int* idx) const
  955. {
  956. int i, d = dims;
  957. uchar* p = data;
  958. CV_DbgAssert( d >= 1 && p );
  959. for( i = 0; i < d; i++ )
  960. {
  961. CV_DbgAssert( (unsigned)idx[i] < (unsigned)size.p[i] );
  962. p += idx[i] * step.p[i];
  963. }
  964. return (const _Tp*)p;
  965. }
  966. template<typename _Tp> inline
  967. _Tp& Mat::at(int i0, int i1)
  968. {
  969. CV_DbgAssert(dims <= 2);
  970. CV_DbgAssert(data);
  971. CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
  972. CV_DbgAssert((unsigned)(i1 * DataType<_Tp>::channels) < (unsigned)(size.p[1] * channels()));
  973. CV_DbgAssert(CV_ELEM_SIZE1(traits::Depth<_Tp>::value) == elemSize1());
  974. return ((_Tp*)(data + step.p[0] * i0))[i1];
  975. }
  976. template<typename _Tp> inline
  977. const _Tp& Mat::at(int i0, int i1) const
  978. {
  979. CV_DbgAssert(dims <= 2);
  980. CV_DbgAssert(data);
  981. CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
  982. CV_DbgAssert((unsigned)(i1 * DataType<_Tp>::channels) < (unsigned)(size.p[1] * channels()));
  983. CV_DbgAssert(CV_ELEM_SIZE1(traits::Depth<_Tp>::value) == elemSize1());
  984. return ((const _Tp*)(data + step.p[0] * i0))[i1];
  985. }
  986. template<typename _Tp> inline
  987. _Tp& Mat::at(Point pt)
  988. {
  989. CV_DbgAssert(dims <= 2);
  990. CV_DbgAssert(data);
  991. CV_DbgAssert((unsigned)pt.y < (unsigned)size.p[0]);
  992. CV_DbgAssert((unsigned)(pt.x * DataType<_Tp>::channels) < (unsigned)(size.p[1] * channels()));
  993. CV_DbgAssert(CV_ELEM_SIZE1(traits::Depth<_Tp>::value) == elemSize1());
  994. return ((_Tp*)(data + step.p[0] * pt.y))[pt.x];
  995. }
  996. template<typename _Tp> inline
  997. const _Tp& Mat::at(Point pt) const
  998. {
  999. CV_DbgAssert(dims <= 2);
  1000. CV_DbgAssert(data);
  1001. CV_DbgAssert((unsigned)pt.y < (unsigned)size.p[0]);
  1002. CV_DbgAssert((unsigned)(pt.x * DataType<_Tp>::channels) < (unsigned)(size.p[1] * channels()));
  1003. CV_DbgAssert(CV_ELEM_SIZE1(traits::Depth<_Tp>::value) == elemSize1());
  1004. return ((const _Tp*)(data + step.p[0] * pt.y))[pt.x];
  1005. }
  1006. template<typename _Tp> inline
  1007. _Tp& Mat::at(int i0)
  1008. {
  1009. CV_DbgAssert(dims <= 2);
  1010. CV_DbgAssert(data);
  1011. CV_DbgAssert((unsigned)i0 < (unsigned)(size.p[0] * size.p[1]));
  1012. CV_DbgAssert(elemSize() == sizeof(_Tp));
  1013. if( isContinuous() || size.p[0] == 1 )
  1014. return ((_Tp*)data)[i0];
  1015. if( size.p[1] == 1 )
  1016. return *(_Tp*)(data + step.p[0] * i0);
  1017. int i = i0 / cols, j = i0 - i * cols;
  1018. return ((_Tp*)(data + step.p[0] * i))[j];
  1019. }
  1020. template<typename _Tp> inline
  1021. const _Tp& Mat::at(int i0) const
  1022. {
  1023. CV_DbgAssert(dims <= 2);
  1024. CV_DbgAssert(data);
  1025. CV_DbgAssert((unsigned)i0 < (unsigned)(size.p[0] * size.p[1]));
  1026. CV_DbgAssert(elemSize() == sizeof(_Tp));
  1027. if( isContinuous() || size.p[0] == 1 )
  1028. return ((const _Tp*)data)[i0];
  1029. if( size.p[1] == 1 )
  1030. return *(const _Tp*)(data + step.p[0] * i0);
  1031. int i = i0 / cols, j = i0 - i * cols;
  1032. return ((const _Tp*)(data + step.p[0] * i))[j];
  1033. }
  1034. template<typename _Tp> inline
  1035. _Tp& Mat::at(int i0, int i1, int i2)
  1036. {
  1037. CV_DbgAssert( elemSize() == sizeof(_Tp) );
  1038. return *(_Tp*)ptr(i0, i1, i2);
  1039. }
  1040. template<typename _Tp> inline
  1041. const _Tp& Mat::at(int i0, int i1, int i2) const
  1042. {
  1043. CV_DbgAssert( elemSize() == sizeof(_Tp) );
  1044. return *(const _Tp*)ptr(i0, i1, i2);
  1045. }
  1046. template<typename _Tp> inline
  1047. _Tp& Mat::at(const int* idx)
  1048. {
  1049. CV_DbgAssert( elemSize() == sizeof(_Tp) );
  1050. return *(_Tp*)ptr(idx);
  1051. }
  1052. template<typename _Tp> inline
  1053. const _Tp& Mat::at(const int* idx) const
  1054. {
  1055. CV_DbgAssert( elemSize() == sizeof(_Tp) );
  1056. return *(const _Tp*)ptr(idx);
  1057. }
  1058. template<typename _Tp, int n> inline
  1059. _Tp& Mat::at(const Vec<int, n>& idx)
  1060. {
  1061. CV_DbgAssert( elemSize() == sizeof(_Tp) );
  1062. return *(_Tp*)ptr(idx.val);
  1063. }
  1064. template<typename _Tp, int n> inline
  1065. const _Tp& Mat::at(const Vec<int, n>& idx) const
  1066. {
  1067. CV_DbgAssert( elemSize() == sizeof(_Tp) );
  1068. return *(const _Tp*)ptr(idx.val);
  1069. }
  1070. template<typename _Tp> inline
  1071. MatConstIterator_<_Tp> Mat::begin() const
  1072. {
  1073. CV_DbgAssert( elemSize() == sizeof(_Tp) );
  1074. return MatConstIterator_<_Tp>((const Mat_<_Tp>*)this);
  1075. }
  1076. template<typename _Tp> inline
  1077. MatConstIterator_<_Tp> Mat::end() const
  1078. {
  1079. CV_DbgAssert( elemSize() == sizeof(_Tp) );
  1080. MatConstIterator_<_Tp> it((const Mat_<_Tp>*)this);
  1081. it += total();
  1082. return it;
  1083. }
  1084. template<typename _Tp> inline
  1085. MatIterator_<_Tp> Mat::begin()
  1086. {
  1087. CV_DbgAssert( elemSize() == sizeof(_Tp) );
  1088. return MatIterator_<_Tp>((Mat_<_Tp>*)this);
  1089. }
  1090. template<typename _Tp> inline
  1091. MatIterator_<_Tp> Mat::end()
  1092. {
  1093. CV_DbgAssert( elemSize() == sizeof(_Tp) );
  1094. MatIterator_<_Tp> it((Mat_<_Tp>*)this);
  1095. it += total();
  1096. return it;
  1097. }
  1098. template<typename _Tp, typename Functor> inline
  1099. void Mat::forEach(const Functor& operation) {
  1100. this->forEach_impl<_Tp>(operation);
  1101. }
  1102. template<typename _Tp, typename Functor> inline
  1103. void Mat::forEach(const Functor& operation) const {
  1104. // call as not const
  1105. (const_cast<Mat*>(this))->forEach<_Tp>(operation);
  1106. }
  1107. template<typename _Tp> inline
  1108. Mat::operator std::vector<_Tp>() const
  1109. {
  1110. std::vector<_Tp> v;
  1111. copyTo(v);
  1112. return v;
  1113. }
  1114. template<typename _Tp, std::size_t _Nm> inline
  1115. Mat::operator std::array<_Tp, _Nm>() const
  1116. {
  1117. std::array<_Tp, _Nm> v;
  1118. copyTo(v);
  1119. return v;
  1120. }
  1121. template<typename _Tp, int n> inline
  1122. Mat::operator Vec<_Tp, n>() const
  1123. {
  1124. CV_Assert( data && dims <= 2 && (rows == 1 || cols == 1) &&
  1125. rows + cols - 1 == n && channels() == 1 );
  1126. if( isContinuous() && type() == traits::Type<_Tp>::value )
  1127. return Vec<_Tp, n>((_Tp*)data);
  1128. Vec<_Tp, n> v;
  1129. Mat tmp(rows, cols, traits::Type<_Tp>::value, v.val);
  1130. convertTo(tmp, tmp.type());
  1131. return v;
  1132. }
  1133. template<typename _Tp, int m, int n> inline
  1134. Mat::operator Matx<_Tp, m, n>() const
  1135. {
  1136. CV_Assert( data && dims <= 2 && rows == m && cols == n && channels() == 1 );
  1137. if( isContinuous() && type() == traits::Type<_Tp>::value )
  1138. return Matx<_Tp, m, n>((_Tp*)data);
  1139. Matx<_Tp, m, n> mtx;
  1140. Mat tmp(rows, cols, traits::Type<_Tp>::value, mtx.val);
  1141. convertTo(tmp, tmp.type());
  1142. return mtx;
  1143. }
  1144. template<typename _Tp> inline
  1145. void Mat::push_back(const _Tp& elem)
  1146. {
  1147. if( !data )
  1148. {
  1149. *this = Mat(1, 1, traits::Type<_Tp>::value, (void*)&elem).clone();
  1150. return;
  1151. }
  1152. CV_Assert(traits::Type<_Tp>::value == type() && cols == 1
  1153. /* && dims == 2 (cols == 1 implies dims == 2) */);
  1154. const uchar* tmp = dataend + step[0];
  1155. if( !isSubmatrix() && isContinuous() && tmp <= datalimit )
  1156. {
  1157. *(_Tp*)(data + (size.p[0]++) * step.p[0]) = elem;
  1158. dataend = tmp;
  1159. }
  1160. else
  1161. push_back_(&elem);
  1162. }
  1163. template<typename _Tp> inline
  1164. void Mat::push_back(const Mat_<_Tp>& m)
  1165. {
  1166. push_back((const Mat&)m);
  1167. }
  1168. template<> inline
  1169. void Mat::push_back(const MatExpr& expr)
  1170. {
  1171. push_back(static_cast<Mat>(expr));
  1172. }
  1173. template<typename _Tp> inline
  1174. void Mat::push_back(const std::vector<_Tp>& v)
  1175. {
  1176. push_back(Mat(v));
  1177. }
  1178. inline
  1179. Mat::Mat(Mat&& m)
  1180. : flags(m.flags), dims(m.dims), rows(m.rows), cols(m.cols), data(m.data),
  1181. datastart(m.datastart), dataend(m.dataend), datalimit(m.datalimit), allocator(m.allocator),
  1182. u(m.u), size(&rows)
  1183. {
  1184. if (m.dims <= 2) // move new step/size info
  1185. {
  1186. step[0] = m.step[0];
  1187. step[1] = m.step[1];
  1188. }
  1189. else
  1190. {
  1191. CV_DbgAssert(m.step.p != m.step.buf);
  1192. step.p = m.step.p;
  1193. size.p = m.size.p;
  1194. m.step.p = m.step.buf;
  1195. m.size.p = &m.rows;
  1196. }
  1197. m.flags = MAGIC_VAL; m.dims = m.rows = m.cols = 0;
  1198. m.data = NULL; m.datastart = NULL; m.dataend = NULL; m.datalimit = NULL;
  1199. m.allocator = NULL;
  1200. m.u = NULL;
  1201. }
  1202. inline
  1203. Mat& Mat::operator = (Mat&& m)
  1204. {
  1205. if (this == &m)
  1206. return *this;
  1207. release();
  1208. flags = m.flags; dims = m.dims; rows = m.rows; cols = m.cols; data = m.data;
  1209. datastart = m.datastart; dataend = m.dataend; datalimit = m.datalimit; allocator = m.allocator;
  1210. u = m.u;
  1211. if (step.p != step.buf) // release self step/size
  1212. {
  1213. fastFree(step.p);
  1214. step.p = step.buf;
  1215. size.p = &rows;
  1216. }
  1217. if (m.dims <= 2) // move new step/size info
  1218. {
  1219. step[0] = m.step[0];
  1220. step[1] = m.step[1];
  1221. }
  1222. else
  1223. {
  1224. CV_DbgAssert(m.step.p != m.step.buf);
  1225. step.p = m.step.p;
  1226. size.p = m.size.p;
  1227. m.step.p = m.step.buf;
  1228. m.size.p = &m.rows;
  1229. }
  1230. m.flags = MAGIC_VAL; m.dims = m.rows = m.cols = 0;
  1231. m.data = NULL; m.datastart = NULL; m.dataend = NULL; m.datalimit = NULL;
  1232. m.allocator = NULL;
  1233. m.u = NULL;
  1234. return *this;
  1235. }
  1236. ///////////////////////////// MatSize ////////////////////////////
  1237. inline
  1238. MatSize::MatSize(int* _p)
  1239. : p(_p) {}
  1240. inline
  1241. int MatSize::dims() const
  1242. {
  1243. return (p - 1)[0];
  1244. }
  1245. inline
  1246. Size MatSize::operator()() const
  1247. {
  1248. CV_DbgAssert(dims() <= 2);
  1249. return Size(p[1], p[0]);
  1250. }
  1251. inline
  1252. const int& MatSize::operator[](int i) const
  1253. {
  1254. CV_DbgAssert(i < dims());
  1255. #ifdef __OPENCV_BUILD
  1256. CV_DbgAssert(i >= 0);
  1257. #endif
  1258. return p[i];
  1259. }
  1260. inline
  1261. int& MatSize::operator[](int i)
  1262. {
  1263. CV_DbgAssert(i < dims());
  1264. #ifdef __OPENCV_BUILD
  1265. CV_DbgAssert(i >= 0);
  1266. #endif
  1267. return p[i];
  1268. }
  1269. inline
  1270. MatSize::operator const int*() const
  1271. {
  1272. return p;
  1273. }
  1274. inline
  1275. bool MatSize::operator == (const MatSize& sz) const
  1276. {
  1277. int d = dims();
  1278. int dsz = sz.dims();
  1279. if( d != dsz )
  1280. return false;
  1281. if( d == 2 )
  1282. return p[0] == sz.p[0] && p[1] == sz.p[1];
  1283. for( int i = 0; i < d; i++ )
  1284. if( p[i] != sz.p[i] )
  1285. return false;
  1286. return true;
  1287. }
  1288. inline
  1289. bool MatSize::operator != (const MatSize& sz) const
  1290. {
  1291. return !(*this == sz);
  1292. }
  1293. ///////////////////////////// MatStep ////////////////////////////
  1294. inline
  1295. MatStep::MatStep()
  1296. {
  1297. p = buf; p[0] = p[1] = 0;
  1298. }
  1299. inline
  1300. MatStep::MatStep(size_t s)
  1301. {
  1302. p = buf; p[0] = s; p[1] = 0;
  1303. }
  1304. inline
  1305. const size_t& MatStep::operator[](int i) const
  1306. {
  1307. return p[i];
  1308. }
  1309. inline
  1310. size_t& MatStep::operator[](int i)
  1311. {
  1312. return p[i];
  1313. }
  1314. inline MatStep::operator size_t() const
  1315. {
  1316. CV_DbgAssert( p == buf );
  1317. return buf[0];
  1318. }
  1319. inline MatStep& MatStep::operator = (size_t s)
  1320. {
  1321. CV_DbgAssert( p == buf );
  1322. buf[0] = s;
  1323. return *this;
  1324. }
  1325. ////////////////////////////// Mat_<_Tp> ////////////////////////////
  1326. template<typename _Tp> inline
  1327. Mat_<_Tp>::Mat_()
  1328. : Mat()
  1329. {
  1330. flags = (flags & ~CV_MAT_TYPE_MASK) + traits::Type<_Tp>::value;
  1331. }
  1332. template<typename _Tp> inline
  1333. Mat_<_Tp>::Mat_(int _rows, int _cols)
  1334. : Mat(_rows, _cols, traits::Type<_Tp>::value)
  1335. {
  1336. }
  1337. template<typename _Tp> inline
  1338. Mat_<_Tp>::Mat_(int _rows, int _cols, const _Tp& value)
  1339. : Mat(_rows, _cols, traits::Type<_Tp>::value)
  1340. {
  1341. *this = value;
  1342. }
  1343. template<typename _Tp> inline
  1344. Mat_<_Tp>::Mat_(Size _sz)
  1345. : Mat(_sz.height, _sz.width, traits::Type<_Tp>::value)
  1346. {}
  1347. template<typename _Tp> inline
  1348. Mat_<_Tp>::Mat_(Size _sz, const _Tp& value)
  1349. : Mat(_sz.height, _sz.width, traits::Type<_Tp>::value)
  1350. {
  1351. *this = value;
  1352. }
  1353. template<typename _Tp> inline
  1354. Mat_<_Tp>::Mat_(int _dims, const int* _sz)
  1355. : Mat(_dims, _sz, traits::Type<_Tp>::value)
  1356. {}
  1357. template<typename _Tp> inline
  1358. Mat_<_Tp>::Mat_(int _dims, const int* _sz, const _Tp& _s)
  1359. : Mat(_dims, _sz, traits::Type<_Tp>::value, Scalar(_s))
  1360. {}
  1361. template<typename _Tp> inline
  1362. Mat_<_Tp>::Mat_(int _dims, const int* _sz, _Tp* _data, const size_t* _steps)
  1363. : Mat(_dims, _sz, traits::Type<_Tp>::value, _data, _steps)
  1364. {}
  1365. template<typename _Tp> inline
  1366. Mat_<_Tp>::Mat_(const Mat_<_Tp>& m, const Range* ranges)
  1367. : Mat(m, ranges)
  1368. {}
  1369. template<typename _Tp> inline
  1370. Mat_<_Tp>::Mat_(const Mat_<_Tp>& m, const std::vector<Range>& ranges)
  1371. : Mat(m, ranges)
  1372. {}
  1373. template<typename _Tp> inline
  1374. Mat_<_Tp>::Mat_(const Mat& m)
  1375. : Mat()
  1376. {
  1377. flags = (flags & ~CV_MAT_TYPE_MASK) + traits::Type<_Tp>::value;
  1378. *this = m;
  1379. }
  1380. template<typename _Tp> inline
  1381. Mat_<_Tp>::Mat_(const Mat_& m)
  1382. : Mat(m)
  1383. {}
  1384. template<typename _Tp> inline
  1385. Mat_<_Tp>::Mat_(int _rows, int _cols, _Tp* _data, size_t steps)
  1386. : Mat(_rows, _cols, traits::Type<_Tp>::value, _data, steps)
  1387. {}
  1388. template<typename _Tp> inline
  1389. Mat_<_Tp>::Mat_(const Mat_& m, const Range& _rowRange, const Range& _colRange)
  1390. : Mat(m, _rowRange, _colRange)
  1391. {}
  1392. template<typename _Tp> inline
  1393. Mat_<_Tp>::Mat_(const Mat_& m, const Rect& roi)
  1394. : Mat(m, roi)
  1395. {}
  1396. template<typename _Tp> template<int n> inline
  1397. Mat_<_Tp>::Mat_(const Vec<typename DataType<_Tp>::channel_type, n>& vec, bool copyData)
  1398. : Mat(n / DataType<_Tp>::channels, 1, traits::Type<_Tp>::value, (void*)&vec)
  1399. {
  1400. CV_Assert(n%DataType<_Tp>::channels == 0);
  1401. if( copyData )
  1402. *this = clone();
  1403. }
  1404. template<typename _Tp> template<int m, int n> inline
  1405. Mat_<_Tp>::Mat_(const Matx<typename DataType<_Tp>::channel_type, m, n>& M, bool copyData)
  1406. : Mat(m, n / DataType<_Tp>::channels, traits::Type<_Tp>::value, (void*)&M)
  1407. {
  1408. CV_Assert(n % DataType<_Tp>::channels == 0);
  1409. if( copyData )
  1410. *this = clone();
  1411. }
  1412. template<typename _Tp> inline
  1413. Mat_<_Tp>::Mat_(const Point_<typename DataType<_Tp>::channel_type>& pt, bool copyData)
  1414. : Mat(2 / DataType<_Tp>::channels, 1, traits::Type<_Tp>::value, (void*)&pt)
  1415. {
  1416. CV_Assert(2 % DataType<_Tp>::channels == 0);
  1417. if( copyData )
  1418. *this = clone();
  1419. }
  1420. template<typename _Tp> inline
  1421. Mat_<_Tp>::Mat_(const Point3_<typename DataType<_Tp>::channel_type>& pt, bool copyData)
  1422. : Mat(3 / DataType<_Tp>::channels, 1, traits::Type<_Tp>::value, (void*)&pt)
  1423. {
  1424. CV_Assert(3 % DataType<_Tp>::channels == 0);
  1425. if( copyData )
  1426. *this = clone();
  1427. }
  1428. template<typename _Tp> inline
  1429. Mat_<_Tp>::Mat_(const MatCommaInitializer_<_Tp>& commaInitializer)
  1430. : Mat(commaInitializer)
  1431. {}
  1432. template<typename _Tp> inline
  1433. Mat_<_Tp>::Mat_(const std::vector<_Tp>& vec, bool copyData)
  1434. : Mat(vec, copyData)
  1435. {}
  1436. template<typename _Tp> inline
  1437. Mat_<_Tp>::Mat_(std::initializer_list<_Tp> list)
  1438. : Mat(list)
  1439. {}
  1440. template<typename _Tp> inline
  1441. Mat_<_Tp>::Mat_(const std::initializer_list<int> sizes, std::initializer_list<_Tp> list)
  1442. : Mat(sizes, list)
  1443. {}
  1444. template<typename _Tp> template<std::size_t _Nm> inline
  1445. Mat_<_Tp>::Mat_(const std::array<_Tp, _Nm>& arr, bool copyData)
  1446. : Mat(arr, copyData)
  1447. {}
  1448. template<typename _Tp> inline
  1449. Mat_<_Tp>& Mat_<_Tp>::operator = (const Mat& m)
  1450. {
  1451. if( traits::Type<_Tp>::value == m.type() )
  1452. {
  1453. Mat::operator = (m);
  1454. return *this;
  1455. }
  1456. if( traits::Depth<_Tp>::value == m.depth() )
  1457. {
  1458. return (*this = m.reshape(DataType<_Tp>::channels, m.dims, 0));
  1459. }
  1460. CV_Assert(DataType<_Tp>::channels == m.channels() || m.empty());
  1461. m.convertTo(*this, type());
  1462. return *this;
  1463. }
  1464. template<typename _Tp> inline
  1465. Mat_<_Tp>& Mat_<_Tp>::operator = (const Mat_& m)
  1466. {
  1467. Mat::operator=(m);
  1468. return *this;
  1469. }
  1470. template<typename _Tp> inline
  1471. Mat_<_Tp>& Mat_<_Tp>::operator = (const _Tp& s)
  1472. {
  1473. typedef typename DataType<_Tp>::vec_type VT;
  1474. Mat::operator=(Scalar((const VT&)s));
  1475. return *this;
  1476. }
  1477. template<typename _Tp> inline
  1478. void Mat_<_Tp>::create(int _rows, int _cols)
  1479. {
  1480. Mat::create(_rows, _cols, traits::Type<_Tp>::value);
  1481. }
  1482. template<typename _Tp> inline
  1483. void Mat_<_Tp>::create(Size _sz)
  1484. {
  1485. Mat::create(_sz, traits::Type<_Tp>::value);
  1486. }
  1487. template<typename _Tp> inline
  1488. void Mat_<_Tp>::create(int _dims, const int* _sz)
  1489. {
  1490. Mat::create(_dims, _sz, traits::Type<_Tp>::value);
  1491. }
  1492. template<typename _Tp> inline
  1493. void Mat_<_Tp>::release()
  1494. {
  1495. Mat::release();
  1496. #ifdef _DEBUG
  1497. flags = (flags & ~CV_MAT_TYPE_MASK) + traits::Type<_Tp>::value;
  1498. #endif
  1499. }
  1500. template<typename _Tp> inline
  1501. Mat_<_Tp> Mat_<_Tp>::cross(const Mat_& m) const
  1502. {
  1503. return Mat_<_Tp>(Mat::cross(m));
  1504. }
  1505. template<typename _Tp> template<typename T2> inline
  1506. Mat_<_Tp>::operator Mat_<T2>() const
  1507. {
  1508. return Mat_<T2>(*this);
  1509. }
  1510. template<typename _Tp> inline
  1511. Mat_<_Tp> Mat_<_Tp>::row(int y) const
  1512. {
  1513. return Mat_(*this, Range(y, y+1), Range::all());
  1514. }
  1515. template<typename _Tp> inline
  1516. Mat_<_Tp> Mat_<_Tp>::col(int x) const
  1517. {
  1518. return Mat_(*this, Range::all(), Range(x, x+1));
  1519. }
  1520. template<typename _Tp> inline
  1521. Mat_<_Tp> Mat_<_Tp>::diag(int d) const
  1522. {
  1523. return Mat_(Mat::diag(d));
  1524. }
  1525. template<typename _Tp> inline
  1526. Mat_<_Tp> Mat_<_Tp>::clone() const
  1527. {
  1528. return Mat_(Mat::clone());
  1529. }
  1530. template<typename _Tp> inline
  1531. size_t Mat_<_Tp>::elemSize() const
  1532. {
  1533. CV_DbgAssert( Mat::elemSize() == sizeof(_Tp) );
  1534. return sizeof(_Tp);
  1535. }
  1536. template<typename _Tp> inline
  1537. size_t Mat_<_Tp>::elemSize1() const
  1538. {
  1539. CV_DbgAssert( Mat::elemSize1() == sizeof(_Tp) / DataType<_Tp>::channels );
  1540. return sizeof(_Tp) / DataType<_Tp>::channels;
  1541. }
  1542. template<typename _Tp> inline
  1543. int Mat_<_Tp>::type() const
  1544. {
  1545. CV_DbgAssert( Mat::type() == traits::Type<_Tp>::value );
  1546. return traits::Type<_Tp>::value;
  1547. }
  1548. template<typename _Tp> inline
  1549. int Mat_<_Tp>::depth() const
  1550. {
  1551. CV_DbgAssert( Mat::depth() == traits::Depth<_Tp>::value );
  1552. return traits::Depth<_Tp>::value;
  1553. }
  1554. template<typename _Tp> inline
  1555. int Mat_<_Tp>::channels() const
  1556. {
  1557. CV_DbgAssert( Mat::channels() == DataType<_Tp>::channels );
  1558. return DataType<_Tp>::channels;
  1559. }
  1560. template<typename _Tp> inline
  1561. size_t Mat_<_Tp>::stepT(int i) const
  1562. {
  1563. return step.p[i] / elemSize();
  1564. }
  1565. template<typename _Tp> inline
  1566. size_t Mat_<_Tp>::step1(int i) const
  1567. {
  1568. return step.p[i] / elemSize1();
  1569. }
  1570. template<typename _Tp> inline
  1571. Mat_<_Tp>& Mat_<_Tp>::adjustROI( int dtop, int dbottom, int dleft, int dright )
  1572. {
  1573. return (Mat_<_Tp>&)(Mat::adjustROI(dtop, dbottom, dleft, dright));
  1574. }
  1575. template<typename _Tp> inline
  1576. Mat_<_Tp> Mat_<_Tp>::operator()( const Range& _rowRange, const Range& _colRange ) const
  1577. {
  1578. return Mat_<_Tp>(*this, _rowRange, _colRange);
  1579. }
  1580. template<typename _Tp> inline
  1581. Mat_<_Tp> Mat_<_Tp>::operator()( const Rect& roi ) const
  1582. {
  1583. return Mat_<_Tp>(*this, roi);
  1584. }
  1585. template<typename _Tp> inline
  1586. Mat_<_Tp> Mat_<_Tp>::operator()( const Range* ranges ) const
  1587. {
  1588. return Mat_<_Tp>(*this, ranges);
  1589. }
  1590. template<typename _Tp> inline
  1591. Mat_<_Tp> Mat_<_Tp>::operator()(const std::vector<Range>& ranges) const
  1592. {
  1593. return Mat_<_Tp>(*this, ranges);
  1594. }
  1595. template<typename _Tp> inline
  1596. _Tp* Mat_<_Tp>::operator [](int y)
  1597. {
  1598. CV_DbgAssert( 0 <= y && y < size.p[0] );
  1599. return (_Tp*)(data + y*step.p[0]);
  1600. }
  1601. template<typename _Tp> inline
  1602. const _Tp* Mat_<_Tp>::operator [](int y) const
  1603. {
  1604. CV_DbgAssert( 0 <= y && y < size.p[0] );
  1605. return (const _Tp*)(data + y*step.p[0]);
  1606. }
  1607. template<typename _Tp> inline
  1608. _Tp& Mat_<_Tp>::operator ()(int i0, int i1)
  1609. {
  1610. CV_DbgAssert(dims <= 2);
  1611. CV_DbgAssert(data);
  1612. CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
  1613. CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
  1614. CV_DbgAssert(type() == traits::Type<_Tp>::value);
  1615. return ((_Tp*)(data + step.p[0] * i0))[i1];
  1616. }
  1617. template<typename _Tp> inline
  1618. const _Tp& Mat_<_Tp>::operator ()(int i0, int i1) const
  1619. {
  1620. CV_DbgAssert(dims <= 2);
  1621. CV_DbgAssert(data);
  1622. CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
  1623. CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
  1624. CV_DbgAssert(type() == traits::Type<_Tp>::value);
  1625. return ((const _Tp*)(data + step.p[0] * i0))[i1];
  1626. }
  1627. template<typename _Tp> inline
  1628. _Tp& Mat_<_Tp>::operator ()(Point pt)
  1629. {
  1630. CV_DbgAssert(dims <= 2);
  1631. CV_DbgAssert(data);
  1632. CV_DbgAssert((unsigned)pt.y < (unsigned)size.p[0]);
  1633. CV_DbgAssert((unsigned)pt.x < (unsigned)size.p[1]);
  1634. CV_DbgAssert(type() == traits::Type<_Tp>::value);
  1635. return ((_Tp*)(data + step.p[0] * pt.y))[pt.x];
  1636. }
  1637. template<typename _Tp> inline
  1638. const _Tp& Mat_<_Tp>::operator ()(Point pt) const
  1639. {
  1640. CV_DbgAssert(dims <= 2);
  1641. CV_DbgAssert(data);
  1642. CV_DbgAssert((unsigned)pt.y < (unsigned)size.p[0]);
  1643. CV_DbgAssert((unsigned)pt.x < (unsigned)size.p[1]);
  1644. CV_DbgAssert(type() == traits::Type<_Tp>::value);
  1645. return ((const _Tp*)(data + step.p[0] * pt.y))[pt.x];
  1646. }
  1647. template<typename _Tp> inline
  1648. _Tp& Mat_<_Tp>::operator ()(const int* idx)
  1649. {
  1650. return Mat::at<_Tp>(idx);
  1651. }
  1652. template<typename _Tp> inline
  1653. const _Tp& Mat_<_Tp>::operator ()(const int* idx) const
  1654. {
  1655. return Mat::at<_Tp>(idx);
  1656. }
  1657. template<typename _Tp> template<int n> inline
  1658. _Tp& Mat_<_Tp>::operator ()(const Vec<int, n>& idx)
  1659. {
  1660. return Mat::at<_Tp>(idx);
  1661. }
  1662. template<typename _Tp> template<int n> inline
  1663. const _Tp& Mat_<_Tp>::operator ()(const Vec<int, n>& idx) const
  1664. {
  1665. return Mat::at<_Tp>(idx);
  1666. }
  1667. template<typename _Tp> inline
  1668. _Tp& Mat_<_Tp>::operator ()(int i0)
  1669. {
  1670. return this->at<_Tp>(i0);
  1671. }
  1672. template<typename _Tp> inline
  1673. const _Tp& Mat_<_Tp>::operator ()(int i0) const
  1674. {
  1675. return this->at<_Tp>(i0);
  1676. }
  1677. template<typename _Tp> inline
  1678. _Tp& Mat_<_Tp>::operator ()(int i0, int i1, int i2)
  1679. {
  1680. return this->at<_Tp>(i0, i1, i2);
  1681. }
  1682. template<typename _Tp> inline
  1683. const _Tp& Mat_<_Tp>::operator ()(int i0, int i1, int i2) const
  1684. {
  1685. return this->at<_Tp>(i0, i1, i2);
  1686. }
  1687. template<typename _Tp> inline
  1688. Mat_<_Tp>::operator std::vector<_Tp>() const
  1689. {
  1690. std::vector<_Tp> v;
  1691. copyTo(v);
  1692. return v;
  1693. }
  1694. template<typename _Tp> template<std::size_t _Nm> inline
  1695. Mat_<_Tp>::operator std::array<_Tp, _Nm>() const
  1696. {
  1697. std::array<_Tp, _Nm> a;
  1698. copyTo(a);
  1699. return a;
  1700. }
  1701. template<typename _Tp> template<int n> inline
  1702. Mat_<_Tp>::operator Vec<typename DataType<_Tp>::channel_type, n>() const
  1703. {
  1704. CV_Assert(n % DataType<_Tp>::channels == 0);
  1705. #if defined _MSC_VER
  1706. const Mat* pMat = (const Mat*)this; // workaround for MSVS <= 2012 compiler bugs (but GCC 4.6 dislikes this workaround)
  1707. return pMat->operator Vec<typename DataType<_Tp>::channel_type, n>();
  1708. #else
  1709. return this->Mat::operator Vec<typename DataType<_Tp>::channel_type, n>();
  1710. #endif
  1711. }
  1712. template<typename _Tp> template<int m, int n> inline
  1713. Mat_<_Tp>::operator Matx<typename DataType<_Tp>::channel_type, m, n>() const
  1714. {
  1715. CV_Assert(n % DataType<_Tp>::channels == 0);
  1716. #if defined _MSC_VER
  1717. const Mat* pMat = (const Mat*)this; // workaround for MSVS <= 2012 compiler bugs (but GCC 4.6 dislikes this workaround)
  1718. Matx<typename DataType<_Tp>::channel_type, m, n> res = pMat->operator Matx<typename DataType<_Tp>::channel_type, m, n>();
  1719. return res;
  1720. #else
  1721. Matx<typename DataType<_Tp>::channel_type, m, n> res = this->Mat::operator Matx<typename DataType<_Tp>::channel_type, m, n>();
  1722. return res;
  1723. #endif
  1724. }
  1725. template<typename _Tp> inline
  1726. MatConstIterator_<_Tp> Mat_<_Tp>::begin() const
  1727. {
  1728. return Mat::begin<_Tp>();
  1729. }
  1730. template<typename _Tp> inline
  1731. MatConstIterator_<_Tp> Mat_<_Tp>::end() const
  1732. {
  1733. return Mat::end<_Tp>();
  1734. }
  1735. template<typename _Tp> inline
  1736. MatIterator_<_Tp> Mat_<_Tp>::begin()
  1737. {
  1738. return Mat::begin<_Tp>();
  1739. }
  1740. template<typename _Tp> inline
  1741. MatIterator_<_Tp> Mat_<_Tp>::end()
  1742. {
  1743. return Mat::end<_Tp>();
  1744. }
  1745. template<typename _Tp> template<typename Functor> inline
  1746. void Mat_<_Tp>::forEach(const Functor& operation) {
  1747. Mat::forEach<_Tp, Functor>(operation);
  1748. }
  1749. template<typename _Tp> template<typename Functor> inline
  1750. void Mat_<_Tp>::forEach(const Functor& operation) const {
  1751. Mat::forEach<_Tp, Functor>(operation);
  1752. }
  1753. template<typename _Tp> inline
  1754. Mat_<_Tp>::Mat_(Mat_&& m)
  1755. : Mat(m)
  1756. {
  1757. }
  1758. template<typename _Tp> inline
  1759. Mat_<_Tp>& Mat_<_Tp>::operator = (Mat_&& m)
  1760. {
  1761. Mat::operator = (std::move(m));
  1762. return *this;
  1763. }
  1764. template<typename _Tp> inline
  1765. Mat_<_Tp>::Mat_(Mat&& m)
  1766. : Mat()
  1767. {
  1768. flags = (flags & ~CV_MAT_TYPE_MASK) + traits::Type<_Tp>::value;
  1769. *this = m;
  1770. }
  1771. template<typename _Tp> inline
  1772. Mat_<_Tp>& Mat_<_Tp>::operator = (Mat&& m)
  1773. {
  1774. if( traits::Type<_Tp>::value == m.type() )
  1775. {
  1776. Mat::operator = ((Mat&&)m);
  1777. return *this;
  1778. }
  1779. if( traits::Depth<_Tp>::value == m.depth() )
  1780. {
  1781. Mat::operator = ((Mat&&)m.reshape(DataType<_Tp>::channels, m.dims, 0));
  1782. return *this;
  1783. }
  1784. CV_DbgAssert(DataType<_Tp>::channels == m.channels());
  1785. m.convertTo(*this, type());
  1786. return *this;
  1787. }
  1788. template<typename _Tp> inline
  1789. Mat_<_Tp>::Mat_(MatExpr&& e)
  1790. : Mat()
  1791. {
  1792. flags = (flags & ~CV_MAT_TYPE_MASK) + traits::Type<_Tp>::value;
  1793. *this = Mat(e);
  1794. }
  1795. ///////////////////////////// SparseMat /////////////////////////////
  1796. inline
  1797. SparseMat::SparseMat()
  1798. : flags(MAGIC_VAL), hdr(0)
  1799. {}
  1800. inline
  1801. SparseMat::SparseMat(int _dims, const int* _sizes, int _type)
  1802. : flags(MAGIC_VAL), hdr(0)
  1803. {
  1804. create(_dims, _sizes, _type);
  1805. }
  1806. inline
  1807. SparseMat::SparseMat(const SparseMat& m)
  1808. : flags(m.flags), hdr(m.hdr)
  1809. {
  1810. addref();
  1811. }
  1812. inline
  1813. SparseMat::~SparseMat()
  1814. {
  1815. release();
  1816. }
  1817. inline
  1818. SparseMat& SparseMat::operator = (const SparseMat& m)
  1819. {
  1820. if( this != &m )
  1821. {
  1822. if( m.hdr )
  1823. CV_XADD(&m.hdr->refcount, 1);
  1824. release();
  1825. flags = m.flags;
  1826. hdr = m.hdr;
  1827. }
  1828. return *this;
  1829. }
  1830. inline
  1831. SparseMat& SparseMat::operator = (const Mat& m)
  1832. {
  1833. return (*this = SparseMat(m));
  1834. }
  1835. inline
  1836. SparseMat SparseMat::clone() const
  1837. {
  1838. SparseMat temp;
  1839. this->copyTo(temp);
  1840. return temp;
  1841. }
  1842. inline
  1843. void SparseMat::assignTo( SparseMat& m, int _type ) const
  1844. {
  1845. if( _type < 0 )
  1846. m = *this;
  1847. else
  1848. convertTo(m, _type);
  1849. }
  1850. inline
  1851. void SparseMat::addref()
  1852. {
  1853. if( hdr )
  1854. CV_XADD(&hdr->refcount, 1);
  1855. }
  1856. inline
  1857. void SparseMat::release()
  1858. {
  1859. if( hdr && CV_XADD(&hdr->refcount, -1) == 1 )
  1860. delete hdr;
  1861. hdr = 0;
  1862. }
  1863. inline
  1864. size_t SparseMat::elemSize() const
  1865. {
  1866. return CV_ELEM_SIZE(flags);
  1867. }
  1868. inline
  1869. size_t SparseMat::elemSize1() const
  1870. {
  1871. return CV_ELEM_SIZE1(flags);
  1872. }
  1873. inline
  1874. int SparseMat::type() const
  1875. {
  1876. return CV_MAT_TYPE(flags);
  1877. }
  1878. inline
  1879. int SparseMat::depth() const
  1880. {
  1881. return CV_MAT_DEPTH(flags);
  1882. }
  1883. inline
  1884. int SparseMat::channels() const
  1885. {
  1886. return CV_MAT_CN(flags);
  1887. }
  1888. inline
  1889. const int* SparseMat::size() const
  1890. {
  1891. return hdr ? hdr->size : 0;
  1892. }
  1893. inline
  1894. int SparseMat::size(int i) const
  1895. {
  1896. if( hdr )
  1897. {
  1898. CV_DbgAssert((unsigned)i < (unsigned)hdr->dims);
  1899. return hdr->size[i];
  1900. }
  1901. return 0;
  1902. }
  1903. inline
  1904. int SparseMat::dims() const
  1905. {
  1906. return hdr ? hdr->dims : 0;
  1907. }
  1908. inline
  1909. size_t SparseMat::nzcount() const
  1910. {
  1911. return hdr ? hdr->nodeCount : 0;
  1912. }
  1913. inline
  1914. size_t SparseMat::hash(int i0) const
  1915. {
  1916. return (size_t)i0;
  1917. }
  1918. inline
  1919. size_t SparseMat::hash(int i0, int i1) const
  1920. {
  1921. return (size_t)(unsigned)i0 * HASH_SCALE + (unsigned)i1;
  1922. }
  1923. inline
  1924. size_t SparseMat::hash(int i0, int i1, int i2) const
  1925. {
  1926. return ((size_t)(unsigned)i0 * HASH_SCALE + (unsigned)i1) * HASH_SCALE + (unsigned)i2;
  1927. }
  1928. inline
  1929. size_t SparseMat::hash(const int* idx) const
  1930. {
  1931. size_t h = (unsigned)idx[0];
  1932. if( !hdr )
  1933. return 0;
  1934. int d = hdr->dims;
  1935. for(int i = 1; i < d; i++ )
  1936. h = h * HASH_SCALE + (unsigned)idx[i];
  1937. return h;
  1938. }
  1939. template<typename _Tp> inline
  1940. _Tp& SparseMat::ref(int i0, size_t* hashval)
  1941. {
  1942. return *(_Tp*)((SparseMat*)this)->ptr(i0, true, hashval);
  1943. }
  1944. template<typename _Tp> inline
  1945. _Tp& SparseMat::ref(int i0, int i1, size_t* hashval)
  1946. {
  1947. return *(_Tp*)((SparseMat*)this)->ptr(i0, i1, true, hashval);
  1948. }
  1949. template<typename _Tp> inline
  1950. _Tp& SparseMat::ref(int i0, int i1, int i2, size_t* hashval)
  1951. {
  1952. return *(_Tp*)((SparseMat*)this)->ptr(i0, i1, i2, true, hashval);
  1953. }
  1954. template<typename _Tp> inline
  1955. _Tp& SparseMat::ref(const int* idx, size_t* hashval)
  1956. {
  1957. return *(_Tp*)((SparseMat*)this)->ptr(idx, true, hashval);
  1958. }
  1959. template<typename _Tp> inline
  1960. _Tp SparseMat::value(int i0, size_t* hashval) const
  1961. {
  1962. const _Tp* p = (const _Tp*)((SparseMat*)this)->ptr(i0, false, hashval);
  1963. return p ? *p : _Tp();
  1964. }
  1965. template<typename _Tp> inline
  1966. _Tp SparseMat::value(int i0, int i1, size_t* hashval) const
  1967. {
  1968. const _Tp* p = (const _Tp*)((SparseMat*)this)->ptr(i0, i1, false, hashval);
  1969. return p ? *p : _Tp();
  1970. }
  1971. template<typename _Tp> inline
  1972. _Tp SparseMat::value(int i0, int i1, int i2, size_t* hashval) const
  1973. {
  1974. const _Tp* p = (const _Tp*)((SparseMat*)this)->ptr(i0, i1, i2, false, hashval);
  1975. return p ? *p : _Tp();
  1976. }
  1977. template<typename _Tp> inline
  1978. _Tp SparseMat::value(const int* idx, size_t* hashval) const
  1979. {
  1980. const _Tp* p = (const _Tp*)((SparseMat*)this)->ptr(idx, false, hashval);
  1981. return p ? *p : _Tp();
  1982. }
  1983. template<typename _Tp> inline
  1984. const _Tp* SparseMat::find(int i0, size_t* hashval) const
  1985. {
  1986. return (const _Tp*)((SparseMat*)this)->ptr(i0, false, hashval);
  1987. }
  1988. template<typename _Tp> inline
  1989. const _Tp* SparseMat::find(int i0, int i1, size_t* hashval) const
  1990. {
  1991. return (const _Tp*)((SparseMat*)this)->ptr(i0, i1, false, hashval);
  1992. }
  1993. template<typename _Tp> inline
  1994. const _Tp* SparseMat::find(int i0, int i1, int i2, size_t* hashval) const
  1995. {
  1996. return (const _Tp*)((SparseMat*)this)->ptr(i0, i1, i2, false, hashval);
  1997. }
  1998. template<typename _Tp> inline
  1999. const _Tp* SparseMat::find(const int* idx, size_t* hashval) const
  2000. {
  2001. return (const _Tp*)((SparseMat*)this)->ptr(idx, false, hashval);
  2002. }
  2003. template<typename _Tp> inline
  2004. _Tp& SparseMat::value(Node* n)
  2005. {
  2006. return *(_Tp*)((uchar*)n + hdr->valueOffset);
  2007. }
  2008. template<typename _Tp> inline
  2009. const _Tp& SparseMat::value(const Node* n) const
  2010. {
  2011. return *(const _Tp*)((const uchar*)n + hdr->valueOffset);
  2012. }
  2013. inline
  2014. SparseMat::Node* SparseMat::node(size_t nidx)
  2015. {
  2016. return (Node*)(void*)&hdr->pool[nidx];
  2017. }
  2018. inline
  2019. const SparseMat::Node* SparseMat::node(size_t nidx) const
  2020. {
  2021. return (const Node*)(const void*)&hdr->pool[nidx];
  2022. }
  2023. inline
  2024. SparseMatIterator SparseMat::begin()
  2025. {
  2026. return SparseMatIterator(this);
  2027. }
  2028. inline
  2029. SparseMatConstIterator SparseMat::begin() const
  2030. {
  2031. return SparseMatConstIterator(this);
  2032. }
  2033. inline
  2034. SparseMatIterator SparseMat::end()
  2035. {
  2036. SparseMatIterator it(this);
  2037. it.seekEnd();
  2038. return it;
  2039. }
  2040. inline
  2041. SparseMatConstIterator SparseMat::end() const
  2042. {
  2043. SparseMatConstIterator it(this);
  2044. it.seekEnd();
  2045. return it;
  2046. }
  2047. template<typename _Tp> inline
  2048. SparseMatIterator_<_Tp> SparseMat::begin()
  2049. {
  2050. return SparseMatIterator_<_Tp>(this);
  2051. }
  2052. template<typename _Tp> inline
  2053. SparseMatConstIterator_<_Tp> SparseMat::begin() const
  2054. {
  2055. return SparseMatConstIterator_<_Tp>(this);
  2056. }
  2057. template<typename _Tp> inline
  2058. SparseMatIterator_<_Tp> SparseMat::end()
  2059. {
  2060. SparseMatIterator_<_Tp> it(this);
  2061. it.seekEnd();
  2062. return it;
  2063. }
  2064. template<typename _Tp> inline
  2065. SparseMatConstIterator_<_Tp> SparseMat::end() const
  2066. {
  2067. SparseMatConstIterator_<_Tp> it(this);
  2068. it.seekEnd();
  2069. return it;
  2070. }
  2071. ///////////////////////////// SparseMat_ ////////////////////////////
  2072. template<typename _Tp> inline
  2073. SparseMat_<_Tp>::SparseMat_()
  2074. {
  2075. flags = MAGIC_VAL + traits::Type<_Tp>::value;
  2076. }
  2077. template<typename _Tp> inline
  2078. SparseMat_<_Tp>::SparseMat_(int _dims, const int* _sizes)
  2079. : SparseMat(_dims, _sizes, traits::Type<_Tp>::value)
  2080. {}
  2081. template<typename _Tp> inline
  2082. SparseMat_<_Tp>::SparseMat_(const SparseMat& m)
  2083. {
  2084. if( m.type() == traits::Type<_Tp>::value )
  2085. *this = (const SparseMat_<_Tp>&)m;
  2086. else
  2087. m.convertTo(*this, traits::Type<_Tp>::value);
  2088. }
  2089. template<typename _Tp> inline
  2090. SparseMat_<_Tp>::SparseMat_(const SparseMat_<_Tp>& m)
  2091. {
  2092. this->flags = m.flags;
  2093. this->hdr = m.hdr;
  2094. if( this->hdr )
  2095. CV_XADD(&this->hdr->refcount, 1);
  2096. }
  2097. template<typename _Tp> inline
  2098. SparseMat_<_Tp>::SparseMat_(const Mat& m)
  2099. {
  2100. SparseMat sm(m);
  2101. *this = sm;
  2102. }
  2103. template<typename _Tp> inline
  2104. SparseMat_<_Tp>& SparseMat_<_Tp>::operator = (const SparseMat_<_Tp>& m)
  2105. {
  2106. if( this != &m )
  2107. {
  2108. if( m.hdr ) CV_XADD(&m.hdr->refcount, 1);
  2109. release();
  2110. flags = m.flags;
  2111. hdr = m.hdr;
  2112. }
  2113. return *this;
  2114. }
  2115. template<typename _Tp> inline
  2116. SparseMat_<_Tp>& SparseMat_<_Tp>::operator = (const SparseMat& m)
  2117. {
  2118. if( m.type() == traits::Type<_Tp>::value )
  2119. return (*this = (const SparseMat_<_Tp>&)m);
  2120. m.convertTo(*this, traits::Type<_Tp>::value);
  2121. return *this;
  2122. }
  2123. template<typename _Tp> inline
  2124. SparseMat_<_Tp>& SparseMat_<_Tp>::operator = (const Mat& m)
  2125. {
  2126. return (*this = SparseMat(m));
  2127. }
  2128. template<typename _Tp> inline
  2129. SparseMat_<_Tp> SparseMat_<_Tp>::clone() const
  2130. {
  2131. SparseMat_<_Tp> m;
  2132. this->copyTo(m);
  2133. return m;
  2134. }
  2135. template<typename _Tp> inline
  2136. void SparseMat_<_Tp>::create(int _dims, const int* _sizes)
  2137. {
  2138. SparseMat::create(_dims, _sizes, traits::Type<_Tp>::value);
  2139. }
  2140. template<typename _Tp> inline
  2141. int SparseMat_<_Tp>::type() const
  2142. {
  2143. return traits::Type<_Tp>::value;
  2144. }
  2145. template<typename _Tp> inline
  2146. int SparseMat_<_Tp>::depth() const
  2147. {
  2148. return traits::Depth<_Tp>::value;
  2149. }
  2150. template<typename _Tp> inline
  2151. int SparseMat_<_Tp>::channels() const
  2152. {
  2153. return DataType<_Tp>::channels;
  2154. }
  2155. template<typename _Tp> inline
  2156. _Tp& SparseMat_<_Tp>::ref(int i0, size_t* hashval)
  2157. {
  2158. return SparseMat::ref<_Tp>(i0, hashval);
  2159. }
  2160. template<typename _Tp> inline
  2161. _Tp SparseMat_<_Tp>::operator()(int i0, size_t* hashval) const
  2162. {
  2163. return SparseMat::value<_Tp>(i0, hashval);
  2164. }
  2165. template<typename _Tp> inline
  2166. _Tp& SparseMat_<_Tp>::ref(int i0, int i1, size_t* hashval)
  2167. {
  2168. return SparseMat::ref<_Tp>(i0, i1, hashval);
  2169. }
  2170. template<typename _Tp> inline
  2171. _Tp SparseMat_<_Tp>::operator()(int i0, int i1, size_t* hashval) const
  2172. {
  2173. return SparseMat::value<_Tp>(i0, i1, hashval);
  2174. }
  2175. template<typename _Tp> inline
  2176. _Tp& SparseMat_<_Tp>::ref(int i0, int i1, int i2, size_t* hashval)
  2177. {
  2178. return SparseMat::ref<_Tp>(i0, i1, i2, hashval);
  2179. }
  2180. template<typename _Tp> inline
  2181. _Tp SparseMat_<_Tp>::operator()(int i0, int i1, int i2, size_t* hashval) const
  2182. {
  2183. return SparseMat::value<_Tp>(i0, i1, i2, hashval);
  2184. }
  2185. template<typename _Tp> inline
  2186. _Tp& SparseMat_<_Tp>::ref(const int* idx, size_t* hashval)
  2187. {
  2188. return SparseMat::ref<_Tp>(idx, hashval);
  2189. }
  2190. template<typename _Tp> inline
  2191. _Tp SparseMat_<_Tp>::operator()(const int* idx, size_t* hashval) const
  2192. {
  2193. return SparseMat::value<_Tp>(idx, hashval);
  2194. }
  2195. template<typename _Tp> inline
  2196. SparseMatIterator_<_Tp> SparseMat_<_Tp>::begin()
  2197. {
  2198. return SparseMatIterator_<_Tp>(this);
  2199. }
  2200. template<typename _Tp> inline
  2201. SparseMatConstIterator_<_Tp> SparseMat_<_Tp>::begin() const
  2202. {
  2203. return SparseMatConstIterator_<_Tp>(this);
  2204. }
  2205. template<typename _Tp> inline
  2206. SparseMatIterator_<_Tp> SparseMat_<_Tp>::end()
  2207. {
  2208. SparseMatIterator_<_Tp> it(this);
  2209. it.seekEnd();
  2210. return it;
  2211. }
  2212. template<typename _Tp> inline
  2213. SparseMatConstIterator_<_Tp> SparseMat_<_Tp>::end() const
  2214. {
  2215. SparseMatConstIterator_<_Tp> it(this);
  2216. it.seekEnd();
  2217. return it;
  2218. }
  2219. ////////////////////////// MatConstIterator /////////////////////////
  2220. inline
  2221. MatConstIterator::MatConstIterator()
  2222. : m(0), elemSize(0), ptr(0), sliceStart(0), sliceEnd(0)
  2223. {}
  2224. inline
  2225. MatConstIterator::MatConstIterator(const Mat* _m)
  2226. : m(_m), elemSize(_m->elemSize()), ptr(0), sliceStart(0), sliceEnd(0)
  2227. {
  2228. if( m && m->isContinuous() )
  2229. {
  2230. sliceStart = m->ptr();
  2231. sliceEnd = sliceStart + m->total()*elemSize;
  2232. }
  2233. seek((const int*)0);
  2234. }
  2235. inline
  2236. MatConstIterator::MatConstIterator(const Mat* _m, int _row, int _col)
  2237. : m(_m), elemSize(_m->elemSize()), ptr(0), sliceStart(0), sliceEnd(0)
  2238. {
  2239. CV_Assert(m && m->dims <= 2);
  2240. if( m->isContinuous() )
  2241. {
  2242. sliceStart = m->ptr();
  2243. sliceEnd = sliceStart + m->total()*elemSize;
  2244. }
  2245. int idx[] = {_row, _col};
  2246. seek(idx);
  2247. }
  2248. inline
  2249. MatConstIterator::MatConstIterator(const Mat* _m, Point _pt)
  2250. : m(_m), elemSize(_m->elemSize()), ptr(0), sliceStart(0), sliceEnd(0)
  2251. {
  2252. CV_Assert(m && m->dims <= 2);
  2253. if( m->isContinuous() )
  2254. {
  2255. sliceStart = m->ptr();
  2256. sliceEnd = sliceStart + m->total()*elemSize;
  2257. }
  2258. int idx[] = {_pt.y, _pt.x};
  2259. seek(idx);
  2260. }
  2261. inline
  2262. MatConstIterator::MatConstIterator(const MatConstIterator& it)
  2263. : m(it.m), elemSize(it.elemSize), ptr(it.ptr), sliceStart(it.sliceStart), sliceEnd(it.sliceEnd)
  2264. {}
  2265. inline
  2266. MatConstIterator& MatConstIterator::operator = (const MatConstIterator& it )
  2267. {
  2268. m = it.m; elemSize = it.elemSize; ptr = it.ptr;
  2269. sliceStart = it.sliceStart; sliceEnd = it.sliceEnd;
  2270. return *this;
  2271. }
  2272. inline
  2273. const uchar* MatConstIterator::operator *() const
  2274. {
  2275. return ptr;
  2276. }
  2277. inline MatConstIterator& MatConstIterator::operator += (ptrdiff_t ofs)
  2278. {
  2279. if( !m || ofs == 0 )
  2280. return *this;
  2281. ptrdiff_t ofsb = ofs*elemSize;
  2282. ptr += ofsb;
  2283. if( ptr < sliceStart || sliceEnd <= ptr )
  2284. {
  2285. ptr -= ofsb;
  2286. seek(ofs, true);
  2287. }
  2288. return *this;
  2289. }
  2290. inline
  2291. MatConstIterator& MatConstIterator::operator -= (ptrdiff_t ofs)
  2292. {
  2293. return (*this += -ofs);
  2294. }
  2295. inline
  2296. MatConstIterator& MatConstIterator::operator --()
  2297. {
  2298. if( m && (ptr -= elemSize) < sliceStart )
  2299. {
  2300. ptr += elemSize;
  2301. seek(-1, true);
  2302. }
  2303. return *this;
  2304. }
  2305. inline
  2306. MatConstIterator MatConstIterator::operator --(int)
  2307. {
  2308. MatConstIterator b = *this;
  2309. *this += -1;
  2310. return b;
  2311. }
  2312. inline
  2313. MatConstIterator& MatConstIterator::operator ++()
  2314. {
  2315. if( m && (ptr += elemSize) >= sliceEnd )
  2316. {
  2317. ptr -= elemSize;
  2318. seek(1, true);
  2319. }
  2320. return *this;
  2321. }
  2322. inline MatConstIterator MatConstIterator::operator ++(int)
  2323. {
  2324. MatConstIterator b = *this;
  2325. *this += 1;
  2326. return b;
  2327. }
  2328. static inline
  2329. bool operator == (const MatConstIterator& a, const MatConstIterator& b)
  2330. {
  2331. return a.m == b.m && a.ptr == b.ptr;
  2332. }
  2333. static inline
  2334. bool operator != (const MatConstIterator& a, const MatConstIterator& b)
  2335. {
  2336. return !(a == b);
  2337. }
  2338. static inline
  2339. bool operator < (const MatConstIterator& a, const MatConstIterator& b)
  2340. {
  2341. return a.ptr < b.ptr;
  2342. }
  2343. static inline
  2344. bool operator > (const MatConstIterator& a, const MatConstIterator& b)
  2345. {
  2346. return a.ptr > b.ptr;
  2347. }
  2348. static inline
  2349. bool operator <= (const MatConstIterator& a, const MatConstIterator& b)
  2350. {
  2351. return a.ptr <= b.ptr;
  2352. }
  2353. static inline
  2354. bool operator >= (const MatConstIterator& a, const MatConstIterator& b)
  2355. {
  2356. return a.ptr >= b.ptr;
  2357. }
  2358. static inline
  2359. ptrdiff_t operator - (const MatConstIterator& b, const MatConstIterator& a)
  2360. {
  2361. if( a.m != b.m )
  2362. return ((size_t)(-1) >> 1);
  2363. if( a.sliceEnd == b.sliceEnd )
  2364. return (b.ptr - a.ptr)/static_cast<ptrdiff_t>(b.elemSize);
  2365. return b.lpos() - a.lpos();
  2366. }
  2367. static inline
  2368. MatConstIterator operator + (const MatConstIterator& a, ptrdiff_t ofs)
  2369. {
  2370. MatConstIterator b = a;
  2371. return b += ofs;
  2372. }
  2373. static inline
  2374. MatConstIterator operator + (ptrdiff_t ofs, const MatConstIterator& a)
  2375. {
  2376. MatConstIterator b = a;
  2377. return b += ofs;
  2378. }
  2379. static inline
  2380. MatConstIterator operator - (const MatConstIterator& a, ptrdiff_t ofs)
  2381. {
  2382. MatConstIterator b = a;
  2383. return b += -ofs;
  2384. }
  2385. inline
  2386. const uchar* MatConstIterator::operator [](ptrdiff_t i) const
  2387. {
  2388. return *(*this + i);
  2389. }
  2390. ///////////////////////// MatConstIterator_ /////////////////////////
  2391. template<typename _Tp> inline
  2392. MatConstIterator_<_Tp>::MatConstIterator_()
  2393. {}
  2394. template<typename _Tp> inline
  2395. MatConstIterator_<_Tp>::MatConstIterator_(const Mat_<_Tp>* _m)
  2396. : MatConstIterator(_m)
  2397. {}
  2398. template<typename _Tp> inline
  2399. MatConstIterator_<_Tp>::MatConstIterator_(const Mat_<_Tp>* _m, int _row, int _col)
  2400. : MatConstIterator(_m, _row, _col)
  2401. {}
  2402. template<typename _Tp> inline
  2403. MatConstIterator_<_Tp>::MatConstIterator_(const Mat_<_Tp>* _m, Point _pt)
  2404. : MatConstIterator(_m, _pt)
  2405. {}
  2406. template<typename _Tp> inline
  2407. MatConstIterator_<_Tp>::MatConstIterator_(const MatConstIterator_& it)
  2408. : MatConstIterator(it)
  2409. {}
  2410. template<typename _Tp> inline
  2411. MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator = (const MatConstIterator_& it )
  2412. {
  2413. MatConstIterator::operator = (it);
  2414. return *this;
  2415. }
  2416. template<typename _Tp> inline
  2417. const _Tp& MatConstIterator_<_Tp>::operator *() const
  2418. {
  2419. return *(_Tp*)(this->ptr);
  2420. }
  2421. template<typename _Tp> inline
  2422. MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator += (ptrdiff_t ofs)
  2423. {
  2424. MatConstIterator::operator += (ofs);
  2425. return *this;
  2426. }
  2427. template<typename _Tp> inline
  2428. MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator -= (ptrdiff_t ofs)
  2429. {
  2430. return (*this += -ofs);
  2431. }
  2432. template<typename _Tp> inline
  2433. MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator --()
  2434. {
  2435. MatConstIterator::operator --();
  2436. return *this;
  2437. }
  2438. template<typename _Tp> inline
  2439. MatConstIterator_<_Tp> MatConstIterator_<_Tp>::operator --(int)
  2440. {
  2441. MatConstIterator_ b = *this;
  2442. MatConstIterator::operator --();
  2443. return b;
  2444. }
  2445. template<typename _Tp> inline
  2446. MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator ++()
  2447. {
  2448. MatConstIterator::operator ++();
  2449. return *this;
  2450. }
  2451. template<typename _Tp> inline
  2452. MatConstIterator_<_Tp> MatConstIterator_<_Tp>::operator ++(int)
  2453. {
  2454. MatConstIterator_ b = *this;
  2455. MatConstIterator::operator ++();
  2456. return b;
  2457. }
  2458. template<typename _Tp> inline
  2459. Point MatConstIterator_<_Tp>::pos() const
  2460. {
  2461. if( !m )
  2462. return Point();
  2463. CV_DbgAssert( m->dims <= 2 );
  2464. if( m->isContinuous() )
  2465. {
  2466. ptrdiff_t ofs = (const _Tp*)ptr - (const _Tp*)m->data;
  2467. int y = (int)(ofs / m->cols);
  2468. int x = (int)(ofs - (ptrdiff_t)y * m->cols);
  2469. return Point(x, y);
  2470. }
  2471. else
  2472. {
  2473. ptrdiff_t ofs = (uchar*)ptr - m->data;
  2474. int y = (int)(ofs / m->step);
  2475. int x = (int)((ofs - y * m->step)/sizeof(_Tp));
  2476. return Point(x, y);
  2477. }
  2478. }
  2479. template<typename _Tp> static inline
  2480. bool operator == (const MatConstIterator_<_Tp>& a, const MatConstIterator_<_Tp>& b)
  2481. {
  2482. return a.m == b.m && a.ptr == b.ptr;
  2483. }
  2484. template<typename _Tp> static inline
  2485. bool operator != (const MatConstIterator_<_Tp>& a, const MatConstIterator_<_Tp>& b)
  2486. {
  2487. return a.m != b.m || a.ptr != b.ptr;
  2488. }
  2489. template<typename _Tp> static inline
  2490. MatConstIterator_<_Tp> operator + (const MatConstIterator_<_Tp>& a, ptrdiff_t ofs)
  2491. {
  2492. MatConstIterator t = (const MatConstIterator&)a + ofs;
  2493. return (MatConstIterator_<_Tp>&)t;
  2494. }
  2495. template<typename _Tp> static inline
  2496. MatConstIterator_<_Tp> operator + (ptrdiff_t ofs, const MatConstIterator_<_Tp>& a)
  2497. {
  2498. MatConstIterator t = (const MatConstIterator&)a + ofs;
  2499. return (MatConstIterator_<_Tp>&)t;
  2500. }
  2501. template<typename _Tp> static inline
  2502. MatConstIterator_<_Tp> operator - (const MatConstIterator_<_Tp>& a, ptrdiff_t ofs)
  2503. {
  2504. MatConstIterator t = (const MatConstIterator&)a - ofs;
  2505. return (MatConstIterator_<_Tp>&)t;
  2506. }
  2507. template<typename _Tp> inline
  2508. const _Tp& MatConstIterator_<_Tp>::operator [](ptrdiff_t i) const
  2509. {
  2510. return *(_Tp*)MatConstIterator::operator [](i);
  2511. }
  2512. //////////////////////////// MatIterator_ ///////////////////////////
  2513. template<typename _Tp> inline
  2514. MatIterator_<_Tp>::MatIterator_()
  2515. : MatConstIterator_<_Tp>()
  2516. {}
  2517. template<typename _Tp> inline
  2518. MatIterator_<_Tp>::MatIterator_(Mat_<_Tp>* _m)
  2519. : MatConstIterator_<_Tp>(_m)
  2520. {}
  2521. template<typename _Tp> inline
  2522. MatIterator_<_Tp>::MatIterator_(Mat_<_Tp>* _m, int _row, int _col)
  2523. : MatConstIterator_<_Tp>(_m, _row, _col)
  2524. {}
  2525. template<typename _Tp> inline
  2526. MatIterator_<_Tp>::MatIterator_(Mat_<_Tp>* _m, Point _pt)
  2527. : MatConstIterator_<_Tp>(_m, _pt)
  2528. {}
  2529. template<typename _Tp> inline
  2530. MatIterator_<_Tp>::MatIterator_(Mat_<_Tp>* _m, const int* _idx)
  2531. : MatConstIterator_<_Tp>(_m, _idx)
  2532. {}
  2533. template<typename _Tp> inline
  2534. MatIterator_<_Tp>::MatIterator_(const MatIterator_& it)
  2535. : MatConstIterator_<_Tp>(it)
  2536. {}
  2537. template<typename _Tp> inline
  2538. MatIterator_<_Tp>& MatIterator_<_Tp>::operator = (const MatIterator_<_Tp>& it )
  2539. {
  2540. MatConstIterator::operator = (it);
  2541. return *this;
  2542. }
  2543. template<typename _Tp> inline
  2544. _Tp& MatIterator_<_Tp>::operator *() const
  2545. {
  2546. return *(_Tp*)(this->ptr);
  2547. }
  2548. template<typename _Tp> inline
  2549. MatIterator_<_Tp>& MatIterator_<_Tp>::operator += (ptrdiff_t ofs)
  2550. {
  2551. MatConstIterator::operator += (ofs);
  2552. return *this;
  2553. }
  2554. template<typename _Tp> inline
  2555. MatIterator_<_Tp>& MatIterator_<_Tp>::operator -= (ptrdiff_t ofs)
  2556. {
  2557. MatConstIterator::operator += (-ofs);
  2558. return *this;
  2559. }
  2560. template<typename _Tp> inline
  2561. MatIterator_<_Tp>& MatIterator_<_Tp>::operator --()
  2562. {
  2563. MatConstIterator::operator --();
  2564. return *this;
  2565. }
  2566. template<typename _Tp> inline
  2567. MatIterator_<_Tp> MatIterator_<_Tp>::operator --(int)
  2568. {
  2569. MatIterator_ b = *this;
  2570. MatConstIterator::operator --();
  2571. return b;
  2572. }
  2573. template<typename _Tp> inline
  2574. MatIterator_<_Tp>& MatIterator_<_Tp>::operator ++()
  2575. {
  2576. MatConstIterator::operator ++();
  2577. return *this;
  2578. }
  2579. template<typename _Tp> inline
  2580. MatIterator_<_Tp> MatIterator_<_Tp>::operator ++(int)
  2581. {
  2582. MatIterator_ b = *this;
  2583. MatConstIterator::operator ++();
  2584. return b;
  2585. }
  2586. template<typename _Tp> inline
  2587. _Tp& MatIterator_<_Tp>::operator [](ptrdiff_t i) const
  2588. {
  2589. return *(*this + i);
  2590. }
  2591. template<typename _Tp> static inline
  2592. bool operator == (const MatIterator_<_Tp>& a, const MatIterator_<_Tp>& b)
  2593. {
  2594. return a.m == b.m && a.ptr == b.ptr;
  2595. }
  2596. template<typename _Tp> static inline
  2597. bool operator != (const MatIterator_<_Tp>& a, const MatIterator_<_Tp>& b)
  2598. {
  2599. return a.m != b.m || a.ptr != b.ptr;
  2600. }
  2601. template<typename _Tp> static inline
  2602. MatIterator_<_Tp> operator + (const MatIterator_<_Tp>& a, ptrdiff_t ofs)
  2603. {
  2604. MatConstIterator t = (const MatConstIterator&)a + ofs;
  2605. return (MatIterator_<_Tp>&)t;
  2606. }
  2607. template<typename _Tp> static inline
  2608. MatIterator_<_Tp> operator + (ptrdiff_t ofs, const MatIterator_<_Tp>& a)
  2609. {
  2610. MatConstIterator t = (const MatConstIterator&)a + ofs;
  2611. return (MatIterator_<_Tp>&)t;
  2612. }
  2613. template<typename _Tp> static inline
  2614. MatIterator_<_Tp> operator - (const MatIterator_<_Tp>& a, ptrdiff_t ofs)
  2615. {
  2616. MatConstIterator t = (const MatConstIterator&)a - ofs;
  2617. return (MatIterator_<_Tp>&)t;
  2618. }
  2619. /////////////////////// SparseMatConstIterator //////////////////////
  2620. inline
  2621. SparseMatConstIterator::SparseMatConstIterator()
  2622. : m(0), hashidx(0), ptr(0)
  2623. {}
  2624. inline
  2625. SparseMatConstIterator::SparseMatConstIterator(const SparseMatConstIterator& it)
  2626. : m(it.m), hashidx(it.hashidx), ptr(it.ptr)
  2627. {}
  2628. inline SparseMatConstIterator& SparseMatConstIterator::operator = (const SparseMatConstIterator& it)
  2629. {
  2630. if( this != &it )
  2631. {
  2632. m = it.m;
  2633. hashidx = it.hashidx;
  2634. ptr = it.ptr;
  2635. }
  2636. return *this;
  2637. }
  2638. template<typename _Tp> inline
  2639. const _Tp& SparseMatConstIterator::value() const
  2640. {
  2641. return *(const _Tp*)ptr;
  2642. }
  2643. inline
  2644. const SparseMat::Node* SparseMatConstIterator::node() const
  2645. {
  2646. return (ptr && m && m->hdr) ? (const SparseMat::Node*)(const void*)(ptr - m->hdr->valueOffset) : 0;
  2647. }
  2648. inline
  2649. SparseMatConstIterator SparseMatConstIterator::operator ++(int)
  2650. {
  2651. SparseMatConstIterator it = *this;
  2652. ++*this;
  2653. return it;
  2654. }
  2655. inline
  2656. void SparseMatConstIterator::seekEnd()
  2657. {
  2658. if( m && m->hdr )
  2659. {
  2660. hashidx = m->hdr->hashtab.size();
  2661. ptr = 0;
  2662. }
  2663. }
  2664. static inline
  2665. bool operator == (const SparseMatConstIterator& it1, const SparseMatConstIterator& it2)
  2666. {
  2667. return it1.m == it2.m && it1.ptr == it2.ptr;
  2668. }
  2669. static inline
  2670. bool operator != (const SparseMatConstIterator& it1, const SparseMatConstIterator& it2)
  2671. {
  2672. return !(it1 == it2);
  2673. }
  2674. ///////////////////////// SparseMatIterator /////////////////////////
  2675. inline
  2676. SparseMatIterator::SparseMatIterator()
  2677. {}
  2678. inline
  2679. SparseMatIterator::SparseMatIterator(SparseMat* _m)
  2680. : SparseMatConstIterator(_m)
  2681. {}
  2682. inline
  2683. SparseMatIterator::SparseMatIterator(const SparseMatIterator& it)
  2684. : SparseMatConstIterator(it)
  2685. {}
  2686. inline
  2687. SparseMatIterator& SparseMatIterator::operator = (const SparseMatIterator& it)
  2688. {
  2689. (SparseMatConstIterator&)*this = it;
  2690. return *this;
  2691. }
  2692. template<typename _Tp> inline
  2693. _Tp& SparseMatIterator::value() const
  2694. {
  2695. return *(_Tp*)ptr;
  2696. }
  2697. inline
  2698. SparseMat::Node* SparseMatIterator::node() const
  2699. {
  2700. return (SparseMat::Node*)SparseMatConstIterator::node();
  2701. }
  2702. inline
  2703. SparseMatIterator& SparseMatIterator::operator ++()
  2704. {
  2705. SparseMatConstIterator::operator ++();
  2706. return *this;
  2707. }
  2708. inline
  2709. SparseMatIterator SparseMatIterator::operator ++(int)
  2710. {
  2711. SparseMatIterator it = *this;
  2712. ++*this;
  2713. return it;
  2714. }
  2715. ////////////////////// SparseMatConstIterator_ //////////////////////
  2716. template<typename _Tp> inline
  2717. SparseMatConstIterator_<_Tp>::SparseMatConstIterator_()
  2718. {}
  2719. template<typename _Tp> inline
  2720. SparseMatConstIterator_<_Tp>::SparseMatConstIterator_(const SparseMat_<_Tp>* _m)
  2721. : SparseMatConstIterator(_m)
  2722. {}
  2723. template<typename _Tp> inline
  2724. SparseMatConstIterator_<_Tp>::SparseMatConstIterator_(const SparseMat* _m)
  2725. : SparseMatConstIterator(_m)
  2726. {
  2727. CV_Assert( _m->type() == traits::Type<_Tp>::value );
  2728. }
  2729. template<typename _Tp> inline
  2730. SparseMatConstIterator_<_Tp>::SparseMatConstIterator_(const SparseMatConstIterator_<_Tp>& it)
  2731. : SparseMatConstIterator(it)
  2732. {}
  2733. template<typename _Tp> inline
  2734. SparseMatConstIterator_<_Tp>& SparseMatConstIterator_<_Tp>::operator = (const SparseMatConstIterator_<_Tp>& it)
  2735. {
  2736. return reinterpret_cast<SparseMatConstIterator_<_Tp>&>
  2737. (*reinterpret_cast<SparseMatConstIterator*>(this) =
  2738. reinterpret_cast<const SparseMatConstIterator&>(it));
  2739. }
  2740. template<typename _Tp> inline
  2741. const _Tp& SparseMatConstIterator_<_Tp>::operator *() const
  2742. {
  2743. return *(const _Tp*)this->ptr;
  2744. }
  2745. template<typename _Tp> inline
  2746. SparseMatConstIterator_<_Tp>& SparseMatConstIterator_<_Tp>::operator ++()
  2747. {
  2748. SparseMatConstIterator::operator ++();
  2749. return *this;
  2750. }
  2751. template<typename _Tp> inline
  2752. SparseMatConstIterator_<_Tp> SparseMatConstIterator_<_Tp>::operator ++(int)
  2753. {
  2754. SparseMatConstIterator_<_Tp> it = *this;
  2755. SparseMatConstIterator::operator ++();
  2756. return it;
  2757. }
  2758. ///////////////////////// SparseMatIterator_ ////////////////////////
  2759. template<typename _Tp> inline
  2760. SparseMatIterator_<_Tp>::SparseMatIterator_()
  2761. {}
  2762. template<typename _Tp> inline
  2763. SparseMatIterator_<_Tp>::SparseMatIterator_(SparseMat_<_Tp>* _m)
  2764. : SparseMatConstIterator_<_Tp>(_m)
  2765. {}
  2766. template<typename _Tp> inline
  2767. SparseMatIterator_<_Tp>::SparseMatIterator_(SparseMat* _m)
  2768. : SparseMatConstIterator_<_Tp>(_m)
  2769. {}
  2770. template<typename _Tp> inline
  2771. SparseMatIterator_<_Tp>::SparseMatIterator_(const SparseMatIterator_<_Tp>& it)
  2772. : SparseMatConstIterator_<_Tp>(it)
  2773. {}
  2774. template<typename _Tp> inline
  2775. SparseMatIterator_<_Tp>& SparseMatIterator_<_Tp>::operator = (const SparseMatIterator_<_Tp>& it)
  2776. {
  2777. return reinterpret_cast<SparseMatIterator_<_Tp>&>
  2778. (*reinterpret_cast<SparseMatConstIterator*>(this) =
  2779. reinterpret_cast<const SparseMatConstIterator&>(it));
  2780. }
  2781. template<typename _Tp> inline
  2782. _Tp& SparseMatIterator_<_Tp>::operator *() const
  2783. {
  2784. return *(_Tp*)this->ptr;
  2785. }
  2786. template<typename _Tp> inline
  2787. SparseMatIterator_<_Tp>& SparseMatIterator_<_Tp>::operator ++()
  2788. {
  2789. SparseMatConstIterator::operator ++();
  2790. return *this;
  2791. }
  2792. template<typename _Tp> inline
  2793. SparseMatIterator_<_Tp> SparseMatIterator_<_Tp>::operator ++(int)
  2794. {
  2795. SparseMatIterator_<_Tp> it = *this;
  2796. SparseMatConstIterator::operator ++();
  2797. return it;
  2798. }
  2799. //////////////////////// MatCommaInitializer_ ///////////////////////
  2800. template<typename _Tp> inline
  2801. MatCommaInitializer_<_Tp>::MatCommaInitializer_(Mat_<_Tp>* _m)
  2802. : it(_m)
  2803. {}
  2804. template<typename _Tp> template<typename T2> inline
  2805. MatCommaInitializer_<_Tp>& MatCommaInitializer_<_Tp>::operator , (T2 v)
  2806. {
  2807. CV_DbgAssert( this->it < ((const Mat_<_Tp>*)this->it.m)->end() );
  2808. *this->it = _Tp(v);
  2809. ++this->it;
  2810. return *this;
  2811. }
  2812. template<typename _Tp> inline
  2813. MatCommaInitializer_<_Tp>::operator Mat_<_Tp>() const
  2814. {
  2815. CV_DbgAssert( this->it == ((const Mat_<_Tp>*)this->it.m)->end() );
  2816. return Mat_<_Tp>(*this->it.m);
  2817. }
  2818. template<typename _Tp, typename T2> static inline
  2819. MatCommaInitializer_<_Tp> operator << (const Mat_<_Tp>& m, T2 val)
  2820. {
  2821. MatCommaInitializer_<_Tp> commaInitializer((Mat_<_Tp>*)&m);
  2822. return (commaInitializer, val);
  2823. }
  2824. ///////////////////////// Matrix Expressions ////////////////////////
  2825. inline
  2826. Mat& Mat::operator = (const MatExpr& e)
  2827. {
  2828. e.op->assign(e, *this);
  2829. return *this;
  2830. }
  2831. template<typename _Tp> inline
  2832. Mat_<_Tp>::Mat_(const MatExpr& e)
  2833. {
  2834. e.op->assign(e, *this, traits::Type<_Tp>::value);
  2835. }
  2836. template<typename _Tp> inline
  2837. Mat_<_Tp>& Mat_<_Tp>::operator = (const MatExpr& e)
  2838. {
  2839. e.op->assign(e, *this, traits::Type<_Tp>::value);
  2840. return *this;
  2841. }
  2842. template<typename _Tp> inline
  2843. MatExpr Mat_<_Tp>::zeros(int rows, int cols)
  2844. {
  2845. return Mat::zeros(rows, cols, traits::Type<_Tp>::value);
  2846. }
  2847. template<typename _Tp> inline
  2848. MatExpr Mat_<_Tp>::zeros(Size sz)
  2849. {
  2850. return Mat::zeros(sz, traits::Type<_Tp>::value);
  2851. }
  2852. template<typename _Tp> inline
  2853. MatExpr Mat_<_Tp>::ones(int rows, int cols)
  2854. {
  2855. return Mat::ones(rows, cols, traits::Type<_Tp>::value);
  2856. }
  2857. template<typename _Tp> inline
  2858. MatExpr Mat_<_Tp>::ones(Size sz)
  2859. {
  2860. return Mat::ones(sz, traits::Type<_Tp>::value);
  2861. }
  2862. template<typename _Tp> inline
  2863. MatExpr Mat_<_Tp>::eye(int rows, int cols)
  2864. {
  2865. return Mat::eye(rows, cols, traits::Type<_Tp>::value);
  2866. }
  2867. template<typename _Tp> inline
  2868. MatExpr Mat_<_Tp>::eye(Size sz)
  2869. {
  2870. return Mat::eye(sz, traits::Type<_Tp>::value);
  2871. }
  2872. inline
  2873. MatExpr::MatExpr()
  2874. : op(0), flags(0), a(Mat()), b(Mat()), c(Mat()), alpha(0), beta(0), s()
  2875. {}
  2876. inline
  2877. MatExpr::MatExpr(const MatOp* _op, int _flags, const Mat& _a, const Mat& _b,
  2878. const Mat& _c, double _alpha, double _beta, const Scalar& _s)
  2879. : op(_op), flags(_flags), a(_a), b(_b), c(_c), alpha(_alpha), beta(_beta), s(_s)
  2880. {}
  2881. inline
  2882. MatExpr::operator Mat() const
  2883. {
  2884. Mat m;
  2885. op->assign(*this, m);
  2886. return m;
  2887. }
  2888. template<typename _Tp> inline
  2889. MatExpr::operator Mat_<_Tp>() const
  2890. {
  2891. Mat_<_Tp> m;
  2892. op->assign(*this, m, traits::Type<_Tp>::value);
  2893. return m;
  2894. }
  2895. template<typename _Tp> static inline
  2896. MatExpr min(const Mat_<_Tp>& a, const Mat_<_Tp>& b)
  2897. {
  2898. return cv::min((const Mat&)a, (const Mat&)b);
  2899. }
  2900. template<typename _Tp> static inline
  2901. MatExpr min(const Mat_<_Tp>& a, double s)
  2902. {
  2903. return cv::min((const Mat&)a, s);
  2904. }
  2905. template<typename _Tp> static inline
  2906. MatExpr min(double s, const Mat_<_Tp>& a)
  2907. {
  2908. return cv::min((const Mat&)a, s);
  2909. }
  2910. template<typename _Tp> static inline
  2911. MatExpr max(const Mat_<_Tp>& a, const Mat_<_Tp>& b)
  2912. {
  2913. return cv::max((const Mat&)a, (const Mat&)b);
  2914. }
  2915. template<typename _Tp> static inline
  2916. MatExpr max(const Mat_<_Tp>& a, double s)
  2917. {
  2918. return cv::max((const Mat&)a, s);
  2919. }
  2920. template<typename _Tp> static inline
  2921. MatExpr max(double s, const Mat_<_Tp>& a)
  2922. {
  2923. return cv::max((const Mat&)a, s);
  2924. }
  2925. template<typename _Tp> static inline
  2926. MatExpr abs(const Mat_<_Tp>& m)
  2927. {
  2928. return cv::abs((const Mat&)m);
  2929. }
  2930. static inline
  2931. Mat& operator += (Mat& a, const MatExpr& b)
  2932. {
  2933. b.op->augAssignAdd(b, a);
  2934. return a;
  2935. }
  2936. static inline
  2937. const Mat& operator += (const Mat& a, const MatExpr& b)
  2938. {
  2939. b.op->augAssignAdd(b, (Mat&)a);
  2940. return a;
  2941. }
  2942. template<typename _Tp> static inline
  2943. Mat_<_Tp>& operator += (Mat_<_Tp>& a, const MatExpr& b)
  2944. {
  2945. b.op->augAssignAdd(b, a);
  2946. return a;
  2947. }
  2948. template<typename _Tp> static inline
  2949. const Mat_<_Tp>& operator += (const Mat_<_Tp>& a, const MatExpr& b)
  2950. {
  2951. b.op->augAssignAdd(b, (Mat&)a);
  2952. return a;
  2953. }
  2954. static inline
  2955. Mat& operator -= (Mat& a, const MatExpr& b)
  2956. {
  2957. b.op->augAssignSubtract(b, a);
  2958. return a;
  2959. }
  2960. static inline
  2961. const Mat& operator -= (const Mat& a, const MatExpr& b)
  2962. {
  2963. b.op->augAssignSubtract(b, (Mat&)a);
  2964. return a;
  2965. }
  2966. template<typename _Tp> static inline
  2967. Mat_<_Tp>& operator -= (Mat_<_Tp>& a, const MatExpr& b)
  2968. {
  2969. b.op->augAssignSubtract(b, a);
  2970. return a;
  2971. }
  2972. template<typename _Tp> static inline
  2973. const Mat_<_Tp>& operator -= (const Mat_<_Tp>& a, const MatExpr& b)
  2974. {
  2975. b.op->augAssignSubtract(b, (Mat&)a);
  2976. return a;
  2977. }
  2978. static inline
  2979. Mat& operator *= (Mat& a, const MatExpr& b)
  2980. {
  2981. b.op->augAssignMultiply(b, a);
  2982. return a;
  2983. }
  2984. static inline
  2985. const Mat& operator *= (const Mat& a, const MatExpr& b)
  2986. {
  2987. b.op->augAssignMultiply(b, (Mat&)a);
  2988. return a;
  2989. }
  2990. template<typename _Tp> static inline
  2991. Mat_<_Tp>& operator *= (Mat_<_Tp>& a, const MatExpr& b)
  2992. {
  2993. b.op->augAssignMultiply(b, a);
  2994. return a;
  2995. }
  2996. template<typename _Tp> static inline
  2997. const Mat_<_Tp>& operator *= (const Mat_<_Tp>& a, const MatExpr& b)
  2998. {
  2999. b.op->augAssignMultiply(b, (Mat&)a);
  3000. return a;
  3001. }
  3002. static inline
  3003. Mat& operator /= (Mat& a, const MatExpr& b)
  3004. {
  3005. b.op->augAssignDivide(b, a);
  3006. return a;
  3007. }
  3008. static inline
  3009. const Mat& operator /= (const Mat& a, const MatExpr& b)
  3010. {
  3011. b.op->augAssignDivide(b, (Mat&)a);
  3012. return a;
  3013. }
  3014. template<typename _Tp> static inline
  3015. Mat_<_Tp>& operator /= (Mat_<_Tp>& a, const MatExpr& b)
  3016. {
  3017. b.op->augAssignDivide(b, a);
  3018. return a;
  3019. }
  3020. template<typename _Tp> static inline
  3021. const Mat_<_Tp>& operator /= (const Mat_<_Tp>& a, const MatExpr& b)
  3022. {
  3023. b.op->augAssignDivide(b, (Mat&)a);
  3024. return a;
  3025. }
  3026. //////////////////////////////// UMat ////////////////////////////////
  3027. inline
  3028. UMat::UMat(UMatUsageFlags _usageFlags)
  3029. : flags(MAGIC_VAL), dims(0), rows(0), cols(0), allocator(0), usageFlags(_usageFlags), u(0), offset(0), size(&rows)
  3030. {}
  3031. inline
  3032. UMat::UMat(int _rows, int _cols, int _type, UMatUsageFlags _usageFlags)
  3033. : flags(MAGIC_VAL), dims(0), rows(0), cols(0), allocator(0), usageFlags(_usageFlags), u(0), offset(0), size(&rows)
  3034. {
  3035. create(_rows, _cols, _type);
  3036. }
  3037. inline
  3038. UMat::UMat(int _rows, int _cols, int _type, const Scalar& _s, UMatUsageFlags _usageFlags)
  3039. : flags(MAGIC_VAL), dims(0), rows(0), cols(0), allocator(0), usageFlags(_usageFlags), u(0), offset(0), size(&rows)
  3040. {
  3041. create(_rows, _cols, _type);
  3042. *this = _s;
  3043. }
  3044. inline
  3045. UMat::UMat(Size _sz, int _type, UMatUsageFlags _usageFlags)
  3046. : flags(MAGIC_VAL), dims(0), rows(0), cols(0), allocator(0), usageFlags(_usageFlags), u(0), offset(0), size(&rows)
  3047. {
  3048. create( _sz.height, _sz.width, _type );
  3049. }
  3050. inline
  3051. UMat::UMat(Size _sz, int _type, const Scalar& _s, UMatUsageFlags _usageFlags)
  3052. : flags(MAGIC_VAL), dims(0), rows(0), cols(0), allocator(0), usageFlags(_usageFlags), u(0), offset(0), size(&rows)
  3053. {
  3054. create(_sz.height, _sz.width, _type);
  3055. *this = _s;
  3056. }
  3057. inline
  3058. UMat::UMat(int _dims, const int* _sz, int _type, UMatUsageFlags _usageFlags)
  3059. : flags(MAGIC_VAL), dims(0), rows(0), cols(0), allocator(0), usageFlags(_usageFlags), u(0), offset(0), size(&rows)
  3060. {
  3061. create(_dims, _sz, _type);
  3062. }
  3063. inline
  3064. UMat::UMat(int _dims, const int* _sz, int _type, const Scalar& _s, UMatUsageFlags _usageFlags)
  3065. : flags(MAGIC_VAL), dims(0), rows(0), cols(0), allocator(0), usageFlags(_usageFlags), u(0), offset(0), size(&rows)
  3066. {
  3067. create(_dims, _sz, _type);
  3068. *this = _s;
  3069. }
  3070. inline
  3071. UMat::UMat(const UMat& m)
  3072. : flags(m.flags), dims(m.dims), rows(m.rows), cols(m.cols), allocator(m.allocator),
  3073. usageFlags(m.usageFlags), u(m.u), offset(m.offset), size(&rows)
  3074. {
  3075. addref();
  3076. if( m.dims <= 2 )
  3077. {
  3078. step[0] = m.step[0]; step[1] = m.step[1];
  3079. }
  3080. else
  3081. {
  3082. dims = 0;
  3083. copySize(m);
  3084. }
  3085. }
  3086. template<typename _Tp> inline
  3087. UMat::UMat(const std::vector<_Tp>& vec, bool copyData)
  3088. : flags(MAGIC_VAL + traits::Type<_Tp>::value + CV_MAT_CONT_FLAG), dims(2), rows((int)vec.size()),
  3089. cols(1), allocator(0), usageFlags(USAGE_DEFAULT), u(0), offset(0), size(&rows)
  3090. {
  3091. if(vec.empty())
  3092. return;
  3093. if( !copyData )
  3094. {
  3095. // !!!TODO!!!
  3096. CV_Error(Error::StsNotImplemented, "");
  3097. }
  3098. else
  3099. Mat((int)vec.size(), 1, traits::Type<_Tp>::value, (uchar*)&vec[0]).copyTo(*this);
  3100. }
  3101. inline
  3102. UMat& UMat::operator = (const UMat& m)
  3103. {
  3104. if( this != &m )
  3105. {
  3106. const_cast<UMat&>(m).addref();
  3107. release();
  3108. flags = m.flags;
  3109. if( dims <= 2 && m.dims <= 2 )
  3110. {
  3111. dims = m.dims;
  3112. rows = m.rows;
  3113. cols = m.cols;
  3114. step[0] = m.step[0];
  3115. step[1] = m.step[1];
  3116. }
  3117. else
  3118. copySize(m);
  3119. allocator = m.allocator;
  3120. if (usageFlags == USAGE_DEFAULT)
  3121. usageFlags = m.usageFlags;
  3122. u = m.u;
  3123. offset = m.offset;
  3124. }
  3125. return *this;
  3126. }
  3127. inline
  3128. UMat UMat::row(int y) const
  3129. {
  3130. return UMat(*this, Range(y, y + 1), Range::all());
  3131. }
  3132. inline
  3133. UMat UMat::col(int x) const
  3134. {
  3135. return UMat(*this, Range::all(), Range(x, x + 1));
  3136. }
  3137. inline
  3138. UMat UMat::rowRange(int startrow, int endrow) const
  3139. {
  3140. return UMat(*this, Range(startrow, endrow), Range::all());
  3141. }
  3142. inline
  3143. UMat UMat::rowRange(const Range& r) const
  3144. {
  3145. return UMat(*this, r, Range::all());
  3146. }
  3147. inline
  3148. UMat UMat::colRange(int startcol, int endcol) const
  3149. {
  3150. return UMat(*this, Range::all(), Range(startcol, endcol));
  3151. }
  3152. inline
  3153. UMat UMat::colRange(const Range& r) const
  3154. {
  3155. return UMat(*this, Range::all(), r);
  3156. }
  3157. inline
  3158. UMat UMat::clone() const
  3159. {
  3160. UMat m;
  3161. copyTo(m);
  3162. return m;
  3163. }
  3164. inline
  3165. void UMat::assignTo( UMat& m, int _type ) const
  3166. {
  3167. if( _type < 0 )
  3168. m = *this;
  3169. else
  3170. convertTo(m, _type);
  3171. }
  3172. inline
  3173. void UMat::create(int _rows, int _cols, int _type, UMatUsageFlags _usageFlags)
  3174. {
  3175. _type &= TYPE_MASK;
  3176. if( dims <= 2 && rows == _rows && cols == _cols && type() == _type && u )
  3177. return;
  3178. int sz[] = {_rows, _cols};
  3179. create(2, sz, _type, _usageFlags);
  3180. }
  3181. inline
  3182. void UMat::create(Size _sz, int _type, UMatUsageFlags _usageFlags)
  3183. {
  3184. create(_sz.height, _sz.width, _type, _usageFlags);
  3185. }
  3186. inline
  3187. void UMat::addref()
  3188. {
  3189. if( u )
  3190. CV_XADD(&(u->urefcount), 1);
  3191. }
  3192. inline void UMat::release()
  3193. {
  3194. if( u && CV_XADD(&(u->urefcount), -1) == 1 )
  3195. deallocate();
  3196. for(int i = 0; i < dims; i++)
  3197. size.p[i] = 0;
  3198. u = 0;
  3199. }
  3200. inline
  3201. UMat UMat::operator()( Range _rowRange, Range _colRange ) const
  3202. {
  3203. return UMat(*this, _rowRange, _colRange);
  3204. }
  3205. inline
  3206. UMat UMat::operator()( const Rect& roi ) const
  3207. {
  3208. return UMat(*this, roi);
  3209. }
  3210. inline
  3211. UMat UMat::operator()(const Range* ranges) const
  3212. {
  3213. return UMat(*this, ranges);
  3214. }
  3215. inline
  3216. UMat UMat::operator()(const std::vector<Range>& ranges) const
  3217. {
  3218. return UMat(*this, ranges);
  3219. }
  3220. inline
  3221. bool UMat::isContinuous() const
  3222. {
  3223. return (flags & CONTINUOUS_FLAG) != 0;
  3224. }
  3225. inline
  3226. bool UMat::isSubmatrix() const
  3227. {
  3228. return (flags & SUBMATRIX_FLAG) != 0;
  3229. }
  3230. inline
  3231. size_t UMat::elemSize() const
  3232. {
  3233. size_t res = dims > 0 ? step.p[dims - 1] : 0;
  3234. CV_DbgAssert(res != 0);
  3235. return res;
  3236. }
  3237. inline
  3238. size_t UMat::elemSize1() const
  3239. {
  3240. return CV_ELEM_SIZE1(flags);
  3241. }
  3242. inline
  3243. int UMat::type() const
  3244. {
  3245. return CV_MAT_TYPE(flags);
  3246. }
  3247. inline
  3248. int UMat::depth() const
  3249. {
  3250. return CV_MAT_DEPTH(flags);
  3251. }
  3252. inline
  3253. int UMat::channels() const
  3254. {
  3255. return CV_MAT_CN(flags);
  3256. }
  3257. inline
  3258. size_t UMat::step1(int i) const
  3259. {
  3260. return step.p[i] / elemSize1();
  3261. }
  3262. inline
  3263. bool UMat::empty() const
  3264. {
  3265. return u == 0 || total() == 0 || dims == 0;
  3266. }
  3267. inline
  3268. size_t UMat::total() const
  3269. {
  3270. if( dims <= 2 )
  3271. return (size_t)rows * cols;
  3272. size_t p = 1;
  3273. for( int i = 0; i < dims; i++ )
  3274. p *= size[i];
  3275. return p;
  3276. }
  3277. inline
  3278. UMat::UMat(UMat&& m)
  3279. : flags(m.flags), dims(m.dims), rows(m.rows), cols(m.cols), allocator(m.allocator),
  3280. usageFlags(m.usageFlags), u(m.u), offset(m.offset), size(&rows)
  3281. {
  3282. if (m.dims <= 2) // move new step/size info
  3283. {
  3284. step[0] = m.step[0];
  3285. step[1] = m.step[1];
  3286. }
  3287. else
  3288. {
  3289. CV_DbgAssert(m.step.p != m.step.buf);
  3290. step.p = m.step.p;
  3291. size.p = m.size.p;
  3292. m.step.p = m.step.buf;
  3293. m.size.p = &m.rows;
  3294. }
  3295. m.flags = MAGIC_VAL; m.dims = m.rows = m.cols = 0;
  3296. m.allocator = NULL;
  3297. m.u = NULL;
  3298. m.offset = 0;
  3299. }
  3300. inline
  3301. UMat& UMat::operator = (UMat&& m)
  3302. {
  3303. if (this == &m)
  3304. return *this;
  3305. release();
  3306. flags = m.flags; dims = m.dims; rows = m.rows; cols = m.cols;
  3307. allocator = m.allocator; usageFlags = m.usageFlags;
  3308. u = m.u;
  3309. offset = m.offset;
  3310. if (step.p != step.buf) // release self step/size
  3311. {
  3312. fastFree(step.p);
  3313. step.p = step.buf;
  3314. size.p = &rows;
  3315. }
  3316. if (m.dims <= 2) // move new step/size info
  3317. {
  3318. step[0] = m.step[0];
  3319. step[1] = m.step[1];
  3320. }
  3321. else
  3322. {
  3323. CV_DbgAssert(m.step.p != m.step.buf);
  3324. step.p = m.step.p;
  3325. size.p = m.size.p;
  3326. m.step.p = m.step.buf;
  3327. m.size.p = &m.rows;
  3328. }
  3329. m.flags = MAGIC_VAL; m.dims = m.rows = m.cols = 0;
  3330. m.allocator = NULL;
  3331. m.u = NULL;
  3332. m.offset = 0;
  3333. return *this;
  3334. }
  3335. inline bool UMatData::hostCopyObsolete() const { return (flags & HOST_COPY_OBSOLETE) != 0; }
  3336. inline bool UMatData::deviceCopyObsolete() const { return (flags & DEVICE_COPY_OBSOLETE) != 0; }
  3337. inline bool UMatData::deviceMemMapped() const { return (flags & DEVICE_MEM_MAPPED) != 0; }
  3338. inline bool UMatData::copyOnMap() const { return (flags & COPY_ON_MAP) != 0; }
  3339. inline bool UMatData::tempUMat() const { return (flags & TEMP_UMAT) != 0; }
  3340. inline bool UMatData::tempCopiedUMat() const { return (flags & TEMP_COPIED_UMAT) == TEMP_COPIED_UMAT; }
  3341. inline void UMatData::markDeviceMemMapped(bool flag)
  3342. {
  3343. if(flag)
  3344. flags |= DEVICE_MEM_MAPPED;
  3345. else
  3346. flags &= ~DEVICE_MEM_MAPPED;
  3347. }
  3348. inline void UMatData::markHostCopyObsolete(bool flag)
  3349. {
  3350. if(flag)
  3351. flags |= HOST_COPY_OBSOLETE;
  3352. else
  3353. flags &= ~HOST_COPY_OBSOLETE;
  3354. }
  3355. inline void UMatData::markDeviceCopyObsolete(bool flag)
  3356. {
  3357. if(flag)
  3358. flags |= DEVICE_COPY_OBSOLETE;
  3359. else
  3360. flags &= ~DEVICE_COPY_OBSOLETE;
  3361. }
  3362. //! @endcond
  3363. } //cv
  3364. #ifdef _MSC_VER
  3365. #pragma warning( pop )
  3366. #endif
  3367. #endif