{"id":2173,"date":"2019-06-25T01:06:50","date_gmt":"2019-06-24T16:06:50","guid":{"rendered":"https:\/\/enoxsoftware.com\/opencvforunity\/?p=2173"},"modified":"2024-12-02T06:28:17","modified_gmt":"2024-12-01T21:28:17","slug":"how-to-catch-native-opencvs-errors-code","status":"publish","type":"post","link":"https:\/\/enoxsoftware.com\/how-to-catch-native-opencvs-errors-code\/","title":{"rendered":"How to catch native OpenCV\u2019s errors code (CVException handling)"},"content":{"rendered":"<p>In order to display the native opencv&#8217;s error code, please enclose the code in Utils.setDebugMode(true) and Utils.setDebugMode(false).<\/p>\n\n<p>\n\tExample Code:\n<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"classic\">\n            \/\/\n            \/\/ CVException handling example\n            \/\/\n\n            \/\/ 32F, channels=1, 3x3\n            Mat m1 = new Mat (3, 3, CvType.CV_32FC1);\n            m1.put (0, 0, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f);\n\n            \/\/ 8U, channels=1, 3x3\n            Mat m2 = new Mat (3, 3, CvType.CV_8UC1);\n            m2.put (0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);\n\n            \/\/ dump\n            Debug.Log (\"\"m1=\"\" + m1);\n            Debug.Log (\"\"m1.dump()=\"\" + m1.dump ());\n            Debug.Log (\"\"m2=\"\" + m2);\n            Debug.Log (\"\"m2.dump()=\"\" + m2.dump ());\n\n            #if UNITY_STANDALONE || UNITY_EDITOR\n            \/\/ Publish CVException to Debug.LogError.\n            Utils.setDebugMode (true, false);\n            Mat m3 = m1 \/ m2;\n            Utils.setDebugMode (false);\n\n            \/\/ Throw CVException.\n            Utils.setDebugMode (true, true);\n            try\n            {\n                Mat m4 = m1 \/ m2;\n            }\n            catch (Exception e)\n            {\n                Debug.Log (\"\"CVException: \"\" + e);\n            }\n            Utils.setDebugMode (false);\n            #else\n            Debug.Log (\"\"The setDebugMode method is only supported on WIN, MAC and LINUX.\"\");\n            #endif\n<\/pre>\n<p>\n\tExecution Result:\n<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"twilight\" data-enlighter-linenumbers=\"false\">\nm1=Mat [ 3*3*CV_32FC1, isCont=True, isSubmat=False, nativeObj=0x820637680, dataAddr=0x820295296 ]\nm1.dump()=[1, 2, 3;\n 4, 5, 6;\n 7, 8, 9]\nm2=Mat [ 3*3*CV_8UC1, isCont=True, isSubmat=False, nativeObj=0x820637792, dataAddr=0x820619712 ]\nm2.dump()=[  1,   2,   3;\n   4,   5,   6;\n   7,   8,   9]\n   \ncore::divide_12() : OpenCV(3.4.1-dev) C:\\Users\\xxxxx\\Desktop\\opencv\\modules\\core\\src\\arithm.cpp:683: error: (-5) When the input arrays in add\/subtract\/multiply\/divide functions have different types, the output array type must be explicitly specified in function cv::arithm_op\nm3=Mat [ 0*0*CV_8UC1, isCont=False, isSubmat=False, nativeObj=0x820637568, dataAddr=0x0 ]\n\nCVException: OpenCVForUnity.CvException: core::divide_12() : OpenCV(3.4.1-dev) C:\\Users\\satoo\\Desktop\\opencv\\modules\\core\\src\\arithm.cpp:683: error: (-5) When the input arrays in add\/subtract\/multiply\/divide functions have different types, the output array type must be explicitly specified in function cv::arithm_op\n<\/pre>\n","protected":false},"excerpt":{"rendered":"In order to display the native opencv&#8217;s error code, please enclose the code in Utils.setDebugMode(true) and Utils.setDebugMode(false). Example Code: \/\/ \/\/ CVException handling example \/\/ \/\/ 32F, channels=1, 3&#215;3 Mat m1 = new Mat (3, 3, CvType.CV_32FC1); m1.put (0, 0, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f); \/\/ 8U, channels=1, 3&#215;3 Mat m2 = new Mat (3, 3, CvType.CV_8UC1); m2.put (0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9); \/\/ dump Debug.Log (&#8220;&#8221;m1=&#8221;&#8221; + m1); Debug.Log (&#8220;&#8221;m1.dump()=&#8221;&#8221; + m1.dump ()); Debug.Log (&#8220;&#8221;m2=&#8221;&#8221; + m2); Debug.Log (&#8220;&#8221;m2.dump()=&#8221;&#8221; + m2.dump ()); #if UNITY_STANDALONE || UNITY_EDITOR \/\/ Publish CVException to Debug.LogError. Utils.setDebugMode (true, false); Mat m3 = m1 \/ m2; Utils.setDebugMode (false); \/\/ Throw CVException. Utils.setDebugMode (true, true); try { Mat m4 = m1 \/ m2; } catch (Exception e) { Debug.Log (&#8220;&#8221;CVException: &#8220;&#8221; + e); } Utils.setDebugMode (false); #else Debug.Log (&#8220;&#8221;The setDebugMode method is only supported on WIN, MAC and LINUX.&#8221;&#8221;); #endif Execution Result: m1=Mat [ 3*3*CV_32FC1, isCont=True, isSubmat=False, nativeObj=0x820637680, dataAddr=0x820295296 ] m1.dump()=[1, 2, 3; 4, 5, 6; 7, 8, 9] m2=Mat [ 3*3*CV_8UC1, isCont=True, isSubmat=False, nativeObj=0x820637792, dataAddr=0x820619712 ] m2.dump()=[ 1, 2, 3; 4, 5, 6; 7, 8, 9] core::divide_12() : OpenCV(3.4.1-dev) C:\\Users\\xxxxx\\Desktop\\opencv\\modules\\core\\src\\arithm.cpp:683: error: (-5) When the input arrays in add\/subtract\/multiply\/divide functions have different types, the output array type must be explicitly specified in function cv::arithm_op m3=Mat [ [&hellip;]","protected":false},"author":1,"featured_media":2328,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[7],"class_list":["post-2173","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tips","tag-opencvforunity"],"_links":{"self":[{"href":"https:\/\/enoxsoftware.com\/wp-json\/wp\/v2\/posts\/2173","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/enoxsoftware.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/enoxsoftware.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/enoxsoftware.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/enoxsoftware.com\/wp-json\/wp\/v2\/comments?post=2173"}],"version-history":[{"count":1,"href":"https:\/\/enoxsoftware.com\/wp-json\/wp\/v2\/posts\/2173\/revisions"}],"predecessor-version":[{"id":3302,"href":"https:\/\/enoxsoftware.com\/wp-json\/wp\/v2\/posts\/2173\/revisions\/3302"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/enoxsoftware.com\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/enoxsoftware.com\/wp-json\/wp\/v2\/media?parent=2173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enoxsoftware.com\/wp-json\/wp\/v2\/categories?post=2173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enoxsoftware.com\/wp-json\/wp\/v2\/tags?post=2173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}