{"id":1224,"date":"2014-02-27T22:26:01","date_gmt":"2014-02-27T14:26:01","guid":{"rendered":"http:\/\/www.magicandlove.com\/blog\/?p=1224"},"modified":"2014-03-28T13:00:38","modified_gmt":"2014-03-28T05:00:38","slug":"conversion-between-processing-pimage-and-opencv-cvmat","status":"publish","type":"post","link":"http:\/\/www.magicandlove.com\/blog\/2014\/02\/27\/conversion-between-processing-pimage-and-opencv-cvmat\/","title":{"rendered":"Conversion between Processing PImage and OpenCV cv::Mat"},"content":{"rendered":"<p>This example illustrates the use of the Java version of OpenCV. I built the OpenCV 2.4.8 in Mac OSX 10.9. After building, I copied the following two files to the <strong>code<\/strong> folder of the Processing sketch:<\/p>\n<ul>\n<li>opencv-248.jar<\/li>\n<li>libopencv_java248.dylib<\/li>\n<\/ul>\n<p>The program initialised the default video capture device. It converted the <a href=\"http:\/\/processing.org\/reference\/PImage.html\">PImage<\/a> first into an OpenCV matrix &#8211; <a href=\"http:\/\/docs.opencv.org\/java\/org\/opencv\/core\/Mat.html\">Mat<\/a>. The matrix is duplicated into another copy and then converted back to another <a href=\"http:\/\/processing.org\/reference\/PImage.html\">PImage<\/a> for display with the <a href=\"http:\/\/processing.org\/reference\/image_.html\">image<\/a> command.<br \/>\n&nbsp;<br \/>\nIt can achieve around 60 frame per second in my old iMac. Here is the screenshot.<br \/>\n&nbsp;<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2014\/02\/OpenCV001.jpg\" alt=\"\" title=\"OpenCV and Processing\" width=\"640\" height=\"480\" class=\"alignnone size-full wp-image-1228\" srcset=\"http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2014\/02\/OpenCV001.jpg 640w, http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2014\/02\/OpenCV001-300x225.jpg 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><br \/>\n&nbsp;<br \/>\n<!--more--><\/p>\n<pre lang=\"java\">\r\nimport processing.video.*;\r\n\r\nimport java.util.*;\r\nimport java.nio.*;\r\n\r\nimport org.opencv.core.Core;\r\nimport org.opencv.core.Mat;\r\nimport org.opencv.core.CvType;\r\nimport org.opencv.core.Scalar;\r\n\r\nCapture cap;\r\nPImage img;\r\n\r\nbyte [] bArray;\r\nint [] iArray;\r\nint pixCnt1, pixCnt2;\r\n\r\nvoid setup() {\r\n  size(640, 480);\r\n  background(0);\r\n  \/\/ Define and initialise the default capture device.\r\n  cap = new Capture(this, width, height);\r\n  cap.start();\r\n\r\n  \/\/ Load the OpenCV native library.\r\n  System.loadLibrary(Core.NATIVE_LIBRARY_NAME);\r\n  println(Core.VERSION);\r\n\r\n  \/\/ pixCnt1 is the number of bytes in the pixel buffer.\r\n  \/\/ pixCnt2 is the number of integers in the PImage pixels buffer.\r\n  pixCnt1 = width*height*4;\r\n  pixCnt2 = width*height;\r\n\r\n  \/\/ bArray is the temporary byte array buffer for OpenCV cv::Mat.\r\n  \/\/ iArray is the temporary integer array buffer for PImage pixels.\r\n  bArray = new byte[pixCnt1];\r\n  iArray = new int[pixCnt2];\r\n\r\n  img = createImage(width, height, ARGB);\r\n}\r\n\r\nvoid draw() {\r\n  \/\/ Copy the webcam image to the temporary integer array iArray.\r\n  arrayCopy(cap.pixels, iArray);\r\n\r\n  \/\/ Define the temporary Java byte and integer buffers. \r\n  \/\/ They share the same storage.\r\n  ByteBuffer bBuf = ByteBuffer.allocate(pixCnt1);\r\n  IntBuffer iBuf = bBuf.asIntBuffer();\r\n\r\n  \/\/ Copy the webcam image to the byte buffer iBuf.\r\n  iBuf.put(iArray);\r\n\r\n  \/\/ Copy the webcam image to the byte array bArray.\r\n  bBuf.get(bArray);\r\n\r\n  \/\/ Create the OpenCV cv::Mat.\r\n  Mat m1 = new Mat(cap.height, cap.width, CvType.CV_8UC4);\r\n\r\n  \/\/ Initialise the matrix m1 with content from bArray.\r\n  m1.put(0, 0, bArray);\r\n\r\n  \/\/ Clone m1 into another matrix m2. Now m2 is an exact copy of m1.\r\n  Mat m2 = m1.clone();\r\n\r\n  \/\/ Copy content of m2 to the byte array bArray.\r\n  m2.get(0, 0, bArray);\r\n\r\n  \/\/ Treat bArray as an integer buffer and copy the content to iArray.\r\n  ByteBuffer.wrap(bArray).asIntBuffer().get(iArray);\r\n\r\n  \/\/ Copy the content of iArray to the PImage img pixels buffer.\r\n  arrayCopy(iArray, img.pixels);\r\n  img.updatePixels();\r\n\r\n  \/\/ Display the new image img.\r\n  image(img, 0, 0);\r\n\r\n  text(\"Frame Rate: \" + round(frameRate), 500, 50);\r\n}\r\n\r\nvoid captureEvent(Capture _c) {\r\n  _c.read();\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This example illustrates the use of the Java version of OpenCV. I built the OpenCV 2.4.8 in Mac OSX 10.9. After building, I copied the following two files to the code folder of the Processing sketch: opencv-248.jar libopencv_java248.dylib The program initialised the default video capture device. It converted the PImage first into an OpenCV matrix [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79,66],"tags":[3,62],"class_list":["post-1224","post","type-post","status-publish","format-standard","hentry","category-software-2","category-testing","tag-opencv","tag-processing-org"],"_links":{"self":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1224","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=1224"}],"version-history":[{"count":6,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1224\/revisions"}],"predecessor-version":[{"id":1226,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1224\/revisions\/1226"}],"wp:attachment":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/media?parent=1224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/categories?post=1224"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/tags?post=1224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}