{"id":1457,"date":"2015-05-31T22:52:53","date_gmt":"2015-05-31T14:52:53","guid":{"rendered":"http:\/\/www.magicandlove.com\/blog\/?p=1457"},"modified":"2015-07-30T14:31:24","modified_gmt":"2015-07-30T06:31:24","slug":"opencv-and-processing-8","status":"publish","type":"post","link":"http:\/\/www.magicandlove.com\/blog\/2015\/05\/31\/opencv-and-processing-8\/","title":{"rendered":"OpenCV and Processing 8"},"content":{"rendered":"<p>The example converts a PImage (ARGB) to a Mat (BGR). It then randomly samples a pixel to compare their color values in the two formats. In order to transfer the integer array pixels[] from the PImage to the byte array of the CV_8UC4 matrix, it makes use of the <a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/nio\/ByteBuffer.html\">ByteBuffer<\/a> and <a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/nio\/IntBuffer.html\">IntBuffer<\/a>.<br \/>\n&nbsp;<br \/>\n<!--more--><\/p>\n<pre lang=\"java\">\r\n\/\/ Conversion from PImage (ARGB) to Mat (BGR)\r\nimport org.opencv.core.Core;\r\nimport org.opencv.core.Mat;\r\nimport org.opencv.core.CvType;\r\n\r\nimport java.nio.*;\r\nimport java.util.List;\r\n\r\nPImage img;\r\nMat mat;\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  img = loadImage(\"sample01.jpg\");\r\n  mat = new Mat(img.height, img.width, CvType.CV_8UC3);\r\n  noLoop();\r\n}\r\n\r\nvoid draw() {\r\n  Mat tmp = new Mat(img.height, img.width, CvType.CV_8UC4); \/\/ temporary matrix\r\n  byte [] bArray = new byte[img.width*img.height*4]; \/\/ byte array for transfer\r\n  ByteBuffer bb = ByteBuffer.allocate(img.width*img.height*4); \r\n  bb.asIntBuffer().put(img.pixels);\r\n  bb.get(bArray);\r\n  tmp.put(0, 0, bArray);\r\n  \/\/ temporary lists for alignment of color channels\r\n  ArrayList<Mat> ch1 = new ArrayList<Mat>(); \r\n  ArrayList<Mat> ch2 = new ArrayList<Mat>();\r\n  Core.split(tmp, ch1);\r\n  ch2.add(ch1.get(3));\r\n  ch2.add(ch1.get(2));\r\n  ch2.add(ch1.get(1));\r\n  Core.merge(ch2, mat);\r\n  tmp.release();\r\n  ch1.clear();\r\n  ch2.clear();\r\n\r\n  image(img, 0, 0);\r\n  \/\/ compare the color information from the PImage and Mat\r\n  int x = floor(random(img.width));\r\n  int y = floor(random(img.height));\r\n  color c1 = img.get(x, y);\r\n  double [] c2 = mat.get(y, x);\r\n  println(\"PImage color: \" + red(c1) + \",\" + green(c1) + \",\" + blue(c1));\r\n  println(\"Mat color:    \" + c2[2] + \",\" + c2[1] + \",\" + c2[0]);\r\n}\r\n<\/pre>\n<p>The tmp matrix is the buffer to read in the pixel information from the PImage, img.<\/p>\n<pre lang=\"java\">\r\nMat tmp = new Mat(img.height, img.width, CvType.CV_8UC4);\r\n<\/pre>\n<p>Through the byte array bArray, the pixel information is copied to the tmp matrix.<\/p>\n<pre lang=\"java\">\r\nbb.asIntBuffer().put(img.pixels);\r\nbb.get(bArray);\r\ntmp.put(0, 0, bArray);\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The example converts a PImage (ARGB) to a Mat (BGR). It then randomly samples a pixel to compare their color values in the two formats. In order to transfer the integer array pixels[] from the PImage to the byte array of the CV_8UC4 matrix, it makes use of the ByteBuffer and IntBuffer. &nbsp;<\/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-1457","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\/1457","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=1457"}],"version-history":[{"count":6,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1457\/revisions"}],"predecessor-version":[{"id":1615,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1457\/revisions\/1615"}],"wp:attachment":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/media?parent=1457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/categories?post=1457"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/tags?post=1457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}