{"id":1425,"date":"2015-05-28T22:45:39","date_gmt":"2015-05-28T14:45:39","guid":{"rendered":"http:\/\/www.magicandlove.com\/blog\/?p=1425"},"modified":"2015-07-30T14:38:33","modified_gmt":"2015-07-30T06:38:33","slug":"opencv-and-processing-4","status":"publish","type":"post","link":"http:\/\/www.magicandlove.com\/blog\/2015\/05\/28\/opencv-and-processing-4\/","title":{"rendered":"OpenCV and Processing 4"},"content":{"rendered":"<p>The Java binding of OpenCV has a number of data classes in the form of MatOfxxx, where xxx is other datatype such as Int, Float, Point. This example will demonstrate the use of <a href=\"http:\/\/docs.opencv.org\/java\/org\/opencv\/core\/MatOfInt.html\">MatOfInt <\/a>class. These classes will usually be used for parameters passing between different OpenCV functions. We can manipulate it similar to array and <a href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/List.html\">List<\/a>.<br \/>\n&nbsp;<br \/>\n<!--more--><\/p>\n<pre lang=\"java\">import org.opencv.core.Core;\r\nimport org.opencv.core.MatOfInt;\r\nimport java.util.ArrayList;\r\n\r\nvoid setup() {\r\n  size(640, 480);\r\n  println(Core.VERSION);\r\n  System.loadLibrary(Core.NATIVE_LIBRARY_NAME);\r\n  noLoop();\r\n}\r\n\r\nvoid draw() {\r\n  background(0);\r\n  MatOfInt mat1 = new MatOfInt(1, 3, 5, 7, 9);\r\n  MatOfInt mat2 = new MatOfInt();\r\n  ArrayList list = new ArrayList();\r\n\r\n  for (int i : mat1.toArray()) {\r\n    println(\"From array \" + i);\r\n  }\r\n\r\n  for (int i=0; i < 10; i++) {\r\n    list.add(i);\r\n  }\r\n\r\n  mat2.fromList(list);\r\n  for (int i : mat2.toList()) {\r\n    println(\"From list \" + i);\r\n  }\r\n}<\/pre>\n<p>The first matrix mat1 is initialized with a number of integers, 1, 3, 5, 7, and 9. The second matrix mat2 is empty. The variable list is an empty <a href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/ArrayList.html\">ArrayList<\/a> of <a href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/lang\/Integer.html\">Integer<\/a>.<\/p>\n<p>The first for loop converts <strong>mat1<\/strong> into a linear array and print all the elements. The second for loop appends 10 elements to the ArrayList, <strong>list<\/strong>. The matrix, <strong>mat2<\/strong> then converts the <strong>list <\/strong>into itself as an matrix. The last for loop converts back the <strong>mat2<\/strong> to a Java <a href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/List.html\">List<\/a> and print out all the elements.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Java binding of OpenCV has a number of data classes in the form of MatOfxxx, where xxx is other datatype such as Int, Float, Point. This example will demonstrate the use of MatOfInt class. These classes will usually be used for parameters passing between different OpenCV functions. We can manipulate it similar to array [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[89,79],"tags":[3,62],"class_list":["post-1425","post","type-post","status-publish","format-standard","hentry","category-research","category-software-2","tag-opencv","tag-processing-org"],"_links":{"self":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1425","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/comments?post=1425"}],"version-history":[{"count":14,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1425\/revisions"}],"predecessor-version":[{"id":1621,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1425\/revisions\/1621"}],"wp:attachment":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/media?parent=1425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/categories?post=1425"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/tags?post=1425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}