{"id":1117,"date":"2013-04-04T22:42:59","date_gmt":"2013-04-04T14:42:59","guid":{"rendered":"http:\/\/www.magicandlove.com\/blog\/?p=1117"},"modified":"2013-04-04T22:48:03","modified_gmt":"2013-04-04T14:48:03","slug":"opencv-2-4-4-and-processing","status":"publish","type":"post","link":"http:\/\/www.magicandlove.com\/blog\/2013\/04\/04\/opencv-2-4-4-and-processing\/","title":{"rendered":"OpenCV 2.4.4 and Processing"},"content":{"rendered":"<p>The latest <a href=\"http:\/\/opencv.org\/opencv-java-api.html\">OpenCV 2.4.4 supports the desktop version of Java<\/a>. We can use the OpenCV functions in the <a href=\"http:\/\/processing.org\">Processing<\/a> environment. Before we write a library for it, we can temporarily test it by putting the opencv-244.jar and libopencv_java244.dylib into the code folder of your sketch. For Windows, the dll file should go there as well.<\/p>\n<p>And we also need to perform conversion between the <a href=\"http:\/\/processing.org\/reference\/PImage.html\">PImage<\/a> and the OpenCV Mat. I do not want to loop through the <a href=\"http:\/\/processing.org\/reference\/pixels.html\">pixels<\/a> array to use bitwise operations. In this case, I use the Java <a href=\"http:\/\/docs.oracle.com\/javase\/6\/docs\/api\/java\/awt\/image\/BufferedImage.html\">BufferedImage<\/a> and <a href=\"http:\/\/docs.oracle.com\/javase\/6\/docs\/api\/java\/awt\/image\/WritableRaster.html\">WritableRaster<\/a> classes with the 8 bits and 4 channels standard format.<\/p>\n<p>Here are the example codes that perform the GaussianBlur and Canny functions.<\/p>\n<pre lang=\"java\">\r\nimport processing.video.*;\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.Size;\r\n\r\nimport java.awt.image.BufferedImage;\r\nimport java.awt.image.WritableRaster;\r\nimport java.awt.image.Raster;\r\n\r\nCapture cap;\r\nint pixCnt;\r\nBufferedImage bm;\r\nPImage img;\r\n\r\nvoid setup() {\r\n  size(640, 480);\r\n  System.loadLibrary(Core.NATIVE_LIBRARY_NAME);\r\n  println(Core.VERSION);\r\n\r\n  cap = new Capture(this, width, height);\r\n  cap.start();\r\n  bm = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);\r\n  img = createImage(width, height, ARGB);\r\n  pixCnt = width*height*4;\r\n}\r\n\r\nvoid convert(PImage _i) {\r\n  bm.setRGB(0, 0, _i.width, _i.height, _i.pixels, 0, _i.width);\r\n  Raster rr = bm.getRaster();\r\n  byte [] b1 = new byte[pixCnt];\r\n  rr.getDataElements(0, 0, _i.width, _i.height, b1);\r\n  Mat m1 = new Mat(_i.height, _i.width, CvType.CV_8UC4);\r\n  m1.put(0, 0, b1);\r\n\r\n  Mat m2 = new Mat(_i.height, _i.width, CvType.CV_8UC1);\r\n  Imgproc.cvtColor(m1, m2, Imgproc.COLOR_BGRA2GRAY);   \r\n\r\n  Imgproc.GaussianBlur(m2, m2, new Size(7, 7), 1.5, 1.5);\r\n  Imgproc.Canny(m2, m2, 0, 30, 3, false);\r\n  Imgproc.cvtColor(m2, m1, Imgproc.COLOR_GRAY2BGRA);\r\n\r\n  m1.get(0, 0, b1);\r\n  WritableRaster wr = bm.getRaster();\r\n  wr.setDataElements(0, 0, _i.width, _i.height, b1);\r\n  bm.getRGB(0, 0, _i.width, _i.height, img.pixels, 0, _i.width);\r\n  img.updatePixels();\r\n  bm.flush();\r\n  m2.release();\r\n  m1.release();\r\n}\r\n\r\nvoid draw() {\r\n  if (cap.available()) {\r\n    background(0);\r\n    cap.read();\r\n    convert(cap);\r\n    image(img, 0, 0);\r\n  }\r\n}\r\n<\/pre>\n<p>&nbsp;<br \/>\n<iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"http:\/\/www.youtube.com\/embed\/pr1sL1oqcwg?rel=0\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The latest OpenCV 2.4.4 supports the desktop version of Java. We can use the OpenCV functions in the Processing environment. Before we write a library for it, we can temporarily test it by putting the opencv-244.jar and libopencv_java244.dylib into the code folder of your sketch. For Windows, the dll file should go there as well. [&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],"tags":[3,62],"class_list":["post-1117","post","type-post","status-publish","format-standard","hentry","category-software-2","tag-opencv","tag-processing-org"],"_links":{"self":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1117","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=1117"}],"version-history":[{"count":4,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1117\/revisions"}],"predecessor-version":[{"id":1122,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1117\/revisions\/1122"}],"wp:attachment":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/media?parent=1117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/categories?post=1117"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/tags?post=1117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}