{"id":1447,"date":"2015-05-29T23:23:32","date_gmt":"2015-05-29T15:23:32","guid":{"rendered":"http:\/\/www.magicandlove.com\/blog\/?p=1447"},"modified":"2015-07-30T14:33:09","modified_gmt":"2015-07-30T06:33:09","slug":"opencv-and-processing-6","status":"publish","type":"post","link":"http:\/\/www.magicandlove.com\/blog\/2015\/05\/29\/opencv-and-processing-6\/","title":{"rendered":"OpenCV and Processing 6"},"content":{"rendered":"<p>To perform the same image loading task in OpenCV, we use the <strong>imread()<\/strong> function in the <strong>Imgcodecs<\/strong> module. The function was in the <strong>Highgui<\/strong> module before. The image loaded in the <strong>Mat<\/strong> data structure is in BGR format. We shall use the <strong>split<\/strong> and <strong>merge<\/strong> commands to align the proper channels. Finally, we use a byte array to transfer the pixels information to the <strong>pixels[]<\/strong> array of a <strong>PImage<\/strong> object.<br \/>\n<!--more--><br \/>\nThe codes are,<\/p>\n<pre lang=\"java\">import org.opencv.core.Core;\r\nimport org.opencv.core.CvType;\r\nimport org.opencv.core.Scalar;\r\nimport org.opencv.core.Mat;\r\nimport org.opencv.imgcodecs.Imgcodecs;\r\n\r\nimport java.nio.*;\r\nimport java.util.List;\r\n\r\nPImage img;\r\nMat mat;\r\nMat alpha;\r\n\r\nvoid setup() {\r\n  size(640, 480);\r\n  background(0);\r\n  println(Core.VERSION);\r\n  System.loadLibrary(Core.NATIVE_LIBRARY_NAME);\r\n  println(dataPath(\"sample01.jpg\"));\r\n  noLoop();\r\n}\r\n\r\nvoid draw() {\r\n  mat = Imgcodecs.imread(dataPath(\"sample01.jpg\"));\r\n  Mat out = new Mat(mat.rows(), mat.cols(), CvType.CV_8UC4);\r\n  alpha = new Mat(mat.rows(), mat.cols(), CvType.CV_8UC1, Scalar.all(255));\r\n  byte [] bArray = new byte[mat.rows()*mat.cols()*4];\r\n  img = createImage(mat.cols(), mat.rows(), ARGB);\r\n  ArrayList ch1 = new ArrayList();\r\n  ArrayList ch2 = new ArrayList();\r\n\r\n  Core.split(mat, ch1);\r\n\r\n  ch2.add(alpha);\r\n  ch2.add(ch1.get(2));\r\n  ch2.add(ch1.get(1));\r\n  ch2.add(ch1.get(0));\r\n\r\n  Core.merge(ch2, out);\r\n\r\n  out.get(0, 0, bArray);\r\n  ByteBuffer.wrap(bArray).asIntBuffer().get(img.pixels);\r\n  img.updatePixels();\r\n  image(img, 0, 0);\r\n  out.release();\r\n}<\/pre>\n<pre lang=\"java\">\r\nmat = Imgcodecs.imread(dataPath(\"sample01.jpg\"));\r\n<\/pre>\n<p>The line will import the external file into a <strong>Mat<\/strong> object named mat. Another Mat object, out was defined to contain the final ARGB image. To create the alpha channel, we also need to define another single channel Mat object, alpha with all elements initialized to 255. <\/p>\n<pre lang=\"java\">\r\nalpha = new Mat(mat.rows(), mat.cols(), CvType.CV_8UC1, Scalar.all(255));\r\n<\/pre>\n<p>As we can only use byte array in this situation to transfer data to and from the Mat object, we create an empty <strong>byte<\/strong> array, bArray. The object img of type <strong>PImage<\/strong> will be used for display purpose. <\/p>\n<p>To realign the channels from BGR to ARGB, we use the <strong>split()<\/strong> and <strong>merge()<\/strong> functions. Before that, we split the original mat image into 3 separate channels in ch1. The realigned 4 channels will be in the ch2 object and merged back to the out <strong>Mat<\/strong> object. The object out uses the <strong>get()<\/strong> function to transfer its data to the <strong>byte<\/strong> array, bArray. And finally, it is transferred to the pixels[] array of the img object.<\/p>\n<pre lang=\"java\">\r\nByteBuffer.wrap(bArray).asIntBuffer().get(img.pixels);\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>To perform the same image loading task in OpenCV, we use the imread() function in the Imgcodecs module. The function was in the Highgui module before. The image loaded in the Mat data structure is in BGR format. We shall use the split and merge commands to align the proper channels. Finally, we use a [&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-1447","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\/1447","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=1447"}],"version-history":[{"count":6,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1447\/revisions"}],"predecessor-version":[{"id":1617,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1447\/revisions\/1617"}],"wp:attachment":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/media?parent=1447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/categories?post=1447"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/tags?post=1447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}